Skip to content

Commit 8ebaf45

Browse files
feat: add support for draft-04 (2019 and 2020 included) json schemas while supporting draft-07 (#1006)
* feat: add support for draft-04 json schemas * feat: add support for the rest of the drafts in ajv * fix: use readFile instead of require to load fixture that is needed as a string instead of parsed json * resolveJsonModule to true to be able to require ajv json schema * revert the use of resolve json because the umd build does not support it * remove support for draft-06 to avoid linting problems --------- Co-authored-by: Muthurajan Sivasubramanian <93245779+msivasubramaniaan@users.noreply.github.com>
1 parent 8a38d41 commit 8ebaf45

File tree

5 files changed

+1752
-9
lines changed

5 files changed

+1752
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"dependencies": {
3333
"ajv": "^8.11.0",
34+
"ajv-draft-04": "^1.0.0",
3435
"lodash": "4.17.21",
3536
"prettier": "^3.0.0",
3637
"request-light": "^0.5.7",

src/languageservice/services/yamlSchemaService.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ import { JSONSchemaDescriptionExt } from '../../requestTypes';
2828
import { SchemaVersions } from '../yamlTypes';
2929

3030
import Ajv, { DefinedError } from 'ajv';
31+
import Ajv2019 from 'ajv/dist/2019';
32+
import Ajv2020 from 'ajv/dist/2020';
33+
import Ajv04 from 'ajv-draft-04';
3134
import { getSchemaTitle } from '../utils/schemaUtils';
3235

33-
const localize = nls.loadMessageBundle();
34-
3536
const ajv = new Ajv();
37+
const ajv04 = new Ajv04();
38+
const ajv2019 = new Ajv2019();
39+
const ajv2020 = new Ajv2020();
3640

37-
// load JSON Schema 07 def to validate loaded schemas
38-
// eslint-disable-next-line @typescript-eslint/no-var-requires
39-
const jsonSchema07 = require('ajv/dist/refs/json-schema-draft-07.json');
40-
const schema07Validator = ajv.compile(jsonSchema07);
41+
const localize = nls.loadMessageBundle();
4142

4243
export declare type CustomSchemaProvider = (uri: string) => Promise<string | string[]>;
4344

@@ -164,9 +165,36 @@ export class YAMLSchemaService extends JSONSchemaService {
164165
let schema: JSONSchema = schemaToResolve.schema;
165166
const contextService = this.contextService;
166167

167-
if (!schema07Validator(schema)) {
168+
let validationErrors: DefinedError[] = [];
169+
switch (this.normalizeId(schema.$schema)) {
170+
case ajv04.defaultMeta(): {
171+
if (!ajv04.validateSchema(schema)) {
172+
validationErrors = validationErrors.concat(ajv04.errors as DefinedError[]);
173+
}
174+
break;
175+
}
176+
case ajv2019.defaultMeta(): {
177+
if (!ajv2019.validateSchema(schema)) {
178+
validationErrors = validationErrors.concat(ajv2019.errors as DefinedError[]);
179+
}
180+
break;
181+
}
182+
case ajv2020.defaultMeta(): {
183+
if (!ajv2020.validateSchema(schema)) {
184+
validationErrors = validationErrors.concat(ajv2020.errors as DefinedError[]);
185+
}
186+
break;
187+
}
188+
default:
189+
if (!ajv.validateSchema(schema)) {
190+
validationErrors = validationErrors.concat(ajv.errors as DefinedError[]);
191+
}
192+
break;
193+
}
194+
195+
if (validationErrors.length > 0) {
168196
const errs: string[] = [];
169-
for (const err of schema07Validator.errors as DefinedError[]) {
197+
for (const err of validationErrors) {
170198
errs.push(`${err.instancePath} : ${err.message}`);
171199
}
172200
resolveErrors.push(`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);

0 commit comments

Comments
 (0)