Comparing version 0.2.3 to 1.0.0
@@ -7,7 +7,16 @@ 'use strict'; | ||
module.exports = function enjoi(schema, subSchemas) { | ||
module.exports = function enjoi(schema, options) { | ||
var subSchemas, types; | ||
options = options || {}; | ||
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(options), 'Expected options to be an object.'); | ||
subSchemas = options.subSchemas; | ||
types = options.types; | ||
Assert.ok(!subSchemas || Thing.isObject(subSchemas), 'Expected options.subSchemas to be an object.'); | ||
Assert.ok(!types || Thing.isObject(types), 'Expected options.types to be an object.'); | ||
function resolve(current) { | ||
@@ -26,5 +35,4 @@ if (current.type) { | ||
if (current.$ref) { | ||
return resolve(refresolver(current.$ref)); | ||
return resolve(resolveref(current.$ref)); | ||
} | ||
@@ -41,3 +49,3 @@ | ||
function refresolver(value) { | ||
function resolveref(value) { | ||
var id, refschema, path, fragment, paths; | ||
@@ -87,2 +95,6 @@ | ||
break; | ||
default: | ||
if (types) { | ||
joischema = types[current.type]; | ||
} | ||
} | ||
@@ -147,3 +159,3 @@ | ||
joischema = joischema.includes(resolve(current.items)); | ||
joischema = joischema.items(resolve(current.items)); | ||
@@ -175,2 +187,3 @@ Thing.isNumber(current.minItems) && (joischema = joischema.min(current.minItems)); | ||
var joischema = Joi.string(); | ||
current.pattern && (joischema = joischema.regex(new RegExp(current.pattern))); | ||
@@ -177,0 +190,0 @@ |
{ | ||
"name": "enjoi", | ||
"version": "0.2.3", | ||
"version": "1.0.0", | ||
"description": "Converts json-schema to Joi schema for validation.", | ||
@@ -33,3 +33,3 @@ "main": "lib/enjoi.js", | ||
"core-util-is": "^1.0.1", | ||
"joi": "^4.7.0" | ||
"joi": "^6.4.3" | ||
}, | ||
@@ -36,0 +36,0 @@ "devDependencies": { |
@@ -13,4 +13,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) | ||
- `allOf` | ||
- `anyOf` | ||
- `oneOf` | ||
@@ -26,5 +24,7 @@ - `not` | ||
- `enjoi(schema [, subSchemas])` | ||
- `enjoi(schema [, options])` | ||
- `schema` - a JSON schema. | ||
- `subSchemas` - an (optional) object with keys representing schema ids, and values representing schemas. | ||
- `options` - an (optional) object of additional options such as `subSchemas` and custom `types`. | ||
- `subSchemas` - an (optional) object with keys representing schema ids, and values representing schemas. | ||
- `types` - an (optional) object with keys representing type names and values representing a Joi schema. | ||
@@ -34,6 +34,6 @@ Example: | ||
```javascript | ||
var Joi = require('joi'), | ||
enjoi = require('enjoi'); | ||
var Joi = require('joi'); | ||
var Enjoi = require('enjoi'); | ||
var schema = enjoi({ | ||
var schema = Enjoi({ | ||
'title': 'Example Schema', | ||
@@ -75,3 +75,3 @@ 'type': 'object', | ||
```javascript | ||
var schema = enjoi({ | ||
var schema = Enjoi({ | ||
'title': 'Example Schema', | ||
@@ -85,5 +85,7 @@ 'type': 'object', | ||
}, { | ||
'sub': { | ||
'something': { | ||
'type': 'string' | ||
subSchemas: { | ||
'sub': { | ||
'something': { | ||
'type': 'string' | ||
} | ||
} | ||
@@ -94,7 +96,27 @@ } | ||
### Performance | ||
### Custom Types | ||
Joi's validation is very fast. `enjoi` is meant to be used to prepare a schema in advance of | ||
validation; not alongside validation. | ||
```javascript | ||
var schema = Enjoi({ | ||
type: 'file' | ||
}, { | ||
types: { | ||
file: Enjoi({ | ||
type: 'object', | ||
properties: { | ||
file: { | ||
type: 'string' | ||
}, | ||
consumes: { | ||
type: 'string', | ||
pattern: /multipart\/form-data/ | ||
} | ||
} | ||
}) | ||
} | ||
}); | ||
You can run a benchmark against `tv4` by running `npm run bench`. | ||
schema.validate({file: 'data', consumes: 'multipart/form-data'}, function (error, value) { | ||
error && console.log(error); | ||
}); | ||
``` |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12975
7
179
0
117
+ Addedjoi@6.10.1(transitive)
- Removedjoi@4.9.0(transitive)
Updatedjoi@^6.4.3