This repository was archived by the owner on Dec 23, 2021. It is now read-only.
forked from jaystack/odata-v4-server
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Model Validation
Theo Sun edited this page Nov 5, 2020
·
3 revisions
use the Validate decorator on odata property to define the validate rule on model.
it will support a subset of validate.js rules, just check the validate.js documentation.
following validation rule will be applied in (CREATE/UPDATE) operations.
it('should support custom format validation', async () => {
@ODataModel()
class CustomValModel {
@UUIDKeyProperty() id: string;
@Validate({
format: {
pattern: /^\d{4}-\d{2}-\d{2}$/,
message: '^is not a valid date string'
},
// allow undefined value
// set true as mandatory
presence: false
})
@OptionalProperty()
value: string;
}
const conn = await createTmpConnection({
name: 'val_conn_10',
entityPrefix: 'val_test_10',
entities: [CustomValModel]
});
const { client, shutdownServer } = await createServerAndClient(conn);
try {
const es = client.getEntitySet<CustomValModel>('CustomValModels');
const d1 = '2020-11-11';
const v = await es.create({ value: d1 });
expect(v.value).toBe(d1);
const v2 = await es.create({});
expect(v2.value).toBeUndefined();
await expect(() => es.create({ value: 'djsa-ds-sd' }))
.rejects.toThrow('property \'value\' is not a valid date string');
await expect(() => es.create({ value: 'hello 2020-11-11' }))
.rejects.toThrow('property \'value\' is not a valid date string');
} finally {
await shutdownServer();
}
});