1.2.0 • Published 3 years ago

ts-model-validators v1.2.0

Weekly downloads
39
License
ISC
Repository
github
Last release
3 years ago

Typescript(ts) Model Validator

this module helps build validation rules for models in typescript projects.

CircleCI

Build Status Dependency Status devDependency Status

Installation & Usage

Install module:

npm i --save ts-model-validators

or

npm i --save @dodo-micro/ts-model-validators

Create your class and put some validation decorators on its properties you want to validate:

// some imports 
// ...
export class Education {

    @min(12, 'participation count must be equal or greater than {0}')
    public participationCount = 0;

    @min(10, 'custom msg without arg')
    public customField = 0;
}

Execute the validation and get error messages:

// some imports 
// ...
const education = new Education();
const validation: ValidationMessage[] = validatorService.validate(education);

/**
 * the structure of a validation Message:
    class ValidationMessage {
        name: string = "";
        msgs: string[] = [];
    }
 * /

Built-in rules

With error message template, {i} will be replaced by i-th passed parameter. | Decorator | Parameter | Data type | | ------------- |:-------------|:-----:| | required | error template | any | | requiredIf | the property name that if it has value current field is required. error template | any | | afterOrEqualTo | the other field's name to compare. error template | string | | beforeOrEqualTo | the other field's name to compare. error template | string | | dateFormat | error template | string | | min | min value. error template | number | | max | max value. error template | number | | minLength | min value . error template | string | | maxLength | max value . error template | string | | email | error template | string |

Update version 1.2.x

DecoratorParameterData type
containsvalue that you want to check, error templatearray
existsInarray input, error templateany
notInarray input, error templateany
intersectarray input, error templatearray

Create custom a validation rule

Beside built-in rules, we can easily create custom validation rule as a Decorator with ValidatorFactory:

// some imports 
// ...
export function min(minValue: number, errorTemplate: string) {
    const rule = new Rule<number>(errorTemplate, [minValue]);

    // the method check the validity of data, the second argument of this method can be the target object (check the rule beforeOrEqualTo)
    rule.isValid = function (input: number): boolean {
        return input >= this.msgArgs[0];
    }

    return validatorFactory.build(rule);
}


export function beforeOrEqualTo(anotherKey: string, errorTemplate: string) {
    const rule = new Rule<string>(errorTemplate, [anotherKey]);
    rule.isValid = function (input: string, entity: any): boolean {
        const anotherField = entity[anotherKey];
        const target = input;
        
        // ... 
        // Your implementations...
        // ...
        //   
    }

    return validatorFactory.build(rule);
}

Update version 1.2.x

Introduce new class CreateValidatorFactory to reduce complexity of decorator factories, usage:

// some imports 
// ...

export function contains(element: string, errorTemplate: string) {
    return new CreateValidatorFactory<Array<string>>(errorTemplate)
        .arguments([element])
        .validateFunction((input) => input.includes(element))
        .build();
}

Todos

  • improve usability of module
1.2.0

3 years ago

1.1.7

3 years ago

1.1.1

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago