File: test/objects.js

Recommend this page to a friend!
  Classes of Harcharan Singh   Node Input Validator   test/objects.js   Download  
File: test/objects.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: release v4.4
Date: 2 years ago
Size: 3,964 bytes
 

Contents

Class file image Download
const assert = require('assert'); const { Validator } = require('../lib/index'); describe('Objects', () => { describe('Single Level', () => { it('should pass with top level required', async () => { const v = new Validator( { product: { id: '1', name: 'Product', price: '12.50', active: 'yes', }, }, { product: 'required|object', 'product.id': 'integer', 'product.name': 'string', 'product.price': 'numeric', 'product.active': 'string', }, { product: 'The given product is invalid :value.', }, ); const matched = await v.check(); assert.equal(matched, true); }); it('should fail with child level required', async () => { const v = new Validator( { product: { id: 1, name: '', price: '', active: 'yes', }, }, { product: 'required|object', 'product.id': 'required|integer', 'product.name': 'required|string', 'product.price': 'required|numeric', 'product.active': 'required|string', }, { product: 'The given product is invalid :value.', }, ); const matched = await v.check(); assert.equal(matched, false); v.errors.should.have.keys('product.name', 'product.price'); }); }); describe('Deep Level', () => { it('should pass with top level required', async () => { const v = new Validator( { product: { id: '1', name: 'Product', price: '12.50', weight: { unit: 'gram', value: 100 }, }, }, { product: 'required|object', 'product.id': 'integer', 'product.name': 'string', 'product.price': 'numeric', 'product.weight': 'object', 'product.weight.unit': 'in:gram', 'product.weight.value': 'numeric', }, { product: 'The given product is invalid :value.', }, ); const matched = await v.check(); assert.equal(matched, true); }); it('should fail with child level required', async () => { const v = new Validator( { product: { id: '1', name: '', price: '12.50', weight: { unit: 'kilo', value: '' }, }, }, { product: 'required|object', 'product.id': 'required|integer', 'product.name': 'required|string', 'product.price': 'required|numeric', 'product.weight': 'required|object', 'product.weight.unit': 'required|in:gram', 'product.weight.value': 'required|numeric', }, { product: 'The given product is invalid :value.', }, ); const matched = await v.check(); assert.equal(matched, false); v.errors.should.have.keys('product.name', 'product.weight.unit', 'product.weight.value'); }); it('should fail with child level missing', async () => { const v = new Validator( { product: { id: '1', name: '', price: '12.50', }, }, { product: 'required|object', 'product.id': 'required|integer', 'product.name': 'required|string', 'product.price': 'required|numeric', 'product.weight': 'required|object', 'product.weight.unit': 'required|in:gram', 'product.weight.value': 'required|numeric', }, { product: 'The given product is invalid :value.', }, ); const matched = await v.check(); assert.equal(matched, false); v.errors.should.have.keys('product.name', 'product.weight', 'product.weight.unit', 'product.weight.value'); }); }); });