Skip to content

Easily create functions to validate your js objects in a functional programming way, using Immutability, currying, ...etc

License

Notifications You must be signed in to change notification settings

polutz/ptz-validations

Repository files navigation

ptz-validations

Codacy Badge Build Status NPM codecov.io Dependency Status bitHound Score MIT license

Translations

pt-br en-us

Validate your js objects.

Install

    npm install --save ptz-validations

How to use

Simple example

    import * as V from 'ptz-validations';

    const validateLogin = V.validate({
        userName: [
            V.required,
            V.isString,
            V.min(2),
            V.max(40),
            V.toLowerCase
        ],
        password: [
            V.required,
            V.isString,
            V.min(6),
            V.max(40)
        ]
    });

    const login = validateLogin({
        userName: '',
        password: 'abcd'
    });

    /* login Output with errors:
    {
        userName: '',
        password: 'abcd',
        errors: [{
            propName: 'userName',
            errorMsg: 'ERROR_REQUIRED'
        }, {
            propName: 'password',
            errorMsg: 'ERROR_MIN'
        }]
    }
    */

Complex example

Example test of how to create a function to validate user.

assert, describe, it are used to test this code, this is a copy paste of a real code.

    import * as assert from 'ptz-assert';
    import * as V from 'ptz-validations';

    describe('ptz-validations', () => {
        it('createUser example', () => {

            // Optional interface for Typescript projects
            interface IUser {
                id: string;
                userName: string;
                password: string;
                email: string;
                weight?: number;
                birthday?: Date;
            }

            const validateUser = V.validate<IUser>({
                id: [
                    V.generateId
                ],
                displayName: [
                    V.required,
                    V.isString,
                    V.min(2),
                    V.max(100)
                ],
                userName: [
                    V.required,
                    V.isString,
                    V.min(2),
                    V.max(40),
                    V.toLowerCase
                ],
                password: [
                    V.required,
                    V.isString,
                    V.min(6),
                    V.max(40)
                ],
                email: [
                    V.required,
                    V.isEmail
                ],
                weight: [
                    V.isNumber,
                    V.min(1),
                    V.max(1000)
                ],
                birthday: [
                    V.isDate,
                    V.min(new Date('1800-01-01')),
                    V.max(new Date())
                ]
            });

            const user = validateUser({
                userName: 'angeloocana',
                password: 'abcd',
                email: 'angeloocana@gmail.com',
                weight: 90,
                birthday: '1992-06-28'
            });

            const expectedUser = {
                id: 'hfk397d',
                userName: 'angeloocana',
                password: 'abcd',
                email: 'angeloocana@gmail.com',
                weight: 90,
                birthday: new Date('1992-06-28'),
                errors: [{
                    propName: 'displayName',
                    errorMsg: 'ERROR_REQUIRED'
                }, {
                    propName: 'password',
                    errorMsg: 'ERROR_MIN'
                }]
            };

            assert.ok(user.id, 'generate id');
            assert.equal(user.userName, expectedUser.userName, 'set userName');
            assert.equal(user.password, expectedUser.password, 'set password');
            assert.equal(user.email, expectedUser.email, 'set email');
            assert.equal(user.weight, expectedUser.weight, 'set weight');
            assert.equal(user.birthday.toString(), expectedUser.birthday.toString(), 'set birthday');
            assert.deepEqual(user.errors, expectedUser.errors, 'add errors');
        });
    });

Create your own custom validation function

Create a curried function using ramda. This function must receive:

  • errorMsg: optional, it is good for enabling custom error messages;
  • propName: will be the property name of the object you are validating;
  • obj: the instance of the object to be validated.
    import R from 'ramda';
    import * as V from 'ptz-validations';

    /**
    * Checks if an object prop is even:
    *   - true: return the same object
    *   - false: return a new object with errorMsg
    */
    export const isEvenWithError = R.curry((errorMsg, propName, obj) => {

        const propValue = R.prop(propName, obj);

        return R.isEven(propValue)
            ? obj
            : V.addError(obj, propName, errorMsg);
    });

    export const isEven = isEvenWithError('DEFAULT_EVEN_ERROR_MSG');

Contribute

NPM Global packages

    npm install -g ts-node babel-cli

Setup

    npm install   

Test

    npm test

About

Easily create functions to validate your js objects in a functional programming way, using Immutability, currying, ...etc

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published