Giter VIP home page Giter VIP logo

validate's Introduction

validate โœ…

Utility to valide objects in TypeScript

Build Status Maintainability Test Coverage

Example

Validating an object

interface User {
  name: string;
  age?: number;
  isAdmin?: boolean;
  capabilities?: string[];
  attributes?: { [key: string]: string };
}

const user = {name: 'Maximus', capabilities: [], attributes: {handsome: 'very'}};

const validationRules = {
  name: [isNotEmpty, name => minLength(name, 3)],
  capabilities: [isNotEmpty],
  attributes: [isNotEmpty],
};

const results = validate<User>(user, validationRules); // {name: true, capabilities: false, attributes: true}
const isValid = isValid<User>(user, validationRules); // false

Checks if validation rules are conforming to the interface ๐Ÿ‘ฎโ€โ™‚๏ธ

const user = {name: 'Maximus'};
const validationRules = {fullName: [isNotUndefined]}

validate<User>(user, validationRules);
                     ^^^^^^^^^^^^^^^
                     'fullName' doesn't exist on type User

Available validation methods

  • isNotNull
    • Checks if the value is null
  • isNotUndefined
    • Checks if the value is undefined
  • isRequired
    • Checks if the value is truthy
  • minLength
    • Checks if the value's length is above (or equal) the specified minimum treshold.
    • Usage: {propertyName: [value => minLength(value, 5)]}
  • maxLength
    • Checks if the value's length is below (or equal) the specified maximum treshold.
    • Usage: {propertyName: [value => maxLength(value, 5)]}
  • isNotEmpty
    • Checks if the string, array, or Object.keys()'s length is truthy
  • isEnumSubset
    • Checks if the values inside an array are (or a single value is) valid values for a given Enum
    • Usage: {propertyName: [value => isValidForEnum(value, EnumType)]

Usage

  • validate<T>(unitUnderTest: T, validators: {[K in keyof Partial<T>]: Function[]}): {[K in keyof Partial<T>]: boolean}
    • Will return the same structure as Partial<T> (that are the fields to be validated), with a single boolean for each test to indicate if it is passing
  • isValid<T>(unitUnderTest: T, validators: {[K in keyof Partial<T>]: Function[]}): boolean
    • Will return a single boolean (using validate() under the hood) signaling the combined status of all tests

Custom rules ๐Ÿ’…

Providing custom validator functions are as easy as ๐Ÿ”ข

const user = {name: 'Maximus'};
const validationRules = {name: [name => name === 'Maximus', name => console.log(name)]};

const isValid = isValid<User>(user, validationRules); // true

The custom validator functions should return a boolean, to be usable for validation. If the function is void, it'll return true as in "I found this test to be passing ๐Ÿค”".

Contributions

Feel free to open a PR with a brief description about the changes.
Don't forget to cover your changes with (passing) tests!

To install, clone the repo & yarn install.
To run tests, you can just simply type yarn jest in your terminal.

validate's People

Contributors

dependabot[bot] avatar duck-nukem 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.