Skip to content

Commit

Permalink
fix: nested anyof const (#888)
Browse files Browse the repository at this point in the history
* fix: nested anyof const

* fix: test and const partial match when callFromAutoComplete
  • Loading branch information
p-spacek authored Jul 10, 2023
1 parent bddfbff commit c2c2979
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/languageservice/parser/jsonParser07.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,10 @@ function validate(
validationResult.merge(bestMatch.validationResult);
validationResult.propertiesMatches += bestMatch.validationResult.propertiesMatches;
validationResult.propertiesValueMatches += bestMatch.validationResult.propertiesValueMatches;
validationResult.enumValueMatch = validationResult.enumValueMatch || bestMatch.validationResult.enumValueMatch;
if (bestMatch.validationResult.enumValues?.length) {
validationResult.enumValues = (validationResult.enumValues || []).concat(bestMatch.validationResult.enumValues);
}
matchingSchemas.merge(bestMatch.matchingSchemas);
}
return matches.length;
Expand Down Expand Up @@ -890,7 +894,10 @@ function validate(

if (isDefined(schema.const)) {
const val = getNodeValue(node);
if (!equals(val, schema.const)) {
if (
!equals(val, schema.const) &&
!(callFromAutoComplete && isString(val) && isString(schema.const) && schema.const.startsWith(val))
) {
validationResult.problems.push({
location: { offset: node.offset, length: node.length },
severity: DiagnosticSeverity.Warning,
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/utils/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function isNumber(val: unknown): val is number {
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function isDefined(val: unknown): val is object {
export function isDefined(val: unknown): val is object | string | number | boolean {
return typeof val !== 'undefined';
}

Expand Down
50 changes: 50 additions & 0 deletions test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1974,4 +1974,54 @@ obj:
expect(telemetry.messages).to.be.empty;
});
});
it('Nested AnyOf const should correctly evaluate and merge problems', async () => {
// note that 'missing form property' is necessary to trigger the bug (there has to be some problem in both subSchemas)
// order of the object in `anyOf` is also important
const schema: JSONSchema = {
type: 'object',
properties: {
options: {
anyOf: [
{
type: 'object',
properties: {
form: {
type: 'string',
},
provider: {
type: 'string',
const: 'test1',
},
},
required: ['form', 'provider'],
},
{
type: 'object',
properties: {
form: {
type: 'string',
},
provider: {
anyOf: [
{
type: 'string',
const: 'testX',
},
],
},
},
required: ['form', 'provider'],
},
],
},
},
};
schemaProvider.addSchema(SCHEMA_ID, schema);
const content = `options:\n provider: testX`;
const result = await parseSetup(content);
assert.deepEqual(
result.map((e) => e.message),
['Missing property "form".'] // not inclide provider error
);
});
});

0 comments on commit c2c2979

Please sign in to comment.