Icontem

File: test/rules/string.js

Recommend this page to a friend!
  Classes of Harcharan Singh  >  Node Input Validator  >  test/rules/string.js  >  Download  
File: test/rules/string.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: fixed #11
Date: 4 months ago
Size: 1,797 bytes
 

Contents

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

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

describe('empty string', function () {

    it('should ignore empty string in not required fields', async () => {

        let v = new Validator({ field: 'test' }, { field: 'string' });

        let matched = await v.check();
        assert.equal(matched, true);

    });

    it('should reject integer in string fields', async () => {

        let v = new Validator({ field: 1 }, { field: 'string' });

        let matched = await v.check();
        assert.equal(matched, false);

    });

    it('should reject boolean in string fields', async () => {

        let v = new Validator({ field: true }, { field: 'string' });

        let matched = await v.check();
        assert.equal(matched, false);

    });

    it('should reject boolean false in string fields', async () => {

        let v = new Validator({ field: false }, { field: 'string' });

        let matched = await v.check();
        assert.equal(matched, false);

    });

    it('should reject array in string fields', async () => {

        let v = new Validator({ field: [1, 2] }, { field: 'string' });

        let matched = await v.check();
        assert.equal(matched, false);

    });

    it('should reject object in string fields', async () => {

        let v = new Validator({ field: {} }, { field: 'string' });

        let matched = await v.check();
        assert.equal(matched, false);
        assert.equal(v.errors.field.message, v.parseExistingMessageOnly('string', 'field'));

    });

    it('should reject empty string in required fields', async () => {

        let v = new Validator({ field: '' }, { field: 'required|string' });

        let matched = await v.check();
        assert.equal(matched, false);

       

    });

});