Git Product home page Git Product logo

fluentvalidation-ts's Introduction

fluentvalidation-ts

Strong, simple, extensible validation.

Visit https://fluentvalidation-ts.alexpotter.dev to get started.

Coverage NPM Version License Last Commit Open Issues

Dependencies GZIP Size

Overview

Front-end validation is a must-have for any project that involves forms, but the requirements vary hugely. You might have a simple sign-up form with a few text fields, or a complex configuration page with collections and deeply nested fields.

There are plenty of libraries out there which help you to solve the problem of front-end validation, but all the ones I've tried have felt lacking in one aspect or another - whether that's TypeScript support, their capacity to handle complex requirements, or the ability to define your own reusable validation logic.

So I wrote fluentvalidation-ts, a tiny library that is:

  • Designed for TypeScript
  • Simple yet powerful
  • Fully extensible

Whatever your validation needs, fluentvalidation-ts can handle them.

Docs

Full documentation, including a tutorial and a number of useful guides, is available at the official fluentvalidation-ts website.

Requirements

This library has been written in, and for, TypeScript. You can still use fluentvalidation-ts without TypeScript, but the primary benefit of having strongly-typed validation rules is lost.

If using TypeScript (strongly recommended), you must be on TypeScript version 2.9 or later.

Installation

Using Yarn:

yarn add fluentvalidation-ts

Using NPM:

npm i --save fluentvalidation-ts

Note: fluentvalidation-ts has been written with first-class support for TypeScript, so there's no need to install types!

Example Usage

import { Validator } from 'fluentvalidation-ts';

type Person = {
  name: string;
  age: number;
};

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

    this.ruleFor('name') // This is type-safe! (Argument is of type 'name' | 'age')
      .notEmpty()
      .withMessage('Please enter your name');

    this.ruleFor('age')
      .greaterThanOrEqualTo(0)
      .withMessage('Age cannot be negative');
  }
}

const validator = new PersonValidator();

validator.validate({ name: '', age: 25 });
// { name: 'Please enter your name' }

validator.validate({ name: 'Alex', age: -1 });
// { age: 'Age cannot be negative' }

validator.validate({ name: '', age: -1 });
// { name: 'Please enter your name', age: 'Age cannot be negative' }

Test Coverage

fluentvalidation-ts has 100% test coverage via unit tests written with Jest.

Note: Some branches are incorrectly reported as uncovered due to the following issue: gotwarlost/istanbul#690.

Issues

Please report issues via GitHub.

License

fluentvalidation-ts is provided under the terms of an Apache-2.0 license.

Development

To run the project locally simply clone the repo and run yarn install then yarn start in the root of the project. You can run the tests with yarn test.

About the Author

Alex Potter is a full-stack Software Developer who is currently part of the team at Ghyston, an award-winning software development company based in Bristol.

Check out his blog at alexpotter.dev, or get in touch @ajp_dev on Twitter.

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.