diff --git a/src/languageservice/parser/jsonParser07.ts b/src/languageservice/parser/jsonParser07.ts index 4b9401a4..eae34091 100644 --- a/src/languageservice/parser/jsonParser07.ts +++ b/src/languageservice/parser/jsonParser07.ts @@ -1251,7 +1251,7 @@ function validate( validationResult.problems.push({ location: location, severity: DiagnosticSeverity.Warning, - message: getWarningMessage(ProblemType.missingRequiredPropWarning, [propertyName]), + message: schema.errorMessage || getWarningMessage(ProblemType.missingRequiredPropWarning, [propertyName]), source: getSchemaSource(schema, originalSchema), schemaUri: getSchemaUri(schema, originalSchema), problemArgs: [propertyName], diff --git a/test/schemaValidation.test.ts b/test/schemaValidation.test.ts index edb73810..b2ad7bc2 100644 --- a/test/schemaValidation.test.ts +++ b/test/schemaValidation.test.ts @@ -1409,6 +1409,46 @@ obj: const result = await parseSetup(content); expect(result[0].message).to.eq('Missing property "pineapple".'); }); + it('should add errorMessage from schema when the property is missing', async () => { + schemaProvider.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + icon: { + type: 'string', + }, + }, + required: ['title'], + errorMessage: 'Custom message', + }); + const content = ''; + const result = await parseSetup(content); + expect(result[0].message).to.eq('Custom message'); + }); + it('should add errorMessage from sub-schema when the property is missing', async () => { + schemaProvider.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + icon: { + type: 'string', + }, + title: { + type: 'string', + }, + }, + anyOf: [ + { + required: ['title'], + errorMessage: 'At least one of `title` or `icon` must be defined.', + }, + { + required: ['icon'], + }, + ], + }); + const content = ''; + const result = await parseSetup(content); + expect(result[0].message).to.eq('At least one of `title` or `icon` must be defined.'); + }); describe('filePatternAssociation', () => { const schema = {