Comparing version 1.0.4 to 2.0.0
@@ -0,1 +1,6 @@ | ||
### v2.0.0 | ||
* updated `joi` to ^9. | ||
* requires node 4 minimum. | ||
### v1.0.4 | ||
@@ -2,0 +7,0 @@ |
120
lib/enjoi.js
'use strict'; | ||
var Assert = require('assert'); | ||
var Joi = require('joi'); | ||
var Thing = require('core-util-is'); | ||
const Assert = require('assert'); | ||
const Joi = require('joi'); | ||
const Thing = require('core-util-is'); | ||
const Alternatives = require('joi/lib/alternatives').constructor; | ||
module.exports = function enjoi(schema, options) { | ||
var subSchemas, types; | ||
options = options || {}; | ||
@@ -15,4 +14,4 @@ | ||
subSchemas = options.subSchemas; | ||
types = options.types; | ||
const subSchemas = options.subSchemas; | ||
const types = options.types; | ||
@@ -54,6 +53,6 @@ Assert.ok(!subSchemas || Thing.isObject(subSchemas), 'Expected options.subSchemas to be an object.'); | ||
function resolveref(value) { | ||
var id, refschema, path, fragment, paths; | ||
let refschema; | ||
id = value.substr(0, value.indexOf('#') + 1); | ||
path = value.substr(value.indexOf('#') + 1); | ||
const id = value.substr(0, value.indexOf('#') + 1); | ||
const path = value.substr(value.indexOf('#') + 1); | ||
@@ -69,6 +68,6 @@ if (id && subSchemas) { | ||
fragment = refschema; | ||
paths = path.split('/'); | ||
let fragment = refschema; | ||
const paths = path.split('/'); | ||
for (var i = 1; i < paths.length && fragment; i++) { | ||
for (let i = 1; i < paths.length && fragment; i++) { | ||
fragment = typeof fragment === 'object' && fragment[paths[i]]; | ||
@@ -81,3 +80,3 @@ } | ||
function resolvetype(current) { | ||
var joischema; | ||
let joischema; | ||
@@ -137,3 +136,3 @@ switch (current.type) { | ||
function resolveproperties(current) { | ||
var schemas = {}; | ||
const schemas = {}; | ||
@@ -145,8 +144,6 @@ if (!Thing.isObject(current.properties)) { | ||
Object.keys(current.properties).forEach(function (key) { | ||
var joischema, property; | ||
const property = current.properties[key]; | ||
property = current.properties[key]; | ||
let joischema = resolve(property); | ||
joischema = resolve(property); | ||
if (current.required && !!~current.required.indexOf(key)) { | ||
@@ -163,3 +160,3 @@ joischema = joischema.required(); | ||
function object(current) { | ||
var joischema = Joi.object(resolveproperties(current)); | ||
let joischema = Joi.object(resolveproperties(current)); | ||
@@ -177,3 +174,3 @@ if (current.additionalProperties === true) { | ||
function array(current) { | ||
var joischema = Joi.array(); | ||
let joischema = Joi.array(); | ||
@@ -193,3 +190,3 @@ joischema = joischema.items(resolve(current.items)); | ||
function number(current) { | ||
var joischema = Joi.number(); | ||
let joischema = Joi.number(); | ||
@@ -207,3 +204,3 @@ if (current.type === 'integer') { | ||
function string(current) { | ||
var joischema = Joi.string(); | ||
let joischema = Joi.string(); | ||
@@ -230,9 +227,10 @@ if (current.enum) { | ||
function regularString(current) { | ||
var joischema = Joi.string(); | ||
let joischema = Joi.string(); | ||
current.pattern && (joischema = joischema.regex(new RegExp(current.pattern))); | ||
if (Thing.isUndefined(current.minLength)) { | ||
current.minLength = 0; | ||
} | ||
if (Thing.isNumber(current.minLength)) { | ||
@@ -250,3 +248,3 @@ if (current.minLength === 0) { | ||
function email(current) { | ||
var joischema = Joi.string().email(); | ||
let joischema = Joi.string().email(); | ||
Thing.isNumber(current.maxLength) && (joischema = joischema.max(current.maxLength)); | ||
@@ -257,3 +255,3 @@ return joischema; | ||
function date(current) { | ||
var joischema = Joi.date(); | ||
let joischema = Joi.date(); | ||
current.min && (joischema = joischema.min(current.min)); | ||
@@ -267,44 +265,42 @@ current.max && (joischema = joischema.max(current.max)); | ||
class All extends Alternatives { | ||
constructor() { | ||
super(); | ||
this._type = 'all'; | ||
this._invalids.remove(null); | ||
this._inner.matches = []; | ||
} | ||
_base(value, state, options) { | ||
let errors = []; | ||
const results = []; | ||
function All() { | ||
All.super_.call(this); | ||
this._type = 'all'; | ||
this._invalids.remove(null); | ||
this._inner.matches = []; | ||
} | ||
if (!options) { | ||
options = {}; | ||
} | ||
require('util').inherits(All, Object.getPrototypeOf(require('joi/lib/alternatives')).constructor); | ||
options.stripUnknown = true; | ||
All.prototype._base = function (value, state, options) { | ||
var errors = []; | ||
var results = []; | ||
for (let i = 0, il = this._inner.matches.length; i < il; ++i) { | ||
const item = this._inner.matches[i]; | ||
let schema = item.schema; | ||
if (!schema) { | ||
const failed = item.is._validate(item.ref(state.parent, options), null, options, state.parent).errors; | ||
schema = failed ? item.otherwise : item.then; | ||
if (!schema) { | ||
continue; | ||
} | ||
} | ||
if (!options) { | ||
options = {}; | ||
} | ||
const result = schema._validate(value, state, options); | ||
options.stripUnknown = true; | ||
for (var i = 0, il = this._inner.matches.length; i < il; ++i) { | ||
var item = this._inner.matches[i]; | ||
var schema = item.schema; | ||
if (!schema) { | ||
var failed = item.is._validate(item.ref(state.parent, options), null, options, state.parent).errors; | ||
schema = failed ? item.otherwise : item.then; | ||
if (!schema) { | ||
continue; | ||
if (!result.errors) { | ||
results.push(result.value); | ||
} | ||
else { | ||
errors = errors.concat(result.errors); | ||
} | ||
} | ||
var result = schema._validate(value, state, options); | ||
if (!result.errors) { | ||
results.push(result.value); | ||
} | ||
else { | ||
errors = errors.concat(result.errors); | ||
} | ||
return { value: value, errors: errors }; | ||
} | ||
return { value: value, errors: errors }; | ||
}; | ||
} |
{ | ||
"name": "enjoi", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"description": "Converts json-schema to Joi schema for validation.", | ||
@@ -16,2 +16,5 @@ "main": "lib/enjoi.js", | ||
}, | ||
"engines": { | ||
"node": "4.x" | ||
}, | ||
"keywords": [ | ||
@@ -34,3 +37,3 @@ "joi", | ||
"core-util-is": "^1.0.1", | ||
"joi": "^6.4.3" | ||
"joi": "^9.2.0" | ||
}, | ||
@@ -37,0 +40,0 @@ "devDependencies": { |
14973
+ Addedhoek@4.3.1(transitive)
+ Addedisemail@2.2.1(transitive)
+ Addeditems@2.2.1(transitive)
+ Addedjoi@9.2.0(transitive)
+ Addedtopo@2.1.1(transitive)
- Removedhoek@2.16.3(transitive)
- Removedisemail@1.2.0(transitive)
- Removedjoi@6.10.1(transitive)
- Removedtopo@1.1.0(transitive)
Updatedjoi@^9.2.0