Icontem

File: test/multiLevelAttributes.js

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

Contents

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

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

let r = {};

describe('Array Fields', function () {

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

        let v = new Validator(
            {
                plan: [
                    { price: '25', title: 'OK' },
                    { price: '30' },
                ]
            },
            {
                'plan.*.price': 'required|integer'
            });

        let matched = await v.check();


        assert.equal(matched, true);

    });

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

        let v = new Validator(
            {
                plan: [15, 30, 40]
            },
            {
                'plan.*': 'required|integer'
            });

        let matched = await v.check();


        assert.equal(matched, true);

    });

    it('should fail of rule integer', async () => {

        let v = new Validator(
            {
                plan: ['ghj', 'ji', 'lp']
            },
            {
                'plan.*': 'required|integer'
            });

        let matched = await v.check();


        assert.equal(matched, false);

    });



    it('should fail of index 1 price|title and index 3 price required', async () => {

        let v = new Validator(
            {
                plan: [
                    { price: '25', title: 'OK' },
                    { price: '', title: '' },
                    { price: '30' },
                    { price: '', title: 'Title' }
                ]
            },
            {
                'plan.*.price': 'required|integer',
                'plan.*.title': 'required'
            });

        let matched = await v.check();

        v.errors.should.have.keys('plan.1.price', 'plan.1.title', 'plan.2.title', 'plan.3.price');

        assert.equal(matched, false);

    });



});