Icontem

File: test/rules/requiredNotIf.js

Recommend this page to a friend!
  Classes of Harcharan Singh  >  Node Input Validator  >  test/rules/requiredNotIf.js  >  Download  
File: test/rules/requiredNotIf.js
Role: Example script
Content type: text/plain
Description: Example script
Class: Node Input Validator
Validate submitted input values in Node.js
Author: By
Last change: Add support of dot parameters in requiredIf

Added the support of subparameters in the conditions rules of requiredIf, requiredWith, requiredNotWith
Date: 2 months ago
Size: 2,613 bytes
 

Contents

Class file image Download
const assert = require('assert');

const Validator = require('../../index');

describe('#requiredNotIf', function () {

    it('validation should pass', async () => {

        let v, matched;

        v = new Validator(
            { name: 'Harcharan Singh', age: '16' },
            { sex: 'requiredNotIf:age,16' });

        matched = await v.check();

        assert.equal(matched, true);

    });
    it('validation should pass', async () => {

        let v, matched;

        v = new Validator(
            { name: 'Harcharan Singh', address:{street:"fantastic"} },
            { age: 'requiredNotIf:address.street,fantastic' });

        matched = await v.check();

        assert.equal(matched, true);

    });

    it('validation should fail', async () => {

        const v = new Validator(
            { name: 'Harcharan Singh', age: 15, sex: 'male' },
            { sex: 'requiredNotIf:age,16' });

        const matched = await v.check();

        assert.equal(matched, false);
    });
    it('validation should fail', async () => {

        const v = new Validator(
            { name: 'Harcharan Singh', address:{street:"fantastic"}, age: 15,},
            { age: 'requiredNotIf:address.street,fantastic' });

        const matched = await v.check();

        assert.equal(matched, false);
    });

    // should(v.errors).be.an.instanceOf(Object);
    // should(v.errors).have.property('sex');

    it('validation should pass', async () => {
        const v = new Validator(
            {
                name: 'Harcharan Singh',
                age: 16,
                parent: 'yes',
                type: 'subscribed',
                //email: 'artisangang@gmail.com'
            },
            {
                email: 'requiredNotIf:age,16,parent,yes,type,subscribed'
            }
        );

        const matched = await v.check();

        assert.equal(matched, true);
    });

    it('validation should fail', async () => {

        const v = new Validator(
            {
                name: 'Harcharan Singh',
                age: 16,
                parent: 'yes',
                type: 'subscribed',
                email: 'artisangang@gmail.com'
            },
            {
                email: 'requiredNotIf:age,16,parent,yes,type,subscribed'
            }
        );

        const matched = await v.check();

        assert.equal(matched, false);
        assert.equal(v.errors.email.message, v.parseExistingMessageOnly('requiredNotIf', 'email', '',4));

        // should(v.errors).be.an.instanceOf(Object);
        // should(v.errors).have.property('email');

    });

});