Skip to content

Commit c3abaf3

Browse files
committed
fix validation
1 parent 115dc4f commit c3abaf3

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

src/languageservice/services/yamlSchemaService.ts

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,41 @@ export class YAMLSchemaService extends JSONSchemaService {
160160
let schema: JSONSchema = schemaToResolve.schema;
161161
const contextService = this.contextService;
162162

163-
if (typeof schema !== 'object' || schema === null || Array.isArray(schema)) {
164-
const invalidSchemaType = Array.isArray(schema) ? 'array' : typeof schema;
165-
resolveErrors.push(
166-
`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' is not valid:\nWrong schema: "${invalidSchemaType}", it MUST be an Object or Boolean`
167-
);
168-
} else {
169-
try {
170-
const schemaVersion = this.detectSchemaVersion(schema);
171-
const validator = this.getValidatorForVersion(schemaVersion);
172-
const metaSchemaUrl = this.getSchemaMetaSchema(schemaVersion);
173-
if (!validator.hasSchema(schema)) {
174-
validator.registerSchema({ $schema: metaSchemaUrl, type: 'string' }, schema);
175-
}
163+
// Validate schema type before processing
164+
if (schema === null) {
165+
resolveErrors.push(`Wrong schema: "null", it MUST be an Object or Boolean`);
166+
return new ResolvedSchema({}, resolveErrors);
167+
}
168+
169+
if (Array.isArray(schema)) {
170+
resolveErrors.push(`Wrong schema: "array", it MUST be an Object or Boolean`);
171+
return new ResolvedSchema({}, resolveErrors);
172+
}
173+
174+
if (typeof schema === 'string') {
175+
resolveErrors.push(`Wrong schema: "string", it MUST be an Object or Boolean`);
176+
return new ResolvedSchema({}, resolveErrors);
177+
}
176178

177-
// Validate the schema against its meta-schema using the URL directly
179+
if (typeof schema === 'number') {
180+
resolveErrors.push(`Wrong schema: "number", it MUST be an Object or Boolean`);
181+
return new ResolvedSchema({}, resolveErrors);
182+
}
183+
184+
// Only proceed if schema is an object or boolean
185+
if (typeof schema !== 'object' && typeof schema !== 'boolean') {
186+
resolveErrors.push(`Wrong schema: "${typeof schema}", it MUST be an Object or Boolean`);
187+
return new ResolvedSchema({}, resolveErrors);
188+
}
189+
190+
const schemaVersion = this.detectSchemaVersion(schema);
191+
const validator = this.getValidatorForVersion(schemaVersion);
192+
const metaSchemaUrl = this.getSchemaMetaSchema(schemaVersion);
193+
194+
// Only validate object schemas against their meta-schema
195+
// Boolean schemas (true/false) are valid by definition and don't need meta-schema validation
196+
if (typeof schema === 'object' && schema !== null) {
197+
try {
178198
const result = await validator.validate(metaSchemaUrl, schema, 'BASIC');
179199
if (!result.valid && result.errors) {
180200
const errs: string[] = [];
@@ -187,9 +207,11 @@ export class YAMLSchemaService extends JSONSchemaService {
187207
resolveErrors.push(`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);
188208
}
189209
}
190-
} catch (error) {
191-
// If meta-schema validation fails, log but don't block schema loading
192-
console.error(`Failed to validate schema meta-schema: ${error.message}`);
210+
} catch (validationError) {
211+
// If validation fails due to incompatible data, add a generic error
212+
resolveErrors.push(
213+
`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' validation failed: ${validationError.message}`
214+
);
193215
}
194216
}
195217

@@ -761,6 +783,9 @@ export class YAMLSchemaService extends JSONSchemaService {
761783
* Detect the JSON Schema version from the $schema property
762784
*/
763785
private detectSchemaVersion(schema: JSONSchema): SupportedSchemaVersion {
786+
if (!schema || typeof schema !== 'object') {
787+
return 'draft-07';
788+
}
764789
const schemaProperty = schema.$schema;
765790
if (typeof schemaProperty === 'string') {
766791
if (schemaProperty.includes('2020-12')) {

test/schemaValidation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,7 @@ obj:
18681868
const content = `foo: bar`;
18691869
const result = await parseSetup(content);
18701870
expect(result).to.have.length(1);
1871-
expect(result[0].message).to.include("Schema 'default_schema_id.yaml' is not valid");
1871+
expect(result[0].message).to.include('Wrong schema: "string", it MUST be an Object or Boolean');
18721872
expect(telemetry.messages).to.be.empty;
18731873
});
18741874

test/yamlSchemaService.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ price: 0`;
956956
sandbox.stub(yamlSchemaService as any, 'detectSchemaVersion').returns('draft-07');
957957
// eslint-disable-next-line @typescript-eslint/no-explicit-any
958958
sandbox.stub(yamlSchemaService as any, 'getValidatorForVersion').returns({
959-
registerSchema: sandbox.stub().throws(new Error('Validation failed')),
959+
validate: sandbox.stub().throws(new Error('Validation failed')),
960960
});
961961

962962
const schema: JSONSchema = {

0 commit comments

Comments
 (0)