Skip to content

Commit

Permalink
feat: add support for the rest of the drafts in ajv
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidonium committed Dec 27, 2024
1 parent 5704546 commit 90c46d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
37 changes: 32 additions & 5 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ import { JSONSchemaDescriptionExt } from '../../requestTypes';
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 ajv = new Ajv();

// eslint-disable-next-line @typescript-eslint/no-var-requires
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();

Expand Down Expand Up @@ -162,12 +171,30 @@ export class YAMLSchemaService extends JSONSchemaService {
const contextService = this.contextService;

let validationErrors: DefinedError[] = [];
if (this.normalizeId(schema.$schema) == ajv04.defaultMeta()) {
if (!ajv04.validateSchema(schema)) {
validationErrors = validationErrors.concat(ajv04.errors as 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;
}
} else if (!ajv.validateSchema(schema)) {
validationErrors = validationErrors.concat(ajv.errors as DefinedError[]);
default:
if (!ajv.validateSchema(schema)) {
validationErrors = validationErrors.concat(ajv.errors as DefinedError[]);
}
break;
}

if (validationErrors.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion test/yamlLanguageService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('getLanguageService()', () => {
// Setup object that resolves schema content
schemaContentMap[schemaUri] = `
{
"$schema": "http://json-schema.org/draft-07/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"firstName": {
Expand Down

0 comments on commit 90c46d2

Please sign in to comment.