Skip to content

Commit

Permalink
feat: add support for draft-04 json schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidonium committed Dec 26, 2024
1 parent 1e3d5ce commit 5704546
Show file tree
Hide file tree
Showing 6 changed files with 1,728 additions and 10 deletions.
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
22 changes: 14 additions & 8 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ import { JSONSchemaDescriptionExt } from '../../requestTypes';
import { SchemaVersions } from '../yamlTypes';

import Ajv, { DefinedError } from 'ajv';
import Ajv04 from 'ajv-draft-04';
import { getSchemaTitle } from '../utils/schemaUtils';

const localize = nls.loadMessageBundle();

const ajv = new Ajv();
const ajv04 = new Ajv04();

// 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 localize = nls.loadMessageBundle();

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

Expand Down Expand Up @@ -164,9 +161,18 @@ export class YAMLSchemaService extends JSONSchemaService {
let schema: JSONSchema = schemaToResolve.schema;
const contextService = this.contextService;

if (!schema07Validator(schema)) {
let validationErrors: DefinedError[] = [];
if (this.normalizeId(schema.$schema) == ajv04.defaultMeta()) {
if (!ajv04.validateSchema(schema)) {
validationErrors = validationErrors.concat(ajv04.errors as DefinedError[]);
}
} else if (!ajv.validateSchema(schema)) {
validationErrors = validationErrors.concat(ajv.errors as DefinedError[]);
}

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

0 comments on commit 5704546

Please sign in to comment.