Skip to content
Merged
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
15 changes: 13 additions & 2 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,11 @@ 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);
const SCHEMA_04_URI_WITH_HTTPS = ajv4.defaultMeta().replace('http://', 'https://');

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

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

if (!schema07Validator(schema)) {
const validator =
this.normalizeId(schema.$schema) === ajv4.defaultMeta() || this.normalizeId(schema.$schema) === SCHEMA_04_URI_WITH_HTTPS
? schema04Validator
: schema07Validator;
if (!validator(schema)) {
const errs: string[] = [];
for (const err of schema07Validator.errors as DefinedError[]) {
for (const err of validator.errors as DefinedError[]) {
errs.push(`${err.instancePath} : ${err.message}`);
}
resolveErrors.push(`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);
Expand Down
62 changes: 62 additions & 0 deletions test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2151,4 +2151,66 @@ obj:
result = await parseSetup(content);
expect(result.length).to.eq(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);
assert.equal(result.length, 0);
});

it('draft-04 schema with https in metaschema URI', async () => {
const schema: JSONSchema = {
$schema: 'https://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);
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