Skip to content

Commit

Permalink
Ensure parser uses errorMessage for minContains/maxContains (#229)
Browse files Browse the repository at this point in the history
Prior to this change, the language server ignored any defined
`errorMessage` when a subschema fails validation for a `contains`
keyword and `minContains` is defined. The `errorMessage` was only
used when `minContains` was not defined and no item in the array
matched the subschema. Even if `minContains` isn't defined, the parser
ignores `errorMessage` when the array contains more matching items than
`maxContains` allows.

This change updates the parser to use the `errorMessage`, if defined,
for any failure of the `contains` keyword, regardless of the values of
`minContains` and `maxContains`.

This is useful for cases where a developer wants to define an error
message indicating both what the schema expects to contain and how many
times. The default error message is not very helpful in these cases,
because most users won't know how to look up "the contains contraint"
for whatever file they're working with.
  • Loading branch information
michaeltlombardi authored Apr 22, 2024
1 parent ad4b805 commit 31dc8ea
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/parser/jsonParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,13 +810,13 @@ function validate(n: ASTNode | undefined, schema: JSONSchema, validationResult:
if (isNumber(schema.minContains) && containsCount < schema.minContains) {
validationResult.problems.push({
location: { offset: node.offset, length: node.length },
message: l10n.t('Array has too few items that match the contains contraint. Expected {0} or more.', schema.minContains)
message: schema.errorMessage || l10n.t('Array has too few items that match the contains contraint. Expected {0} or more.', schema.minContains)
});
}
if (isNumber(schema.maxContains) && containsCount > schema.maxContains) {
validationResult.problems.push({
location: { offset: node.offset, length: node.length },
message: l10n.t('Array has too many items that match the contains contraint. Expected {0} or less.', schema.maxContains)
message: schema.errorMessage || l10n.t('Array has too many items that match the contains contraint. Expected {0} or less.', schema.maxContains)
});
}
}
Expand Down

0 comments on commit 31dc8ea

Please sign in to comment.