Icontem

File: test/rules/arrayUniqueObjects.js

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

Contents

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

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


describe('arrayUniqueObjects', () => {
  it('should fail with string', async () => {
    const v = new Validator(
      { features: 'test' },
      { features: 'arrayUniqueObjects:id' },
    );

    const matched = await v.check();

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


  it('should pass with array of objects', async () => {
    const v = new Validator(
      {
        features: [{
          id: 1,
          name: 'ok',
        },
        {
          id: 2,
          name: 'ok2',
        }],
      },
      { features: 'array|arrayUniqueObjects:id' },
    );

    const matched = await v.check();

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

  it('should fail for duplicate ids in array of objects', async () => {
    const v = new Validator(
      {
        features: [{
          id: 1,
          name: 'ok',
        },
        {
          id: 1,
          name: 'ok2',
        }],
      },
      { features: 'array|arrayUniqueObjects:id' },
    );
    const matched = await v.check();

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

  it('should pass for multiple individual duplicates and unique when grouped', async () => {
    const v = new Validator(
      {
        features: [{
          id: 1,
          name: 'ok',
        },
        {
          id: 1,
          name: 'ok1',
        },
        {
          id: 3,
          name: 'ok2',
        }],
      },
      { features: 'array|arrayUniqueObjects:id,name' },
    );
    const matched = await v.check();

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

  it('message should exist', async () => {
    const v = new Validator(
      { features: {} },
      { features: 'arrayUniqueObjects:id' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
    assert.equal(
      v.errors.features.message,
      v.getExistinParsedMessage({
        rule: 'arrayUniqueObjects',
        value: {},
        attr: 'features',
        args: ['id'],
      }),
    );
  });
});