From 3e4bd022896e6baf76629cc38af9c833f6feb237 Mon Sep 17 00:00:00 2001 From: Iqbal Djulfri Date: Wed, 3 Apr 2019 23:46:38 +0700 Subject: [PATCH] feat(repository-json-schema): refactor metaToJsonProperty to accept custom jsonSchema currently there is no way to use AJV's validation keywords. This commit will make metaToJsonProperty reads the jsonSchema and add it as an additional schema to be used by AJV validation on OpenAPI layer. --- .../integration/build-schema.integration.ts | 39 +++++++++++++++++++ .../src/__tests__/unit/build-schema.unit.ts | 20 ++++++++++ .../src/build-schema.ts | 4 ++ packages/repository/src/model.ts | 1 + 4 files changed, 64 insertions(+) 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