Comparing version 0.1.16 to 0.2.0
'use strict'; | ||
var assert = require('assert'), | ||
joi = require('joi'), | ||
thing = require('core-util-is'); | ||
var Assert = require('assert'); | ||
var Joi = require('joi'); | ||
var Thing = require('core-util-is'); | ||
module.exports = function enjoi(schema, subSchemas) { | ||
assert.ok(thing.isObject(schema), 'Expected schema to be an object.'); | ||
assert.ok(!subSchemas || thing.isObject(subSchemas), 'Expected subSchemas to be an object.'); | ||
Assert.ok(Thing.isObject(schema), 'Expected schema to be an object.'); | ||
Assert.ok(!subSchemas || Thing.isObject(subSchemas), 'Expected subSchemas to be an object.'); | ||
function resolve(current) { | ||
@@ -21,3 +22,3 @@ if (current.type) { | ||
if (current.enum) { | ||
return joi.any().valid(current.enum); | ||
return Joi.any().valid(current.enum); | ||
} | ||
@@ -27,3 +28,3 @@ | ||
console.warn('WARNING: schema missing a \'type\' or \'$ref\' or \'enum\': %s', JSON.stringify(current)); | ||
return joi.any(); | ||
return Joi.any(); | ||
} | ||
@@ -44,6 +45,6 @@ | ||
assert.ok(refschema, 'Can not find schema reference: ' + value + '.'); | ||
Assert.ok(refschema, 'Can not find schema reference: ' + value + '.'); | ||
fragment = refschema; | ||
paths = Array.isArray(path) ? path : path.split('/'); | ||
paths = path.split('/'); | ||
@@ -65,3 +66,3 @@ for (var i = 1; i < paths.length && fragment; i++) { | ||
case 'boolean': | ||
joischema = joi.boolean(); | ||
joischema = Joi.boolean(); | ||
break; | ||
@@ -80,3 +81,3 @@ case 'integer': | ||
assert.ok(joischema, 'Could not resolve type: ' + current.type + '.'); | ||
Assert.ok(joischema, 'Could not resolve type: ' + current.type + '.'); | ||
@@ -89,3 +90,3 @@ return joischema; | ||
if (!thing.isObject(current.properties)) { | ||
if (!Thing.isObject(current.properties)) { | ||
return; | ||
@@ -112,6 +113,6 @@ } | ||
function object(current) { | ||
var joischema = joi.object(resolveproperties(current)); | ||
var joischema = Joi.object(resolveproperties(current)); | ||
thing.isNumber(current.minProperties) && (joischema = joischema.min(current.minProperties)); | ||
thing.isNumber(current.maxProperties) && (joischema = joischema.max(current.maxProperties)); | ||
Thing.isNumber(current.minProperties) && (joischema = joischema.min(current.minProperties)); | ||
Thing.isNumber(current.maxProperties) && (joischema = joischema.max(current.maxProperties)); | ||
@@ -122,8 +123,8 @@ return joischema; | ||
function array(current) { | ||
var joischema = joi.array(); | ||
var joischema = Joi.array(); | ||
joischema = joischema.includes(resolve(current.items)); | ||
thing.isNumber(current.minItems) && (joischema = joischema.min(current.minItems)); | ||
thing.isNumber(current.maxItems) && (joischema = joischema.max(current.maxItems)); | ||
Thing.isNumber(current.minItems) && (joischema = joischema.min(current.minItems)); | ||
Thing.isNumber(current.maxItems) && (joischema = joischema.max(current.maxItems)); | ||
@@ -138,3 +139,3 @@ if (current.uniqueItems) { | ||
function number(current) { | ||
var joischema = joi.number(); | ||
var joischema = Joi.number(); | ||
@@ -145,4 +146,4 @@ if (current.type === 'integer') { | ||
thing.isNumber(current.minimum) && (joischema = joischema.min(current.minimum)); | ||
thing.isNumber(current.maximum) && (joischema = joischema.max(current.maximum)); | ||
Thing.isNumber(current.minimum) && (joischema = joischema.min(current.minimum)); | ||
Thing.isNumber(current.maximum) && (joischema = joischema.max(current.maximum)); | ||
@@ -153,8 +154,14 @@ return joischema; | ||
function string(current) { | ||
var joischema = joi.string(); | ||
var joischema = Joi.string(); | ||
current.pattern && (joischema = joischema.regex(new RegExp(current.pattern))); | ||
thing.isNumber(current.minLength) && (joischema = joischema.min(current.minLength)); | ||
thing.isNumber(current.maxLength) && (joischema = joischema.max(current.maxLength)); | ||
if (Thing.isNumber(current.minLength)) { | ||
if (current.minLength === 0) { | ||
joischema = joischema.allow(''); | ||
} | ||
joischema = joischema.min(current.minLength); | ||
} | ||
Thing.isNumber(current.maxLength) && (joischema = joischema.max(current.maxLength)); | ||
return joischema; | ||
@@ -161,0 +168,0 @@ } |
{ | ||
"name": "enjoi", | ||
"version": "0.1.16", | ||
"version": "0.2.0", | ||
"description": "Converts json-schema to Joi schema for validation.", | ||
@@ -36,8 +36,9 @@ "main": "lib/enjoi.js", | ||
"devDependencies": { | ||
"tape": "^2.14.0", | ||
"hammertime": "^0.1.2", | ||
"istanbul": "^0.3.2", | ||
"jshint": "^2.5.5", | ||
"hammertime": "^0.1.2", | ||
"tape": "^2.14.0", | ||
"themis": "^1.0.0", | ||
"tv4": "^1.1.0" | ||
} | ||
} |
@@ -20,4 +20,2 @@ [![Build Status](https://travis-ci.org/tlivings/enjoi.png)](https://travis-ci.org/tlivings/enjoi) [![NPM version](https://badge.fury.io/js/enjoi.png)](http://badge.fury.io/js/enjoi) | ||
- `object:additionalProperties` | ||
- `object:maxProperties` | ||
- `object:minProperties` | ||
- `array:items` (supports as single schema, not supported as array of schemas). | ||
@@ -97,10 +95,2 @@ - `array:additionalItems` | ||
You can run a benchmark against `tv4` by running the following command. | ||
```shell | ||
$ npm run bench | ||
$ | ||
$ tv4 vs joi benchmark: | ||
$ tv4: 44122 operations/second. (0.0226644ms) | ||
$ joi: 111245 operations/second. (0.0089892ms) | ||
``` | ||
You can run a benchmark against `tv4` by running `npm run bench`. |
117
10045
6
95