Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for draft-04 (2019 and 2020 included) json schemas while supporting draft-07 #1006

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"dependencies": {
"ajv": "^8.11.0",
"ajv-draft-04": "^1.0.0",
"lodash": "4.17.21",
"prettier": "^3.0.0",
"request-light": "^0.5.7",
Expand Down
47 changes: 40 additions & 7 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@
import { SchemaVersions } from '../yamlTypes';

import Ajv, { DefinedError } from 'ajv';
import Ajv2019 from 'ajv/dist/2019';
import Ajv2020 from 'ajv/dist/2020';
import Ajv04 from 'ajv-draft-04';
import { getSchemaTitle } from '../utils/schemaUtils';

const localize = nls.loadMessageBundle();

const ajv = new Ajv();

// load JSON Schema 07 def to validate loaded schemas
// eslint-disable-next-line @typescript-eslint/no-var-requires
const jsonSchema07 = require('ajv/dist/refs/json-schema-draft-07.json');
const schema07Validator = ajv.compile(jsonSchema07);
const draft6MetaSchema = require('ajv/dist/refs/json-schema-draft-06.json');
ajv.addMetaSchema(draft6MetaSchema);

const ajv04 = new Ajv04();
const ajv2019 = new Ajv2019();
const ajv2020 = new Ajv2020();

const localize = nls.loadMessageBundle();

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

Expand Down Expand Up @@ -164,9 +170,36 @@
let schema: JSONSchema = schemaToResolve.schema;
const contextService = this.contextService;

if (!schema07Validator(schema)) {
let validationErrors: DefinedError[] = [];
switch (this.normalizeId(schema.$schema)) {
case ajv04.defaultMeta(): {
if (!ajv04.validateSchema(schema)) {
validationErrors = validationErrors.concat(ajv04.errors as DefinedError[]);
}
break;
}
case ajv2019.defaultMeta(): {
if (!ajv2019.validateSchema(schema)) {
validationErrors = validationErrors.concat(ajv2019.errors as DefinedError[]);
}
break;
}
case ajv2020.defaultMeta(): {
if (!ajv2020.validateSchema(schema)) {
validationErrors = validationErrors.concat(ajv2020.errors as DefinedError[]);
}
break;
}
default:
if (!ajv.validateSchema(schema)) {
validationErrors = validationErrors.concat(ajv.errors as DefinedError[]);
}
break;
}

if (validationErrors.length > 0) {
const errs: string[] = [];
for (const err of schema07Validator.errors as DefinedError[]) {
for (const err of validationErrors) {
errs.push(`${err.instancePath} : ${err.message}`);
}
resolveErrors.push(`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);
Expand Down
Loading
Loading