Skip to content

Commit

Permalink
fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Nov 9, 2024
1 parent cb2678a commit e8674a8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
6 changes: 5 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,11 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
ntk = p.nextNotCommentToken()
antk = p.afterNextNotCommentToken()
}
if tk := p.nextNotCommentToken(); tk != nil && tk.Position.Line > node.Start.Position.Line && tk.Position.Column > node.Start.Position.Column {
validationTk := node.Start
if len(node.Values) != 0 {
validationTk = node.Values[len(node.Values)-1].Key.GetToken()
}
if tk := p.nextNotCommentToken(); tk != nil && tk.Position.Line > validationTk.Position.Line && tk.Position.Column > validationTk.Position.Column {
// a: b
// c <= this token is invalid.
return nil, errors.ErrSyntax("value is not allowed in this context", tk)
Expand Down
47 changes: 47 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,20 @@ i: 'j'
"e": "f"
g: "h"
i: 'j'
`,
},
{
`
a:
- |2
b
c: d
`,
`
a:
- |2
b
c: d
`,
},
}
Expand Down Expand Up @@ -1165,6 +1179,39 @@ b: - 2
^
`,
},
{
`
a:
- |
b
c: d
`,
`
[5:5] value is not allowed in this context
2 | a:
3 | - |
4 | b
> 5 | c: d
^
`,
},
{
`
a:
- |
b
c:
d: e
`,
`
[5:5] value is not allowed in this context
2 | a:
3 | - |
4 | b
> 5 | c:
^
6 | d: e`,
},
}
for _, test := range tests {
t.Run(test.source, func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@ func (s *Scanner) scanDocument(ctx *Context, c rune) error {
s.progressColumn(ctx, 1)
} else {
ctx.updateDocumentLineIndentColumn(s.column)
if ctx.docFirstLineIndentColumn > 0 {
s.lastDelimColumn = ctx.docFirstLineIndentColumn - 1
}
if err := ctx.validateDocumentLineIndentColumn(); err != nil {
invalidTk := token.Invalid(string(ctx.obuf), s.pos())
s.progressColumn(ctx, 1)
Expand Down
16 changes: 8 additions & 8 deletions testdata/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ name: myDocument
roles:
name: myRole
permissions:
- hello
- how
- are
- you
`,
- hello
- how
- are
- you
`,
ExpectedErr: `[4:7] mapping was used where sequence is expected
1 | ---
2 | name: myDocument
3 | roles:
> 4 | name: myRole
^
5 | permissions:
6 | - hello
7 | - how
8 | `,
6 | - hello
7 | - how
8 | `,
Instance: &struct {
Name string `yaml:"name"`
Roles []struct {
Expand Down

0 comments on commit e8674a8

Please sign in to comment.