joi-nochange
Advanced tools
Comparing version 0.2.0 to 1.0.0-0
58
index.js
'use strict'; | ||
var Any = require('joi/lib/any'); | ||
var language = require('joi/lib/language'); | ||
var Hoek = require('hoek'); | ||
const Joi = require('joi'); | ||
const Hoek = require('hoek'); | ||
// monkey patch noChange() into Any's prototype | ||
Any.prototype.noChange = function (sourceObj) { | ||
// creates a rule for the provided base type | ||
function createRule(joiType) { | ||
const joiBaseType = Joi[joiType]; | ||
return this._test('noChange', null, (value, state, options) => { | ||
return { | ||
base: joiBaseType(), | ||
name: joiType, | ||
language: { | ||
noChange: 'is not allowed to change' // maybe use 'may not change from {{q}}' instead? | ||
}, | ||
rules: [{ | ||
name: 'noChange', | ||
params: { | ||
q: Joi.any() | ||
}, | ||
validate(params, value, state, options) { | ||
const path = state.path === '' ? null : state.path; | ||
const origValue = Hoek.reach(sourceObj, path); | ||
if (Hoek.deepEqual(origValue, value)) { | ||
return null; | ||
} | ||
return this.createError('any.noChange', null, state, options); | ||
}); | ||
}; | ||
const path = state.path === '' ? null : state.path; | ||
const origValue = Hoek.reach(params.q, path); | ||
// Add any.noChang error message to the language map | ||
language.errors.any.noChange = 'is not allowed to change'; | ||
if (Hoek.deepEqual(origValue, value)) { | ||
return null; // Everything is OK | ||
} | ||
return this.createError(joiType + '.noChange', { | ||
v: value//, | ||
// q: origValue | ||
}, state, options); | ||
} | ||
}] | ||
}; | ||
} | ||
// extend Joi and export | ||
// due to extending any() not extending other types, number(), string(), etc. we need to extend all of these manually | ||
// see https://github.com/hapijs/joi/issues/577 | ||
module.exports = Joi | ||
.extend(createRule('any')) | ||
.extend(createRule('object')) | ||
.extend(createRule('number')) | ||
.extend(createRule('string')) | ||
.extend(createRule('date')) | ||
.extend(createRule('boolean')); |
{ | ||
"name": "joi-nochange", | ||
"version": "0.2.0", | ||
"version": "1.0.0-0", | ||
"description": "Adds noChange() to Joi", | ||
@@ -30,6 +30,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"hoek": "^3.0.4" | ||
"hoek": "^3.0.4", | ||
"joi": "9.0.0-6" | ||
}, | ||
"peerDependencies": { | ||
"joi": ">=6.0.0" | ||
"joi": ">=9.0.0" | ||
}, | ||
@@ -36,0 +37,0 @@ "devDependencies": { |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
5145
4
41
1
53
3
+ Addedjoi@9.0.0-6
+ Addedhoek@4.3.1(transitive)
+ Addedisemail@2.2.1(transitive)
+ Addeditems@2.2.1(transitive)
+ Addedjoi@9.0.0-6(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedtopo@2.1.1(transitive)
- Removed@hapi/hoek@9.3.0(transitive)
- Removed@hapi/topo@5.1.0(transitive)
- Removed@sideway/address@4.1.5(transitive)
- Removed@sideway/formula@3.0.1(transitive)
- Removed@sideway/pinpoint@2.0.0(transitive)
- Removedjoi@17.13.3(transitive)