Skip to content

Support draft-04 schemas #1065

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"ajv": "^8.17.1",
"ajv-draft-04": "^1.0.0",
"lodash": "4.17.21",
"prettier": "^3.5.0",
"request-light": "^0.5.7",
Expand Down
28 changes: 23 additions & 5 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import { JSONSchemaDescriptionExt } from '../../requestTypes';
import { SchemaVersions } from '../yamlTypes';

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

const ajv = new Ajv();
const ajv4 = new Ajv4();

const localize = nls.loadMessageBundle();

Expand All @@ -39,6 +41,10 @@ const localize = nls.loadMessageBundle();
const jsonSchema07 = require('ajv/dist/refs/json-schema-draft-07.json');
const schema07Validator = ajv.compile(jsonSchema07);

// eslint-disable-next-line @typescript-eslint/no-var-requires
const jsonSchema04 = require('ajv-draft-04/dist/refs/json-schema-draft-04.json');
const schema04Validator = ajv4.compile(jsonSchema04);

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

export enum MODIFICATION_ACTIONS {
Expand Down Expand Up @@ -164,12 +170,24 @@ export class YAMLSchemaService extends JSONSchemaService {
let schema: JSONSchema = schemaToResolve.schema;
const contextService = this.contextService;

if (!schema07Validator(schema)) {
const errs: string[] = [];
for (const err of schema07Validator.errors as DefinedError[]) {
errs.push(`${err.instancePath} : ${err.message}`);
if (schema.$schema === 'http://json-schema.org/draft-04/schema#') {
if (!schema04Validator(schema)) {
if (!schema07Validator(schema)) {
const errs: string[] = [];
for (const err of schema07Validator.errors as DefinedError[]) {
errs.push(`${err.instancePath} : ${err.message}`);
}
resolveErrors.push(`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);
}
}
} else {
if (!schema07Validator(schema)) {
const errs: string[] = [];
for (const err of schema07Validator.errors as DefinedError[]) {
errs.push(`${err.instancePath} : ${err.message}`);
}
resolveErrors.push(`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);
}
resolveErrors.push(`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);
}

const findSection = (schema: JSONSchema, path: string): JSONSchema => {
Expand Down
32 changes: 32 additions & 0 deletions test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2094,4 +2094,36 @@ obj:
const result = await parseSetup(content);
assert.equal(result.length, 0);
});

it('draft-04 schema', async () => {
const schema: JSONSchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
type: 'object',
properties: {
myProperty: {
$ref: '#/definitions/Interface%3Ctype%3E',
},
},
definitions: {
'Interface<type>': {
type: 'object',
properties: {
foo: {
type: 'string',
},
multipleOf: {
type: 'number',
minimum: 0,
exclusiveMinimum: true,
},
},
},
},
};
schemaProvider.addSchema(SCHEMA_ID, schema);
const content = `myProperty:\n foo: bar\n multipleOf: 1`;
const result = await parseSetup(content);
console.log(result);
assert.equal(result.length, 0);
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,11 @@ aggregate-error@^3.0.0:
clean-stack "^2.0.0"
indent-string "^4.0.0"

ajv-draft-04@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ajv-draft-04/-/ajv-draft-04-1.0.0.tgz#3b64761b268ba0b9e668f0b41ba53fce0ad77fc8"
integrity sha512-mv00Te6nmYbRp5DCwclxtt7yV/joXJPGS7nM+97GdxvuttCOfgI3K4U25zboyeX0O+myI8ERluxQe5wljMmVIw==

ajv@^6.10.0, ajv@^6.12.3, ajv@^6.12.4:
version "6.12.6"
resolved "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz"
Expand Down