Keyan Rhm Keyan Rhm - 4 months ago 11
YAML Question

YAML File has a syntax error, but it looks like it has the same format as another one that is working?

I have two YAML files with similar formats, however one is running and the other is not running when I run integration tests on my rails app.

Here is the first YAML file which doesn't work:

prince_edward_island:
name: 'prince edward island'
country: 'Canada'


nova_scotia:
name: 'nova scotia'
country: 'Canada'

newfoundland:
name: 'newfoundland'
country: 'Canada'

new_brunswick:
name: 'new brunswick'
country: 'Canada'

quebec:
name: 'quebec'
country: 'Canada'

manitoba:
name: 'manitoba'
country: 'Canada'

alberta:
name: 'alberta'
country: 'Canada'

ontario:
name: 'ontario'
country: 'Canada'

british_columbia: 'Canada'
name: 'british columbia'
country: 'Canada'

saskatchewan: 'Canada'
name: 'saskatchewan'
country: 'Canada'


Here is the second YAML file which does work:

student_advisor_message:
body: 'This is a message'
subject: 'Subject!'
user: student_advisor

manager_message:
body: 'This is a message'
subject: 'Subject!'
user: manager

read_manager_message:
body: 'This is a message'
subject: 'Subject!'
user: manager

agent_manager_message:
body: 'This is a message'
subject: 'Subject!'
user: agent_manager

message_for_jack:
body: 'This is a message'
subject: 'Subject!'
user: manager

message_for_jack_sent:
body: 'This is a sent message'
subject: 'Sent Subject!'
user: manager

message_for_jack_trash:
body: 'This is a trashed message'
subject: 'Trash Subject!'
user: manager


The error is:

ERROR:

while parsing a block mapping
in "<unicode string>", line 1, column 1:
prince_edward_island:
^
expected <block end>, but found '<block mapping start>'
in "<unicode string>", line 34, column 3:
name: 'british columbia'


Can someone explain the difference I am missing between the two? For both I use spaces, not tabs.

Answer

There are several errors:

For both I use tabs, not spaces.

Tabs are not allowed as indentation characters in YAML.

country: 'Canada'

  nova_scotia:

Here, you give both a scalar ('Canada') and a mapping (starting with nova_scotia:) as value for the key country. There may only be one value for this key. You either have to remove the scalar 'Canada' or move the mapping it contains elsewhere.

british_columbia: 'Canada'
  name: 'british columbia'
  country: 'Canada'

saskatchewan: 'Canada'
  name: 'saskatchewan'
  country: 'Canada'

Here, you make the same error again twice.