Icontem

File: test/rules/requiredIf.js

Recommend this page to a friend!
  Classes of Harcharan Singh  >  Node Input Validator  >  test/rules/requiredIf.js  >  Download  
File: test/rules/requiredIf.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: 9 months ago
Size: 4,074 bytes
 

Contents

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


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

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

    const matched = await v.check();

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

  it('should pass with false as string', async () => {
    const v = new Validator({ remember: 'false', age: 16 }, { remember: 'requiredIf:age,16' });

    const matched = await v.check();

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

  it('should pass with false as boolean', async () => {
    const v = new Validator({ remember: false, age: 16 }, { remember: 'requiredIf:age,16' });

    const matched = await v.check();

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

  it('should pass with 0 as integer', async () => {
    const v = new Validator({ remember: 0, age: 16 }, { remember: 'requiredIf:age,16' });

    const matched = await v.check();

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

  it('should pass with boolean true', async () => {
    const v = new Validator({ remember: true, age: 16 }, { remember: 'requiredIf:age,16' });

    const matched = await v.check();

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

  it('should throw exceptin for invalid seed length', async () => {
    try {
      const v = new Validator({ age: 16 }, { remember: 'requiredIf:age' });

      await v.check();
      throw new Error('Invalid seed.');
    } catch (e) {
      assert.equal(e, 'Error: Invalid arguments supplied for field remember in requiredIf rule.');
    }
  });

  it('should throw exceptin for invalid seed length', async () => {
    try {
      const v = new Validator({ age: 16 }, { remember: 'requiredIf:age,8,ok' });

      await v.check();
      throw new Error('Invalid seed.');
    } catch (e) {
      assert.equal(e, 'Error: Invalid arguments supplied for field remember in requiredIf rule.');
    }
  });

  it('should throw exception for missing seed length', async () => {
    try {
      const v = new Validator({ age: 16 }, { remember: 'requiredIf' });

      await v.check();
      throw new Error('Invalid seed.');
    } catch (e) {
      assert.equal(e, 'Error: Invalid arguments supplied for field remember in requiredIf rule.');
    }
  });

  it('should fails with missing attribute of seed', async () => {
    const v = new Validator({ age: 16 }, { remember: 'requiredIf:age,16' });

    const matched = await v.check();

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

  it('should fails', async () => {
    let v = new Validator({ name: 'Harcharan Singh', age: 16 }, { sex: 'requiredIf:age,16' });

    let matched = await v.check();

    assert.equal(matched, false);

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

    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: 'requiredIf: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: 'requiredIf: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: 16 }, { sex: 'requiredIf:age,16' });

    const matched = await v.check();

    assert.equal(matched, false);

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