Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Adds an error message if the form indentation is wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Nov 16, 2015
1 parent 049cb00 commit 8150dff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
Unreleased
~~~~~~~~~~

- Adds an error message if the form indentation is wrong.
[href]

- Adds an error message for duplicate labels.
[href]

Expand Down
17 changes: 14 additions & 3 deletions onegov/form/parser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,30 +627,39 @@ def translate_to_yaml(text):
"""

lines = ((ix, l) for ix, l in prepare(text))
expect_nested = False

for ix, line in lines:

indent = ' ' * (4 + (len(line) - len(line.lstrip())))

# the top level are the fieldsets
if match(elements.fieldset_title, line):
yield '- "{}":'.format(line.lstrip('# ').rstrip())
expect_nested = False
continue

# fields are nested lists of dictionaries
parse_result = try_parse(elements.single_line_fields, line)
if parse_result is not None:
yield '{indent}- "{identifier}": !{type} "{definition}"'.format(
indent=' ' * (4 + (len(line) - len(line.lstrip()))),
indent=indent,
type=parse_result.type,
identifier=line.split('=')[0].strip(),
definition=line.split('=')[1].strip()
)
expect_nested = indent and True or False
continue

# checkboxes/radios come without identifier
parse_result = try_parse(elements.boxes, line)
if parse_result is not None:

if not expect_nested:
raise errors.InvalidFormSyntax(line=ix + 1)

yield '{indent}- !{type} "{definition}":'.format(
indent=' ' * (4 + (len(line) - len(line.lstrip()))),
indent=indent,
type=parse_result.type,
definition=line.strip()
)
Expand All @@ -664,9 +673,11 @@ def translate_to_yaml(text):
raise errors.InvalidFormSyntax(line=ix + 1)

yield '{indent}- "{identifier}":'.format(
indent=' ' * (4 + (len(line) - len(line.lstrip()))),
indent=indent,
identifier=line.strip()
)

expect_nested = True
continue

raise errors.InvalidFormSyntax(line=ix + 1)
Expand Down
8 changes: 8 additions & 0 deletions onegov/form/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,11 @@ def test_invalid_syntax():
"Last Name * __"
)))
assert e.value.line == 4

with pytest.raises(errors.InvalidFormSyntax) as e:
parse_form('\n'.join((
"# Fields",
"",
" [x] What",
)))
assert e.value.line == 3

0 comments on commit 8150dff

Please sign in to comment.