Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
feat: #68 add date validate documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Nov 5, 2020
1 parent 344ba12 commit 4d01b3b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/type/decorators/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ validate.validators.bigNumber = function (value: BigNumber, options: BigNumberVa
const KEY_PROP_CONSTRAINT = 'entity:constraint_information';

export interface ConstraintOption {
presence?: { allowEmpty?: boolean, message?: string };
presence?: { allowEmpty?: boolean, message?: string } | boolean;
type?: 'array' | 'integer' | 'number' | 'string' | 'date' | 'boolean';
/**
* The inclusion validator is useful for validating input from a dropdown for example.
Expand Down
11 changes: 11 additions & 0 deletions src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export function applyValidate(entityType: Class, input: any, method: ODataMethod
return msgs;
}

/**
* generate validation from column metadata
*
* @param entityType
* @param method
*/
function createColumnValidationRules(entityType: Class, method: ODataMethod) {
const entityProps = Edm.getProperties(entityType);
const columnMetaValidationRules = entityProps.reduce((allRules, entityProp) => {
Expand All @@ -53,6 +59,11 @@ function createColumnValidationRules(entityType: Class, method: ODataMethod) {
return columnMetaValidationRules;
}

/**
* generate validation from decorators
*
* @param entityType
*/
function createCustomValidationRules(entityType: Class) {

const entityProps = Edm.getProperties(entityType);
Expand Down
55 changes: 54 additions & 1 deletion test/type/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ODataMethod } from '@odata/parser';
import BigNumber from 'bignumber.js';
import { ColumnOptions } from 'typeorm';
import * as validate from 'validate.js';
import { BaseODataModel, columnToValidateRule, EColumnOptions, KeyProperty, ODataEntityType, ODataModel, ODataNavigation, OptionalProperty, Property, UUIDKeyProperty } from '../../src';
import { BaseODataModel, columnToValidateRule, EColumnOptions, KeyProperty, ODataEntityType, ODataModel, ODataNavigation, OptionalProperty, Property, UUIDKeyProperty, Validate } from '../../src';
import { createServerAndClient, createTmpConnection, getTestCharDataType } from './utils';


Expand Down Expand Up @@ -365,4 +365,57 @@ describe('Validate Test Suite', () => {

});

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();
}


});

});

0 comments on commit 4d01b3b

Please sign in to comment.