diff --git a/packages/formats/src/__tests__/jsonSchema.test.ts b/packages/formats/src/__tests__/jsonSchema.test.ts index 9a185607f..e9f648fe6 100644 --- a/packages/formats/src/__tests__/jsonSchema.test.ts +++ b/packages/formats/src/__tests__/jsonSchema.test.ts @@ -80,6 +80,15 @@ describe('JSON Schema format', () => { expect(jsonSchemaLoose({ not: {} }, null)).toBe(true); }); + it('recognizes by the presence of valid "enum"', () => { + expect(jsonSchemaLoose({ enum: ['1'] }, null)).toBe(true); + expect(jsonSchemaLoose({ enum: [] }, null)).toBe(true); + + expect(jsonSchemaLoose({ enum: 2 }, null)).toBe(false); + expect(jsonSchemaLoose({ enum: {} }, null)).toBe(false); + expect(jsonSchemaLoose({ enum: null }, null)).toBe(false); + }); + describe('mixed', () => { it('invalid type but valid combiner', () => { expect(jsonSchemaLoose({ type: 'foo', allOf: [] }, null)).toBe(true); diff --git a/packages/formats/src/jsonSchema.ts b/packages/formats/src/jsonSchema.ts index 1cd63335c..7c35401ee 100644 --- a/packages/formats/src/jsonSchema.ts +++ b/packages/formats/src/jsonSchema.ts @@ -17,6 +17,8 @@ const hasValidJSONSchemaType = (document: Partial<{ type?: unknown }>): boolean return Array.isArray(document.type) && document.type.every(type => KNOWN_JSON_SCHEMA_TYPES.includes(type)); }; +const hasValidJSONSchemaEnumKeyword = (document: Record): boolean => Array.isArray(document['enum']); + const hasValidJSONSchemaCompoundKeyword = (document: Record): boolean => KNOWN_JSON_SCHEMA_COMPOUND_KEYWORDS.some( combiner => combiner in document && typeof document[combiner] === 'object' && document[combiner] !== null, @@ -43,7 +45,10 @@ export const jsonSchemaLoose: Format> = ( document: unknown, ): document is Record => isPlainObject(document) && - (isJsonSchema(document) || hasValidJSONSchemaType(document) || hasValidJSONSchemaCompoundKeyword(document)); + (isJsonSchema(document) || + hasValidJSONSchemaType(document) || + hasValidJSONSchemaEnumKeyword(document) || + hasValidJSONSchemaCompoundKeyword(document)); jsonSchemaLoose.displayName = 'JSON Schema (loose)';