Skip to content

Commit

Permalink
feat(repository-json-schema): refactor metaToJsonProperty to accept c…
Browse files Browse the repository at this point in the history
…ustom 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.
  • Loading branch information
iqbaldjulfri authored and bajtos committed Apr 8, 2019
1 parent e1f7ef6 commit d0014c6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
});
});
4 changes: 4 additions & 0 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export function metaToJsonProperty(meta: PropertyDefinition): JSONSchema {
});
}

if (meta.jsonSchema) {
Object.assign(propDef, meta.jsonSchema);
}

return result;
}

Expand Down
1 change: 1 addition & 0 deletions packages/repository/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d0014c6

Please sign in to comment.