Git Product home page Git Product logo

validatorts's Introduction

ValidatorTS

A TypeScript validator implementation inspired by .NET data annotations like [Required], [Range] etc.

Example

1. Property decorators

Code:

import {
    required,
    range,
    length,
    validate
} from "./validator";

class Human {
    @required("sex is required.")
    sex: string;
}

class Person extends Human {
    @required("name is required.")
    name: string;
    @range(1, 10, "age must be in the range [1, 10].")
    age: number;
    @required("data is required")
    @length(10, 10, "data must be of length 10.")
    data;
}

let person = new Person();
console.log(validate(person));

Result:

[ 'name is required.',
  'age must be in the range [1, 10].',
  'data must be of length 10.',
  'data is required',
  'sex is required.' ]

2. Method decorator

Code:

class Person extends Human {
    @required("name is required.")
    name: string;
    @range(1, 10, "age must be in the range [1, 10].")
    age: number;
    @required("data is required")
    @length(10, 10, "data must be of length 10.")
    data;

    @validated
    test(p: Person) {
        let modelState = arguments[arguments.length - 1];
        console.log(modelState);
        return;
    }
}

Result:

When @validated is used on a method, an object containing the validation results for each of the method arguments will be passed as the last argument. It can be retrieved either by explicitly adding a parameter to the method's signature or by indexing the arguments array. The indices in the modelState array correspond to the indices of the method's parameters - modelState[0] cotnains the validation errors for parameter p of the method test in the previous example.

[ [ { property: 'name', message: 'name is required.' },
    { property: 'age',
      message: 'age must be in the range [1, 10].' },
    { property: 'data', message: 'data must be of length 10.' },
    { property: 'data', message: 'data is required' },
    { property: 'sex', message: 'sex is required.' } ] ]

validatorts's People

Contributors

dboikliev avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.