Coder Social home page Coder Social logo

lakmus's Introduction

Lakmus

Fluent validation in TypeScript.

Features

  • 20+ built-in validators
  • Refactoring-friendly (generic property selectors)
  • Multilanguage

Inspired by .NET FluentValidation

Demo

export class Person {
    public id: number;
    public name: string;
    public address: Address;
    public phoneNumbers: PhoneNumber[];
    public hasDiscount: boolean;
    public discountValue: number;
}

export class Address {
    public country: string;
    public city: string;
    public street: string;
    public houseNumber: string;
}

export class PhoneNumber {
    public country: string;
    public code: string;
    public number: string;
}

export class PersonValidator extends Validator<Person> {
    constructor() {
        super();

        // not null & max length;
        this.ruleFor(x => x.name)
            .notNull()
            .maxLenght(100);

        // nested objection validation;
        this.ruleFor(x => x.address)
            .notNull()
            .setValidator(new AddressValidator());

        // collection validation;
        this.ruleFor(x => x.phoneNumbers)
            .notNull()
            .setCollectionValidator(new PhoneNumberValidator());

        this.when(x => x.hasDiscount, () => {
            this.ruleFor(x => x.discountValue)
                .notNull();
        });
    }
}

export class AddressValidator extends Validator<Address> {
    constructor() {
        super();

        this.ruleFor(x => x.city)
            .notNull().maxLenght(50);

        this.ruleFor(x => x.street)
            .notNull().maxLenght(50);
    }
}

export class PhoneNumberValidator extends Validator<PhoneNumber> {
    constructor() {
        super();

        this.ruleFor(x => x.country)
            .notNull().maxLenght(3);

        this.ruleFor(x => x.code)
            .notNull().maxLenght(5);

        // predicate and custom error message;
        this.ruleFor(x => x.number)
            .notNull().maxLenght(7)
            .must((number, phoneNumber) => (phoneNumber.code || "").length + (phoneNumber.country || "").length + (phoneNumber.number || "").length == 11)
            .withMessage("Please enter a valid phone number.");
    }
}

// set custom property name resolver: discountValue -> Discount Value
ValidatorOptions.propertyNameResolver = ApStyleTitleCasePropertyNameResolver;

// initialize object to validate;
var person = new Person();

// create validator;
var validator = new PersonValidator();
// or get it from factory (recommended) 
validator = ValidatorFactory.getValidator(PersonValidator);

// validate;
var result = validator.validate(person);

// process result;
if (result.isValid) {
    console.log("Is valid.");
}
else {
    console.log(result.errors.map(x => x.errorMessage).join("\n"));
}

License

MIT

lakmus's People

Contributors

bmitchenko avatar pgazmuri avatar romansp avatar

Watchers

 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.