npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-model-validators

v1.2.0

Published

_this module helps build validation rules for models in typescript projects_.

Downloads

44

Readme

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

| Decorator | Parameter | Data type | | ------------- |:-------------|:-----:| | contains | value that you want to check, error template | array | | existsIn | array input, error template | any | | notIn | array input, error template | any | | intersect | array input, error template | array |

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