Skip to content

Commit 9674dc9

Browse files
authored
fix: allows pattern error in mustMatchProperty functionality (#28)
* fix: allows pattern error in mustMatchProperty functionality * chore: add unit test
1 parent b5c96ca commit 9674dc9

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/languageserver/handlers/requestHandlers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export class RequestHandlers {
5252
*/
5353
this.connection.onRequest(RevalidateRequest.type, async (uri: string) => {
5454
const document = this.yamlSettings.documents.get(uri);
55+
if (!document) {
56+
console.log('Revalidate: No document found for uri: ' + uri);
57+
}
5558
await this.validationHandler.validate(document);
5659
});
5760

src/languageservice/parser/jsonParser07.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ function validate(
767767
matchingSchemas: ISchemaCollector;
768768
} = null;
769769

770+
let alternativesFiltered = alternatives;
770771
// jigx custom: remove subSchemas if the mustMatchProps (`type`, `provider`) is different
771772
// another idea is to add some attribute to schema, so type will have `mustMatch` attribute - this could work in general not only for jigx
772773
const mustMatchProps = ['type', 'provider'];
@@ -781,7 +782,7 @@ function validate(
781782
}
782783

783784
// take only subSchemas that have the same mustMatch property in yaml and in schema
784-
alternatives = alternatives.filter((subSchemaRef) => {
785+
alternativesFiltered = alternatives.filter((subSchemaRef) => {
785786
const subSchema = asSchema(subSchemaRef);
786787

787788
const typeSchemaProp = subSchema.properties?.[mustMatch];
@@ -795,6 +796,8 @@ function validate(
795796
validate(mustMatchYamlProp, typeSchemaProp, subSchema, subValidationResult, subMatchingSchemas, options);
796797
if (
797798
!subValidationResult.hasProblems() ||
799+
// allows some of the other errors like: patterns validations
800+
subValidationResult.enumValueMatch ||
798801
// a little bit hack that I wasn't able to solve it in official YLS
799802
// problem: some schemas look like this: `provider: { anyOf: [{enum: ['pr1', 'pr2']}, {type: 'string', title: 'expression'}] }`
800803
// and with yaml value `provider: =expression`
@@ -817,7 +820,7 @@ function validate(
817820

818821
// if no match, just return
819822
// example is jig.list with anyOf in the root... so types are in anyOf[0]
820-
if (!alternatives.length) {
823+
if (!alternativesFiltered.length) {
821824
const data = validationData[mustMatch];
822825
// const values = [...new Set(data.values)];
823826
validationResult.problems.push({
@@ -836,6 +839,8 @@ function validate(
836839
// don't need to check other mustMatchProps (`type` => `provider`)
837840
break;
838841
}
842+
843+
alternatives = alternativesFiltered;
839844
// end jigx custom
840845

841846
for (const subSchemaRef of alternatives) {

test/schemaValidation.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,35 @@ data:
19461946
const result = await parseSetup(content);
19471947
expect(result?.map((r) => r.message)).deep.equals(['Missing property "b".']);
19481948
});
1949+
1950+
it('should allow provider with invalid value and propagate inner pattern error', async () => {
1951+
const schema = {
1952+
anyOf: [
1953+
{
1954+
properties: {
1955+
provider: {
1956+
const: 'provider1',
1957+
pattern: '^$',
1958+
patternErrorMessage: 'Try to avoid provider1',
1959+
},
1960+
},
1961+
required: ['provider'],
1962+
},
1963+
{
1964+
properties: {
1965+
provider: {
1966+
const: 'provider2',
1967+
},
1968+
},
1969+
required: ['provider'],
1970+
},
1971+
],
1972+
};
1973+
schemaProvider.addSchema(SCHEMA_ID, schema);
1974+
const content = 'provider: provider1';
1975+
const result = await parseSetup(content);
1976+
expect(result?.map((r) => r.message)).deep.equals(['Try to avoid provider1']);
1977+
});
19491978
});
19501979

19511980
it('Expression is valid inline object', async function () {

0 commit comments

Comments
 (0)