Git Product home page Git Product logo

sontaran's Introduction

Sontaran

Build Status Coverage Status

Sontaran is a javascript validator library. It has a lot validation options out of the box and all validators are extendable with custom validation functions.

Some key features

  • Completely written in Typescript
  • CommonJS build for Node JS
  • Tree shakable ES build
  • Fluid, chainable api
  • Support for async validators with the validateAsync method

Installation

Sontaran can be installed using npm.

npm install --save sontaran

For the complete code including all tests the repo can be cloned.

git clone https://github.com/Barry127/sontaran.git
cd sontaran
npm run test

Getting Started

The object().schema() function takes a schema of Sontaran validators as an argument.

In this example:

username

  • Must be a string
  • Cannot be only empty characters (spaces, tabs, return, ...)
  • Must have a length between 3 and 10 characters
  • Must match the given RegExp (only contain alphanumeric characters and dash, underscore)

email

  • Must be a valid email

password

  • Must be a string
  • Must have a length of at least 8 characters
import { object, string, email } from 'sontaran';

const schema = object().schema({
  username: string()
    .notEmpty()
    .between(3, 10)
    .match(/^[a-zA-Z0-9_\-]*$/),
  email: email(),
  password: string().min(8)
});

// Valid schema (return true)
schema.validate({
  username: 'sontaran',
  email: '[email protected]',
  password: 'mySuperSecretPassword'
}); /** => {
  valid: true,
  value: {
    username: 'sontaran',
    email: '[email protected]',
    password: 'mySuperSecretPassword
  }
}*/

// invalid usernames
let a = 123; // => not a string
let b = ' \t\r'; // => Empty characters
let c = 'aa'; // => too short
let d = 'Hello-World'; // => too long
let e = 'B@dInput'; // => invalid character

Sontarans custom can take any ValidatorFunction function as argument to validate and / or transform a value.

import { string, ValidationError } from 'sontaran';

// value cannot be root and is transformed to lowercase
const myCustomValidator = (value) => {
  if (value.toLocaleLowercase() === 'root') {
    throw new ValidationError('root is not allowed');
  }

  return value.toLocaleLowercase();
};

const schema = string().custom(myCustomValidator);

// valid result transformed to lowercase
const result = schema.label('username').validate('Admin');
/* => {
  valid: true,
  value: 'admin'
}*/

// invalid result
const result = schema.label('username').validate('Root');
/* => {
  valid: false,
  value: 'Root',
  errors: [{
    field: 'username',
    message: 'root is not allowed',
    type: 'root is not allowed'
  }]
}
*/

sontaran's People

Contributors

barry127 avatar dependabot[bot] avatar

Watchers

 avatar  avatar

Forkers

abbyarmada

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.