UNPKG

violations

Version:

get violated arguments for validation and assertion

278 lines (240 loc) 7.14 kB
'use strict'; var assert = require('assert'); var Violate = require('../src').Violate; describe('new', function () { it('with empty rules holds empty rules', function () { var rules = {}; var validator = new Violate(rules); assert.strictEqual(validator.rules, rules); }); it('without rules holds empty rules', function () { var validator = new Violate(); assert.strictEqual(JSON.stringify(validator.rules), '{}'); }); }); describe('no argument', function () { var rules = {}; var validator = new Violate(rules); describe('validate()', function () { it('without values returns undefined', function () { var violations = validator.validate(); assert.strictEqual(violations, undefined); }); it('with null returns undefined', function () { var violations = validator.validate(null); assert.strictEqual(violations, undefined); }); }); describe('assert()', function () { it('without values', function () { try { validator.assert(); } catch (err) { assert.fail(true); } }); it('with null', function () { try { validator.assert(null); } catch (err) { assert.fail(true); } }); }); }); describe('with argument', function () { var rules = { a: function a(_a) { if (_a === undefined) return 'a is required'; if (_a !== 1) return 'a should be 1'; }, b: function b(_b) { if (_b === undefined) return; // b is optional if (_b !== 'b') return 'b should be "b"'; } }; var validator = new Violate(rules); describe('validate()', function () { describe('success', function () { it('valid argument', function () { var violations = validator.validate({ a: 1, b: 'b' }); assert.strictEqual(violations, undefined); }); it('over argument', function () { var violations = validator.validate({ a: 1, b: 'b', c: function c(a) { return a; } }); assert.strictEqual(violations, undefined); }); it('without not required argument', function () { var violations = validator.validate({ a: 1 }); assert.strictEqual(violations, undefined); }); }); describe('fail', function () { it('invalid single argument', function () { var violations = validator.validate({ a: 10, b: 'b' }); assert.strictEqual(violations.length, 1); assert.strictEqual(violations[0], 'a should be 1'); }); it('invalid multi arguments', function () { var violations = validator.validate({ a: 10, b: 'c' }); assert.strictEqual(violations[0], 'a should be 1'); assert.strictEqual(violations[1], 'b should be "b"'); }); it('invalid without required', function () { var violations = validator.validate({ b: 'b' }); assert.strictEqual(violations.length, 1); assert.strictEqual(violations[0], 'a is required'); }); }); }); describe('assert()', function () { describe('success', function () { it('valid argument', function () { try { validator.validate({ a: 1, b: 'b' }); } catch (err) { assert.fail(err); } }); it('over argument', function () { try { validator.validate({ a: 1, b: 'b', c: function c(a) { return a; } }); } catch (err) { assert.fail(err); } }); it('without not required argument', function () { try { validator.validate({ a: 1 }); } catch (err) { assert.fail(err); } }); }); describe('fail', function () { it('invalid single argument', function () { try { validator.assert({ a: 10, b: 'b' }); } catch (err) { var violations = JSON.parse(err.message); assert.strictEqual(violations.length, 1); assert.strictEqual(violations[0], 'a should be 1'); } }); it('invalid multi arguments', function () { try { validator.assert({ a: 10, b: 'c' }); } catch (err) { var violations = JSON.parse(err.message); assert.strictEqual(violations.length, 2); assert.strictEqual(violations[0], 'a should be 1'); assert.strictEqual(violations[1], 'b should be "b"'); } }); it('invalid without required', function () { try { validator.assert({ b: 'b' }); } catch (err) { var violations = JSON.parse(err.message); assert.strictEqual(violations.length, 1); assert.strictEqual(violations[0], 'a is required'); } }); }); }); }); describe('multiple error', function () { describe('validate', function () { it('returns list errors', function () { var rules = { a: function a() { return ['invalid a', 'invalid b']; } }; var validator = new Violate(rules); var violations = validator.validate({ a: 1 }); assert.strictEqual(violations.length, 2); }); it('return empty errors', function () { var rules = { a: function a() { return []; } }; var validator = new Violate(rules); var violations = validator.validate({ a: 1 }); assert.strictEqual(violations, undefined); }); }); describe('assert', function () { it('returns list errors', function () { var rules = { a: function a() { return ['invalid a', 'invalid b']; } }; var validator = new Violate(rules); try { validator.assert({ a: 1 }); } catch (err) { var violations = JSON.parse(err.message); assert.strictEqual(violations.length, 2); } }); it('return empty errors', function () { var rules = { a: function a() { return []; } }; var validator = new Violate(rules); try { validator.assert({ a: 1 }); } catch (err) { assert.fail(err); } }); }); }); describe('nested object', function () { var target = { a: 'a', b: { c: 'c', d: 'd' } }; var rules = { a: function a(v) { if (v !== 'a') return 'a should "a"'; }, b: { c: function c(v) { if (v !== 'c') return 'c should "c"'; }, d: function d(v) { if (v !== 'd') return 'd should "d"'; } } }; var validator = new Violate(rules); it('valid', function () { var violations = validator.validate(target); assert.strictEqual(violations, undefined); }); it('validate fail', function () { var violations = validator.validate({ a: 0, b: { c: 0, d: 0 } }); assert.deepEqual(violations, ['a should "a"', 'c should "c"', 'd should "d"']); }); it('assert fail', function () { try { validator.assert({ a: 0, b: { c: 0, d: 0 } }); } catch (e) { assert.deepEqual(JSON.parse(e.message), ['a should "a"', 'c should "c"', 'd should "d"']); } }); });