Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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