diff --git a/packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts b/packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts index ae24f4a0a0a9..a61230f0b56d 100644 --- a/packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts +++ b/packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts @@ -415,6 +415,45 @@ describe('build-schema', () => { expectValidJsonSchema(jsonSchema); }); + it('properly handles AJV keywords in property decorator', () => { + @model() + class TestModel { + @property({ + type: 'string', + required: true, + jsonSchema: { + format: 'email', + maxLength: 50, + minLength: 5, + }, + }) + email: string; + + @property({ + type: 'string', + required: true, + jsonSchema: { + pattern: '(a|b|c)', + }, + }) + type: string; + } + + const jsonSchema = modelToJsonSchema(TestModel); + expect(jsonSchema.properties).to.eql({ + email: { + type: 'string', + format: 'email', + maxLength: 50, + minLength: 5, + }, + type: { + type: 'string', + pattern: '(a|b|c)', + }, + }); + }); + it('properly converts decorated custom array type with a resolver', () => { @model() class Address { diff --git a/packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts b/packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts index 1d4598800c81..dc1daa1310ab 100644 --- a/packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts +++ b/packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts @@ -169,5 +169,25 @@ describe('build-schema', () => { description: 'test', }); }); + + it('keeps AJV keywords', () => { + const schema = metaToJsonProperty({ + type: String, + jsonSchema: { + pattern: '(a|b|c)', + format: 'email', + maxLength: 50, + minLength: 5, + }, + }); + + expect(schema).to.eql({ + type: 'string', + pattern: '(a|b|c)', + format: 'email', + maxLength: 50, + minLength: 5, + }); + }); }); }); diff --git a/packages/repository-json-schema/src/build-schema.ts b/packages/repository-json-schema/src/build-schema.ts index 9824ea5b27a2..647a100cf6e8 100644 --- a/packages/repository-json-schema/src/build-schema.ts +++ b/packages/repository-json-schema/src/build-schema.ts @@ -127,6 +127,10 @@ export function metaToJsonProperty(meta: PropertyDefinition): JSONSchema { }); } + if (meta.jsonSchema) { + Object.assign(propDef, meta.jsonSchema); + } + return result; } diff --git a/packages/repository/src/model.ts b/packages/repository/src/model.ts index 8586f9375644..006749ee5c28 100644 --- a/packages/repository/src/model.ts +++ b/packages/repository/src/model.ts @@ -30,6 +30,7 @@ export interface PropertyDefinition { type: PropertyType; // For example, 'string', String, or {} id?: boolean | number; json?: PropertyForm; + jsonSchema?: {[attribute: string]: any}; store?: PropertyForm; itemType?: PropertyType; // type of array [attribute: string]: any; // Other attributes