Icontem

File: test/rules/between.js

Recommend this page to a friend!
  Classes of Harcharan Singh  >  Node Input Validator  >  test/rules/between.js  >  Download  
File: test/rules/between.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: test upgrades
test cases: later rewrite message verification
Date: 4 months ago
Size: 2,315 bytes
 

Contents

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

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


let r = {};


describe('between: with integer', function () {

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

        const v = new Validator(
            { age: '19' },
            { age: 'between:16,21' }
        );

        const matched = await v.check();

        assert.equal(matched, true);

    });

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

        const v = new Validator(
            { age: '29' },
            { age: 'between:16,21' }
        );

        const matched = await v.check();

        assert.equal(matched, false);

        //console.log(v.errors);

    });

});

describe('between: with float', function () {

    it('validation should pass: with float inputs and integer seeds', async () => {

        const v = new Validator(
            { price: '19.99' },
            { price: 'between:19,20' }
        );

        const matched = await v.check();

        assert.equal(matched, true);

    });

    it('validation should pass: with float inputs and mixed seeds', async () => {

        const v = new Validator(
            { price: '19.98' },
            { price: 'between:19,19.98' }
        );

        const matched = await v.check();

        assert.equal(matched, true);

    });

    it('validation should fail: with float inputs and mixed seeds', async () => {

        const v = new Validator(
            { price: '19.99' },
            { price: 'between:19,19.89' }
        );

        const matched = await v.check();

        assert.equal(matched, false);

        //console.log(v.errors);

    });

});

describe('between: with array', function () {

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

        const v = new Validator(
            { features: [1, 2, 3] },
            { features: 'between:3,5' }
        );

        const matched = await v.check();

        assert.equal(matched, true);

    });

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

        const v = new Validator(
            { features: [1, 2] },
            { features: 'between:3,6' }
        );

        const matched = await v.check();

        assert.equal(matched, false);

        assert.equal(v.errors.features.message, v.parseExistingMessageOnly('between', 'features', [1, 2], [3, 6]));
    });

});