Comparing version 0.1.2 to 0.1.3
123
lib/index.js
@@ -5,5 +5,5 @@ 'use strict'; | ||
var _lodash = require('lodash'); | ||
var _fp = require('lodash/fp'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _fp2 = _interopRequireDefault(_fp); | ||
@@ -122,3 +122,3 @@ var _required = require('./validators/required'); | ||
/* | ||
/** | ||
* Rules should have format: | ||
@@ -143,3 +143,2 @@ * { | ||
* } | ||
* | ||
*/ | ||
@@ -227,4 +226,4 @@ | ||
this.validation = { | ||
rules: _lodash2.default.cloneDeep(validation), | ||
messages: _lodash2.default.cloneDeep(validationMessages) | ||
rules: _fp2.default.cloneDeep(validation), | ||
messages: _fp2.default.cloneDeep(validationMessages) | ||
}; | ||
@@ -234,12 +233,5 @@ } | ||
/** | ||
* @example | ||
* let ruleMapping = {name: ['required']}; | ||
* let inputObj = {name: ''}; | ||
* let validator = satpam.create(); | ||
* let result = validator.validate(ruleMapping, inputObj); | ||
* | ||
* @param ruleMapping - An mapping of input property to the available rules | ||
* e.g. {name: ['required', 'alpha']} | ||
* @param inputObj - Input object to be validated | ||
* @returns {{result: Boolean, messages: Object}} | ||
* Create a rule object based on the given rule. | ||
* @param rule {Object|String} | ||
* @returns {{name: String, fullName: String, params: Array<String>}} | ||
*/ | ||
@@ -249,4 +241,48 @@ | ||
_createClass(Validator, [{ | ||
key: '_createRuleObject', | ||
value: function _createRuleObject(rule) { | ||
var ruleObj = {}; | ||
if (_fp2.default.isString(rule)) { | ||
// First variant, everything is embedded as string | ||
var splitted = rule.split(':'); | ||
// Get only the first part of full rule e.g if range:1:3 then | ||
// we will get 'range' | ||
ruleObj.name = _fp2.default.first(splitted); | ||
// Get the rule params if e.g range:1:3 -> [1, 3] | ||
ruleObj.params = splitted.slice(1); | ||
} else { | ||
// Second variant, it is already parsed (Object) | ||
ruleObj.name = rule.name; | ||
ruleObj.params = rule.params || []; | ||
} | ||
// Property fullName is the generic full name of validation rule | ||
// e.g range:1:3 -> range:$1:$2, required -> required | ||
ruleObj.fullName = ruleObj.params.reduce(function (ruleName, val, index) { | ||
return ruleName + ':$' + (index + 1).toString(); | ||
}, ruleObj.name); | ||
return ruleObj; | ||
} | ||
/** | ||
* @example | ||
* const ruleMapping = {name: ['required']}; | ||
* const inputObj = {name: ''}; | ||
* const validator = satpam.create(); | ||
* const result = validator.validate(ruleMapping, inputObj); | ||
* | ||
* @param ruleMapping - An mapping of input property to the available rules | ||
* e.g. {name: ['required', 'alpha']} | ||
* @param inputObj - Input object to be validated | ||
* @returns {{result: Boolean, messages: Object}} | ||
*/ | ||
}, { | ||
key: 'validate', | ||
value: function validate(ruleMapping, inputObj) { | ||
var _this = this; | ||
var validator = this; | ||
@@ -257,3 +293,3 @@ var result = true; | ||
// Loop through the given rule mapping | ||
_lodash2.default.forEach(ruleMapping, function (ruleArray, propertyName) { | ||
_fp2.default.forEach(function (ruleArray, propertyName) { | ||
var val = inputObj[propertyName]; | ||
@@ -263,30 +299,3 @@ | ||
ruleArray.forEach(function (rule) { | ||
var ruleObj = {}; | ||
if (_lodash2.default.isString(rule)) { | ||
// First letiant, everything is embedded as string | ||
var splitted = rule.split(':'); | ||
ruleObj = { | ||
// Property fullName is the generic full name of validation rule | ||
// e.g range:1:3 -> range:$1:$2, required -> required | ||
fullName: _lodash2.default.first(splitted), | ||
// Get only the first part of full rule e.g if range:1:3 then | ||
// we will get 'range' | ||
name: _lodash2.default.first(splitted), | ||
// Get the rule params if e.g range:1:3 -> [1, 3] | ||
params: splitted.slice(1) | ||
}; | ||
} else { | ||
// Second letiant, it is already parsed | ||
ruleObj.name = rule.name; | ||
ruleObj.fullName = rule.name; | ||
ruleObj.params = rule.params; | ||
} | ||
ruleObj.fullName = ruleObj.params.reduce(function (ruleName, val, index) { | ||
return ruleName + ':$' + (index + 1).toString(); | ||
}, ruleObj.fullName); | ||
var ruleObj = _this._createRuleObject(rule); | ||
var validationRuleFn = validator.validation.rules[ruleObj.fullName]; | ||
@@ -307,3 +316,3 @@ var validationResult = validationRuleFn(val, ruleObj, propertyName, inputObj); | ||
}); | ||
}); | ||
}, ruleMapping); | ||
@@ -326,4 +335,4 @@ return { | ||
value: function getValidationMessage(ruleObj, propertyName, val) { | ||
var compiled = _lodash2.default.template(this.validation.messages[ruleObj.fullName]); | ||
propertyName = _lodash2.default.startCase(propertyName); | ||
var compiled = _fp2.default.template(this.validation.messages[ruleObj.fullName]); | ||
propertyName = _fp2.default.startCase(propertyName); | ||
@@ -342,2 +351,4 @@ return compiled({ | ||
* addCustomValidation method on satpam module. | ||
* | ||
* @example | ||
* import satpam from 'satpam'; | ||
@@ -360,4 +371,7 @@ * satpam.addCustomValidation(.., ..); | ||
* message then use addCustomValidation method on satpam module. | ||
* | ||
* @example | ||
* import satpam from 'satpam'; | ||
* satpam.setValidationMessage(.., ..); | ||
* | ||
* @param ruleName | ||
@@ -390,6 +404,6 @@ * @param message | ||
* @example | ||
* let ruleMapping = {name: ['required']}; | ||
* let inputObj = {name: ''}; | ||
* let validator = satpam.create(); | ||
* let result = validator.validate(ruleMapping, inputObj); | ||
* const ruleMapping = {name: ['required']}; | ||
* const inputObj = {name: ''}; | ||
* const validator = satpam.create(); | ||
* const result = validator.validate(ruleMapping, inputObj); | ||
* | ||
@@ -410,2 +424,4 @@ * @param ruleMapping - An mapping of input property to the available rules | ||
* addCustomValidation method on satpam module. | ||
* | ||
* @example | ||
* import satpam from 'satpam'; | ||
@@ -425,4 +441,7 @@ * satpam.addCustomValidation(.., ..); | ||
* message then use addCustomValidation method on satpam module. | ||
* | ||
* @example | ||
* import satpam from 'satpam'; | ||
* satpam.setValidationMessage(.., ..); | ||
* | ||
* @param ruleName | ||
@@ -429,0 +448,0 @@ * @param message |
{ | ||
"name": "satpam", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Simple and Effective Object Validator", | ||
@@ -9,3 +9,3 @@ "main": "lib/index.js", | ||
"prepublish": "npm run compile", | ||
"test": "npm run compile && mocha --compilers js:babel-core/register" | ||
"test": "npm run compile && mocha test --recursive --compilers js:babel-core/register" | ||
}, | ||
@@ -12,0 +12,0 @@ "repository": "sendyhalim/satpam", |
@@ -5,269 +5,359 @@ import { expect } from 'chai'; | ||
describe('Validator', () => { | ||
const rules = { | ||
name: ['required'], | ||
officeEmail: ['required', 'email'], | ||
officeEmailOptional: ['email'] | ||
}; | ||
describe('.createRuleObject', () => { | ||
const validator = satpam.create(); | ||
context('Test with simple rules', () => { | ||
it('should success', () => { | ||
const result = satpam.validate(rules, { | ||
name: 'sendy', | ||
officeEmail: 'asd@gg.com' | ||
context('when the given rule is an object', () => { | ||
context('and it has no rule parameters', () => { | ||
it('should create valid ruleObj', () => { | ||
const ruleObj = validator._createRuleObject({ | ||
name: 'wut' | ||
}); | ||
expect(ruleObj).to.deep.equal({ | ||
name: 'wut', | ||
fullName: 'wut', | ||
params: [] | ||
}); | ||
}); | ||
}); | ||
const err = result.messages; | ||
expect(result.success).to.equal(true); | ||
expect(err).to.not.have.property('name'); | ||
expect(err).to.not.have.property('officeEmail'); | ||
expect(err).to.not.have.property('officeEmailOptional'); | ||
}); | ||
context('and it has one rule parameter', () => { | ||
it('should create valid ruleObj', () => { | ||
const ruleObj = validator._createRuleObject({ | ||
name: 'this', | ||
params: ['is smelll'] | ||
}); | ||
it('should fail with empty name', () => { | ||
const result = satpam.validate(rules, { | ||
name: '', | ||
officeEmail: 'asd@gg.com' | ||
expect(ruleObj).to.deep.equal({ | ||
name: 'this', | ||
fullName: 'this:$1', | ||
params: ['is smelll'] | ||
}); | ||
}); | ||
}); | ||
const err = result.messages; | ||
expect(result.success).to.equal(false); | ||
expect(err).to.have.property('name'); | ||
expect(err.name.required).to.equal('Name field is required.'); | ||
expect(err.name).to.have.property('required'); | ||
expect(err).to.not.have.property('officeEmail'); | ||
expect(err).to.not.have.property('officeEmailOptional'); | ||
context('and it has two rule parameters', () => { | ||
it('should create valid ruleObj', () => { | ||
const ruleObj = validator._createRuleObject({ | ||
name: 'woosah', | ||
params: ['rocky', 'balboa'] | ||
}); | ||
expect(ruleObj).to.deep.equal({ | ||
name: 'woosah', | ||
fullName: 'woosah:$1:$2', | ||
params: ['rocky', 'balboa'] | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should fail with optional email', () => { | ||
const result = satpam.validate(rules, { | ||
name: 'sendy', | ||
officeEmail: 'asd@gg.com', | ||
officeEmailOptional: 'hehe' | ||
context('when the given rule is a string', () => { | ||
context('and it has no rule parameters', () => { | ||
it('should create valid ruleObj', () => { | ||
const ruleObj = validator._createRuleObject('minions'); | ||
expect(ruleObj).to.deep.equal({ | ||
name: 'minions', | ||
fullName: 'minions', | ||
params: [] | ||
}); | ||
}); | ||
}); | ||
const err = result.messages; | ||
expect(result.success).to.equal(false); | ||
expect(err).to.have.property('officeEmailOptional'); | ||
expect(err.officeEmailOptional).to.have.property('email'); | ||
expect(err).to.not.have.property('name'); | ||
expect(err).to.not.have.property('officeEmail'); | ||
context('and it has one rule parameter', () => { | ||
it('should create valid ruleObj', () => { | ||
const ruleObj = validator._createRuleObject('minions:banana'); | ||
expect(ruleObj).to.deep.equal({ | ||
name: 'minions', | ||
fullName: 'minions:$1', | ||
params: ['banana'] | ||
}); | ||
}); | ||
}); | ||
context('and it has two rule parameters', () => { | ||
it('should create valid ruleObj', () => { | ||
const ruleObj = validator._createRuleObject('minions:banana:hulala'); | ||
expect(ruleObj).to.deep.equal({ | ||
name: 'minions', | ||
fullName: 'minions:$1:$2', | ||
params: ['banana', 'hulala'] | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
context('Test with custom rules', () => { | ||
satpam.addCustomValidation('must-be-ironman', function (val) { | ||
return val === 'ironman'; | ||
}); | ||
satpam.setValidationMessage('must-be-ironman', 'Not ironman D:'); | ||
describe('.validate()', () => { | ||
const rules = { | ||
name: ['required', 'must-be-ironman'], | ||
phone: ['numeric'] | ||
name: ['required'], | ||
officeEmail: ['required', 'email'], | ||
officeEmailOptional: ['email'] | ||
}; | ||
it('should success', () => { | ||
const result = satpam.validate(rules, { | ||
name: 'ironman', | ||
phone: 123131 | ||
context('Test with simple rules', () => { | ||
it('should success', () => { | ||
const result = satpam.validate(rules, { | ||
name: 'sendy', | ||
officeEmail: 'asd@gg.com' | ||
}); | ||
const err = result.messages; | ||
expect(result.success).to.equal(true); | ||
expect(err).to.not.have.property('name'); | ||
expect(err).to.not.have.property('officeEmail'); | ||
expect(err).to.not.have.property('officeEmailOptional'); | ||
}); | ||
const err = result.messages; | ||
expect(result.success).to.equal(true); | ||
expect(err).to.not.have.property('name'); | ||
expect(err).to.not.have.property('phone'); | ||
}); | ||
it('should fail with empty name', () => { | ||
const result = satpam.validate(rules, { | ||
name: '', | ||
officeEmail: 'asd@gg.com' | ||
}); | ||
const err = result.messages; | ||
it('should fail', () => { | ||
const result = satpam.validate(rules, { | ||
name: 'sendy', | ||
phone: 123131 | ||
expect(result.success).to.equal(false); | ||
expect(err).to.have.property('name'); | ||
expect(err.name.required).to.equal('Name field is required.'); | ||
expect(err.name).to.have.property('required'); | ||
expect(err).to.not.have.property('officeEmail'); | ||
expect(err).to.not.have.property('officeEmailOptional'); | ||
}); | ||
const err = result.messages; | ||
expect(result.success).to.equal(false); | ||
expect(err).to.have.property('name'); | ||
expect(err.name['must-be-ironman']).to.equal('Not ironman D:'); | ||
expect(err).to.not.have.property('phone'); | ||
expect(err.messageArray.length).to.equal(1); | ||
expect(err.messageArray[0]).to.equal('Not ironman D:'); | ||
}); | ||
}); | ||
it('should fail with optional email', () => { | ||
const result = satpam.validate(rules, { | ||
name: 'sendy', | ||
officeEmail: 'asd@gg.com', | ||
officeEmailOptional: 'hehe' | ||
}); | ||
const err = result.messages; | ||
context('Test with rule params', () => { | ||
it('should fail with custom validation range:0:30', () => { | ||
satpam.addCustomValidation('range:$1:$2', function (val, ruleObj) { | ||
return val >= ruleObj.params[0] && val <= ruleObj.params[1]; | ||
expect(result.success).to.equal(false); | ||
expect(err).to.have.property('officeEmailOptional'); | ||
expect(err.officeEmailOptional).to.have.property('email'); | ||
expect(err).to.not.have.property('name'); | ||
expect(err).to.not.have.property('officeEmail'); | ||
}); | ||
satpam.setValidationMessage('range:$1:$2', '<%= propertyName %> must between <%= ruleParams[0] %> and <%= ruleParams[1] %>'); | ||
const obj = { salary: 31 }; | ||
const rules = { salary: ['range:0:30'] }; | ||
const result = satpam.validate(rules, obj); | ||
const err = result.messages; | ||
expect(result.success).to.equal(false); | ||
expect(err.salary['range:$1:$2']).to.equal('Salary must between 0 and 30'); | ||
expect(err.messageArray.length).to.equal(1); | ||
}); | ||
}); | ||
context('Test with multiple errors', () => { | ||
it('should have multiple errors', () => { | ||
satpam.addCustomValidation('must-equal:$1', function (val, ruleObj) { | ||
return val === ruleObj.params[0]; | ||
context('Test with custom rules', () => { | ||
satpam.addCustomValidation('must-be-ironman', function (val) { | ||
return val === 'ironman'; | ||
}); | ||
satpam.setValidationMessage('must-equal:$1', '<%= propertyName %> must equal <%= ruleParams[0] %> !!'); | ||
satpam.setValidationMessage('must-be-ironman', 'Not ironman D:'); | ||
const obj = { | ||
officeEmail: 'asd@gg.com' | ||
}; | ||
const rules = { | ||
name: ['must-equal:sendyhalim'], | ||
officeEmail: ['email'], | ||
address: ['must-equal:dimana aja bole', 'required'], | ||
title: ['required'] | ||
name: ['required', 'must-be-ironman'], | ||
phone: ['numeric'] | ||
}; | ||
const result = satpam.validate(rules, obj); | ||
const err = result.messages; | ||
expect(result.success).to.equal(false); | ||
expect(err.messageArray.length).to.equal(4); | ||
expect(err.name['must-equal:$1']).to.equal('Name must equal sendyhalim !!'); | ||
expect(err.address['must-equal:$1']).to.equal('Address must equal dimana aja bole !!'); | ||
expect(err.address['required']).to.equal('Address field is required.'); | ||
expect(err.title['required']).to.equal('Title field is required.'); | ||
}); | ||
}); | ||
it('should success', () => { | ||
const result = satpam.validate(rules, { | ||
name: 'ironman', | ||
phone: 123131 | ||
}); | ||
const err = result.messages; | ||
context('.getValidationMessage()', () => { | ||
context('when instance validation message is customized', () => { | ||
const validator = satpam.create(); | ||
before('set validation message', () => { | ||
validator.setValidationMessage('required', 'bulbazaurz'); | ||
expect(result.success).to.equal(true); | ||
expect(err).to.not.have.property('name'); | ||
expect(err).to.not.have.property('phone'); | ||
}); | ||
it('should return correct validation message', () => { | ||
const ruleObj = { | ||
fullName: 'required' | ||
}; | ||
it('should fail', () => { | ||
const result = satpam.validate(rules, { | ||
name: 'sendy', | ||
phone: 123131 | ||
}); | ||
const err = result.messages; | ||
const message = validator.getValidationMessage(ruleObj, 'name', 'wut'); | ||
expect(message).to.equal('bulbazaurz'); | ||
expect(result.success).to.equal(false); | ||
expect(err).to.have.property('name'); | ||
expect(err.name['must-be-ironman']).to.equal('Not ironman D:'); | ||
expect(err).to.not.have.property('phone'); | ||
expect(err.messageArray.length).to.equal(1); | ||
expect(err.messageArray[0]).to.equal('Not ironman D:'); | ||
}); | ||
}); | ||
it('should not affect global validation message', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = satpam.validate(rules, {}); | ||
context('Test with rule params', () => { | ||
it('should fail with custom validation range:0:30', () => { | ||
satpam.addCustomValidation('range:$1:$2', function (val, ruleObj) { | ||
return val >= ruleObj.params[0] && val <= ruleObj.params[1]; | ||
}); | ||
satpam.setValidationMessage('range:$1:$2', '<%= propertyName %> must between <%= ruleParams[0] %> and <%= ruleParams[1] %>'); | ||
expect(result.messages.fullName.required).to.equal('Full Name field is required.'); | ||
const obj = { salary: 31 }; | ||
const rules = { salary: ['range:0:30'] }; | ||
const result = satpam.validate(rules, obj); | ||
const err = result.messages; | ||
expect(result.success).to.equal(false); | ||
expect(err.salary['range:$1:$2']).to.equal('Salary must between 0 and 30'); | ||
expect(err.messageArray.length).to.equal(1); | ||
}); | ||
}); | ||
context('when global validation message is customized', () => { | ||
const validator = satpam.create(); | ||
context('Test with multiple errors', () => { | ||
it('should have multiple errors', () => { | ||
satpam.addCustomValidation('must-equal:$1', function (val, ruleObj) { | ||
return val === ruleObj.params[0]; | ||
}); | ||
satpam.setValidationMessage('must-equal:$1', '<%= propertyName %> must equal <%= ruleParams[0] %> !!'); | ||
before('set global validation message', () => { | ||
satpam.setValidationMessage('required', 'huuu!'); | ||
}); | ||
const obj = { | ||
officeEmail: 'asd@gg.com' | ||
}; | ||
it('should return correct validation message', () => { | ||
const ruleObj = {fullName: ['required']}; | ||
const result = satpam.validate(ruleObj, {}); | ||
const rules = { | ||
name: ['must-equal:sendyhalim'], | ||
officeEmail: ['email'], | ||
address: ['must-equal:dimana aja bole', 'required'], | ||
title: ['required'] | ||
}; | ||
const result = satpam.validate(rules, obj); | ||
const err = result.messages; | ||
expect(result.messages.fullName.required).to.equal('huuu!'); | ||
expect(result.success).to.equal(false); | ||
expect(err.messageArray.length).to.equal(4); | ||
expect(err.name['must-equal:$1']).to.equal('Name must equal sendyhalim !!'); | ||
expect(err.address['must-equal:$1']).to.equal('Address must equal dimana aja bole !!'); | ||
expect(err.address['required']).to.equal('Address field is required.'); | ||
expect(err.title['required']).to.equal('Title field is required.'); | ||
}); | ||
}); | ||
it('should affect the newly created validator instance', () => { | ||
const newValidator = satpam.create(); | ||
const ruleObj = {fullName: 'required'}; | ||
context('.getValidationMessage()', () => { | ||
context('when instance validation message is customized', () => { | ||
const validator = satpam.create(); | ||
const message = newValidator.getValidationMessage(ruleObj, 'fullName', 'wut'); | ||
expect(message).to.equal('huuu!'); | ||
}); | ||
before('set validation message', () => { | ||
validator.setValidationMessage('required', 'bulbazaurz'); | ||
}); | ||
it('should not affect the old validator insance', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = validator.validate(rules, {}); | ||
it('should return correct validation message', () => { | ||
const ruleObj = { | ||
fullName: 'required' | ||
}; | ||
expect(result.messages.fullName.required).to.equal('Full Name field is required.'); | ||
}); | ||
}); | ||
}); | ||
const message = validator.getValidationMessage(ruleObj, 'name', 'wut'); | ||
expect(message).to.equal('bulbazaurz'); | ||
}); | ||
context('.addCustomValidation()', () => { | ||
context('when instance validation rule is customized', () => { | ||
const validator = satpam.create(); | ||
it('should not affect global validation message', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = satpam.validate(rules, {}); | ||
before('set validation rule', () => { | ||
validator.addCustomValidation('required', function (val) { | ||
return val === 'yo'; | ||
expect(result.messages.fullName.required).to.equal('Full Name field is required.'); | ||
}); | ||
}); | ||
it('should not pass with the customized required rule', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = validator.validate(rules, {fullName: 'ayo'}); | ||
context('when global validation message is customized', () => { | ||
const validator = satpam.create(); | ||
expect(result.success).to.be.false; | ||
expect(result.messages.fullName.required).to.equal('Full Name field is required.'); | ||
}); | ||
before('set global validation message', () => { | ||
satpam.setValidationMessage('required', 'huuu!'); | ||
}); | ||
it('should pass with the customized required rule', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = satpam.validate(rules, { | ||
fullName: 'yo' | ||
it('should return correct validation message', () => { | ||
const ruleObj = {fullName: ['required']}; | ||
const result = satpam.validate(ruleObj, {}); | ||
expect(result.messages.fullName.required).to.equal('huuu!'); | ||
}); | ||
expect(result.success).to.be.true; | ||
expect(result.messages).to.not.have.property('fullName'); | ||
}); | ||
it('should affect the newly created validator instance', () => { | ||
const newValidator = satpam.create(); | ||
const ruleObj = {fullName: 'required'}; | ||
it('should not affect global validation rule', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = satpam.validate(rules, {fullName: 'hehe'}); | ||
const message = newValidator.getValidationMessage(ruleObj, 'fullName', 'wut'); | ||
expect(message).to.equal('huuu!'); | ||
}); | ||
expect(result.success).to.be.true; | ||
expect(result.messages).to.not.have.property('fullName'); | ||
it('should not affect the old validator insance', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = validator.validate(rules, {}); | ||
expect(result.messages.fullName.required).to.equal('Full Name field is required.'); | ||
}); | ||
}); | ||
}); | ||
context('when global validation rule is customized', () => { | ||
const oldValidator = satpam.create(); | ||
context('.addCustomValidation()', () => { | ||
context('when instance validation rule is customized', () => { | ||
const validator = satpam.create(); | ||
before('add global validation rule', () => { | ||
satpam.addCustomValidation('wutwut', function (val) { | ||
return val === 'wut?'; | ||
before('set validation rule', () => { | ||
validator.addCustomValidation('required', function (val) { | ||
return val === 'yo'; | ||
}); | ||
}); | ||
satpam.setValidationMessage('wutwut', 'lukeskywalker'); | ||
}); | ||
it('should not pass with the customized required rule', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = validator.validate(rules, {fullName: 'ayo'}); | ||
it('should return correct validation result', () => { | ||
const ruleObj = {fullName: ['wutwut']}; | ||
const result = satpam.validate(ruleObj, {fullName: 'wut?'}); | ||
expect(result.success).to.be.false; | ||
expect(result.messages.fullName.required).to.equal('Full Name field is required.'); | ||
}); | ||
expect(result.success).to.be.true; | ||
expect(result.messages).to.not.have.property('fullName'); | ||
}); | ||
it('should pass with the customized required rule', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = satpam.validate(rules, { | ||
fullName: 'yo' | ||
}); | ||
it('should affect the newly created validator instance', () => { | ||
const newValidator = satpam.create(); | ||
const ruleObj = {fullName: ['wutwut']}; | ||
const result = newValidator.validate(ruleObj, {fullName: 'wut?'}); | ||
expect(result.success).to.be.true; | ||
expect(result.messages).to.not.have.property('fullName'); | ||
}); | ||
expect(result.success).to.be.true; | ||
expect(result.messages).to.not.have.property('fullName'); | ||
it('should not affect global validation rule', () => { | ||
const rules = {fullName: ['required']}; | ||
const result = satpam.validate(rules, {fullName: 'hehe'}); | ||
expect(result.success).to.be.true; | ||
expect(result.messages).to.not.have.property('fullName'); | ||
}); | ||
}); | ||
it('should not affect the old validator insance', () => { | ||
const ruleObj = {fullName: ['wutwut']}; | ||
const validateFn = oldValidator.validate.bind( | ||
oldValidator, | ||
ruleObj, | ||
{fullName: 'hiks'} | ||
); | ||
context('when global validation rule is customized', () => { | ||
const oldValidator = satpam.create(); | ||
expect(validateFn).to.throw(Error); | ||
before('add global validation rule', () => { | ||
satpam.addCustomValidation('wutwut', function (val) { | ||
return val === 'wut?'; | ||
}); | ||
satpam.setValidationMessage('wutwut', 'lukeskywalker'); | ||
}); | ||
it('should return correct validation result', () => { | ||
const ruleObj = {fullName: ['wutwut']}; | ||
const result = satpam.validate(ruleObj, {fullName: 'wut?'}); | ||
expect(result.success).to.be.true; | ||
expect(result.messages).to.not.have.property('fullName'); | ||
}); | ||
it('should affect the newly created validator instance', () => { | ||
const newValidator = satpam.create(); | ||
const ruleObj = {fullName: ['wutwut']}; | ||
const result = newValidator.validate(ruleObj, {fullName: 'wut?'}); | ||
expect(result.success).to.be.true; | ||
expect(result.messages).to.not.have.property('fullName'); | ||
}); | ||
it('should not affect the old validator insance', () => { | ||
const ruleObj = {fullName: ['wutwut']}; | ||
const validateFn = oldValidator.validate.bind( | ||
oldValidator, | ||
ruleObj, | ||
{fullName: 'hiks'} | ||
); | ||
expect(validateFn).to.throw(Error); | ||
}); | ||
}); | ||
@@ -274,0 +364,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
123745
2601
1