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: nxt
Date: 5 months ago
Size: 1,879 bytes
 

Contents

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


const { Validator } = require('../../lib/index');

describe('requiredNotIf', () => {
  it('should pass', async () => {
    const v = new Validator(
      { name: 'Harcharan Singh', age: '16' },
      { sex: 'requiredNotIf:age,16' },
    );

    const matched = await v.check();

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


  it('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('should pass with multiple seeds', 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('should fail with multiple seeds', 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);
  });

  it('message should exist', 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);

    assert.equal(
      v.errors.sex.message,
      v.getExistinParsedMessage({
        rule: 'requiredNotIf',
        value: '',
        attr: 'sex',
        args: ['age', 16],
      }),
    );
  });
});