08-27-周三_17-09-29
This commit is contained in:
21
node_modules/is-my-json-valid/LICENSE
generated
vendored
Normal file
21
node_modules/is-my-json-valid/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Mathias Buus
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
263
node_modules/is-my-json-valid/README.md
generated
vendored
Normal file
263
node_modules/is-my-json-valid/README.md
generated
vendored
Normal file
@@ -0,0 +1,263 @@
|
||||
# is-my-json-valid
|
||||
|
||||
A [JSONSchema](https://json-schema.org/) validator that uses code generation to be extremely fast.
|
||||
|
||||
It passes the entire JSONSchema v4 test suite except for `remoteRefs` and `maxLength`/`minLength` when using unicode surrogate pairs.
|
||||
|
||||
[](https://travis-ci.org/mafintosh/is-my-json-valid)
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install --save is-my-json-valid
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Simply pass a schema to compile it
|
||||
|
||||
```js
|
||||
var validator = require('is-my-json-valid')
|
||||
|
||||
var validate = validator({
|
||||
required: true,
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
console.log('should be valid', validate({hello: 'world'}))
|
||||
console.log('should not be valid', validate({}))
|
||||
|
||||
// get the last list of errors by checking validate.errors
|
||||
// the following will print [{field: 'data.hello', message: 'is required'}]
|
||||
console.log(validate.errors)
|
||||
```
|
||||
|
||||
You can also pass the schema as a string
|
||||
|
||||
```js
|
||||
var validate = validator('{"type": ... }')
|
||||
```
|
||||
|
||||
Optionally you can use the require submodule to load a schema from `__dirname`
|
||||
|
||||
```js
|
||||
var validator = require('is-my-json-valid/require')
|
||||
var validate = validator('my-schema.json')
|
||||
```
|
||||
|
||||
## Custom formats
|
||||
|
||||
is-my-json-valid supports the formats specified in JSON schema v4 (such as date-time).
|
||||
If you want to add your own custom formats pass them as the formats options to the validator
|
||||
|
||||
```js
|
||||
var validate = validator({
|
||||
type: 'string',
|
||||
required: true,
|
||||
format: 'only-a'
|
||||
}, {
|
||||
formats: {
|
||||
'only-a': /^a+$/
|
||||
}
|
||||
})
|
||||
|
||||
console.log(validate('aa')) // true
|
||||
console.log(validate('ab')) // false
|
||||
```
|
||||
|
||||
## External schemas
|
||||
|
||||
You can pass in external schemas that you reference using the `$ref` attribute as the `schemas` option
|
||||
|
||||
```js
|
||||
var ext = {
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
|
||||
var schema = {
|
||||
$ref: '#ext' // references another schema called ext
|
||||
}
|
||||
|
||||
// pass the external schemas as an option
|
||||
var validate = validator(schema, {schemas: {ext: ext}})
|
||||
|
||||
validate('hello') // returns true
|
||||
validate(42) // return false
|
||||
```
|
||||
|
||||
## Filtering away additional properties
|
||||
|
||||
is-my-json-valid supports filtering away properties not in the schema
|
||||
|
||||
```js
|
||||
var filter = validator.filter({
|
||||
required: true,
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {type: 'string', required: true}
|
||||
},
|
||||
additionalProperties: false
|
||||
})
|
||||
|
||||
var doc = {hello: 'world', notInSchema: true}
|
||||
console.log(filter(doc)) // {hello: 'world'}
|
||||
```
|
||||
|
||||
## Verbose mode shows more information about the source of the error
|
||||
|
||||
When the `verbose` options is set to `true`, `is-my-json-valid` also outputs:
|
||||
|
||||
- `value`: The data value that caused the error
|
||||
- `schemaPath`: an array of keys indicating which sub-schema failed
|
||||
|
||||
```js
|
||||
var schema = {
|
||||
required: true,
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
var validate = validator(schema, {
|
||||
verbose: true
|
||||
})
|
||||
|
||||
validate({hello: 100});
|
||||
console.log(validate.errors)
|
||||
// [ { field: 'data.hello',
|
||||
// message: 'is the wrong type',
|
||||
// value: 100,
|
||||
// type: 'string',
|
||||
// schemaPath: [ 'properties', 'hello' ] } ]
|
||||
```
|
||||
|
||||
Many popular libraries make it easy to retrieve the failing rule with the `schemaPath`:
|
||||
|
||||
```js
|
||||
var schemaPath = validate.errors[0].schemaPath
|
||||
var R = require('ramda')
|
||||
|
||||
console.log( 'All evaluate to the same thing: ', R.equals(
|
||||
schema.properties.hello,
|
||||
{ required: true, type: 'string' },
|
||||
R.path(schemaPath, schema),
|
||||
require('lodash').get(schema, schemaPath),
|
||||
require('jsonpointer').get(schema, [""].concat(schemaPath))
|
||||
))
|
||||
// All evaluate to the same thing: true
|
||||
```
|
||||
|
||||
## Greedy mode tries to validate as much as possible
|
||||
|
||||
By default is-my-json-valid bails on first validation error but when greedy is
|
||||
set to true it tries to validate as much as possible:
|
||||
|
||||
```js
|
||||
var validate = validator({
|
||||
type: 'object',
|
||||
properties: {
|
||||
x: {
|
||||
type: 'number'
|
||||
}
|
||||
},
|
||||
required: ['x', 'y']
|
||||
}, {
|
||||
greedy: true
|
||||
});
|
||||
|
||||
validate({x: 'string'});
|
||||
console.log(validate.errors) // [{field: 'data.y', message: 'is required'},
|
||||
// {field: 'data.x', message: 'is the wrong type'}]
|
||||
```
|
||||
|
||||
## Error messages
|
||||
|
||||
Here is a list of possible `message` values for errors:
|
||||
|
||||
- `is required`
|
||||
- `is the wrong type`
|
||||
- `has additional items`
|
||||
- `must be FORMAT format` (FORMAT is the `format` property from the schema)
|
||||
- `must be unique`
|
||||
- `must be an enum value`
|
||||
- `dependencies not set`
|
||||
- `has additional properties`
|
||||
- `referenced schema does not match`
|
||||
- `negative schema matches`
|
||||
- `pattern mismatch`
|
||||
- `no schemas match`
|
||||
- `no (or more than one) schemas match`
|
||||
- `has a remainder`
|
||||
- `has more properties than allowed`
|
||||
- `has less properties than allowed`
|
||||
- `has more items than allowed`
|
||||
- `has less items than allowed`
|
||||
- `has longer length than allowed`
|
||||
- `has less length than allowed`
|
||||
- `is less than minimum`
|
||||
- `is more than maximum`
|
||||
|
||||
## Performance
|
||||
|
||||
is-my-json-valid uses code generation to turn your JSON schema into basic javascript code that is easily optimizeable by v8.
|
||||
|
||||
At the time of writing, is-my-json-valid is the **fastest validator** when running
|
||||
|
||||
- [json-schema-benchmark](https://github.com/Muscula/json-schema-benchmark)
|
||||
- [cosmicreals.com benchmark](http://cosmicrealms.com/blog/2014/08/29/benchmark-of-node-dot-js-json-validation-modules-part-3/)
|
||||
- [jsck benchmark](https://github.com/pandastrike/jsck/issues/72#issuecomment-70992684)
|
||||
- [themis benchmark](https://cdn.rawgit.com/playlyfe/themis/master/benchmark/results.html)
|
||||
- [z-schema benchmark](https://rawgit.com/zaggino/z-schema/master/benchmark/results.html)
|
||||
|
||||
If you know any other relevant benchmarks open a PR and I'll add them.
|
||||
|
||||
## TypeScript support
|
||||
|
||||
This library ships with TypeScript typings. They are still early on and not perfect at the moment, but should hopefully handle the most common cases. If you find anything that doesn't work, please open an issue and we'll try to solve it.
|
||||
|
||||
The typings are using `unknown` and thus require TypeScript 3.0 or later.
|
||||
|
||||
Here is a quick sample of usage together with express:
|
||||
|
||||
```typescript
|
||||
import createError = require('http-errors')
|
||||
import createValidator = require('is-my-json-valid')
|
||||
import { Request, Response, NextFunction } from 'express'
|
||||
|
||||
const personValidator = createValidator({
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
age: { type: 'number' },
|
||||
},
|
||||
required: [
|
||||
'name'
|
||||
]
|
||||
})
|
||||
|
||||
export function post (req: Request, res: Response, next: NextFunction) {
|
||||
// Here req.body is typed as: any
|
||||
|
||||
if (!personValidator(req.body)) {
|
||||
throw createError(400, { errors: personValidator.errors })
|
||||
}
|
||||
|
||||
// Here req.body is typed as: { name: string, age: number | undefined }
|
||||
}
|
||||
```
|
||||
|
||||
As you can see, the typings for is-my-json-valid will contruct an interface from the schema passed in. This allows you to work with your incoming json body in a type safe way.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
40
node_modules/is-my-json-valid/formats.js
generated
vendored
Normal file
40
node_modules/is-my-json-valid/formats.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
var createIpValidator = require('is-my-ip-valid')
|
||||
|
||||
var reEmailWhitespace = /\s/
|
||||
var reHostnameFirstPass = /^[a-zA-Z0-9.-]+$/
|
||||
var reHostnamePart = /^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9])$/
|
||||
var rePhoneFirstPass = /^\+[0-9][0-9 ]{5,27}[0-9]$/
|
||||
var rePhoneDoubleSpace = / {2}/
|
||||
var rePhoneGlobalSpace = / /g
|
||||
|
||||
exports['date-time'] = /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}[tT ]\d{2}:\d{2}:\d{2}(?:\.\d+|)([zZ]|[+-]\d{2}:\d{2})$/
|
||||
exports['date'] = /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}$/
|
||||
exports['time'] = /^\d{2}:\d{2}:\d{2}$/
|
||||
exports['email'] = function (input) { return (input.indexOf('@') !== -1) && (!reEmailWhitespace.test(input)) }
|
||||
exports['ip-address'] = exports['ipv4'] = createIpValidator({ version: 4 })
|
||||
exports['ipv6'] = createIpValidator({ version: 6 })
|
||||
exports['uri'] = /^[a-zA-Z][a-zA-Z0-9+\-.]*:[^\s]*$/
|
||||
exports['color'] = /(#?([0-9A-Fa-f]{3,6})\b)|(aqua)|(black)|(blue)|(fuchsia)|(gray)|(green)|(lime)|(maroon)|(navy)|(olive)|(orange)|(purple)|(red)|(silver)|(teal)|(white)|(yellow)|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\))/
|
||||
exports['hostname'] = function (input) {
|
||||
if (!(reHostnameFirstPass.test(input))) return false
|
||||
|
||||
var parts = input.split('.')
|
||||
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
if (!(reHostnamePart.test(parts[i]))) return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
exports['alpha'] = /^[a-zA-Z]+$/
|
||||
exports['alphanumeric'] = /^[a-zA-Z0-9]+$/
|
||||
exports['style'] = /.:\s*[^;]/g
|
||||
exports['phone'] = function (input) {
|
||||
if (!(rePhoneFirstPass.test(input))) return false
|
||||
if (rePhoneDoubleSpace.test(input)) return false
|
||||
|
||||
var digits = input.substring(1).replace(rePhoneGlobalSpace, '').length
|
||||
|
||||
return (digits >= 7 && digits <= 15)
|
||||
}
|
||||
exports['utc-millisec'] = /^[0-9]{1,15}\.?[0-9]{0,15}$/
|
127
node_modules/is-my-json-valid/index.d.ts
generated
vendored
Normal file
127
node_modules/is-my-json-valid/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
type AnySchema = NullSchema | BooleanSchema | NullableBooleanSchema | NumberSchema | NullableNumberSchema | StringSchema | NullableStringSchema | AnyEnumSchema | AnyArraySchema | AnyNullableArraySchema | AnyObjectSchema | AnyNullableObjectSchema | AnyAllOptionalObjectSchema | AnyNullableAllOptionalObjectSchema | AnyOneOfSchema
|
||||
type StringKeys<T> = (keyof T) & string
|
||||
|
||||
interface NullSchema { type: 'null' }
|
||||
|
||||
interface BooleanSchema { type: 'boolean' }
|
||||
interface NullableBooleanSchema { type: ('boolean' | 'null')[] }
|
||||
|
||||
interface NumberSchema { type: 'number' }
|
||||
interface NullableNumberSchema { type: ('number' | 'null')[] }
|
||||
|
||||
interface StringSchema { type: 'string' }
|
||||
interface NullableStringSchema { type: ('string' | 'null')[] }
|
||||
|
||||
interface AnyEnumSchema extends EnumSchema<any> {}
|
||||
interface EnumSchema<Enum> { enum: Enum[] }
|
||||
|
||||
interface AnyArraySchema extends ArraySchema<AnySchema> {}
|
||||
interface ArraySchema<ItemSchema extends AnySchema> { type: 'array', items: ItemSchema }
|
||||
|
||||
interface AnyNullableArraySchema extends NullableArraySchema<AnySchema> {}
|
||||
interface NullableArraySchema<ItemSchema extends AnySchema> { type: ('array' | 'null')[], items: ItemSchema }
|
||||
|
||||
interface AnyObjectSchema extends ObjectSchema<Record<string, AnySchema>, string> {}
|
||||
interface ObjectSchema<Properties extends Record<string, AnySchema>, Required extends StringKeys<Properties>> {
|
||||
additionalProperties?: boolean
|
||||
type: 'object'
|
||||
properties: Properties
|
||||
required: Required[]
|
||||
}
|
||||
|
||||
interface AnyNullableObjectSchema extends NullableObjectSchema<Record<string, AnySchema>, string> {}
|
||||
interface NullableObjectSchema<Properties extends Record<string, AnySchema>, Required extends StringKeys<Properties>> {
|
||||
additionalProperties?: boolean
|
||||
type: ('object' | 'null')[]
|
||||
properties: Properties
|
||||
required: Required[]
|
||||
}
|
||||
|
||||
interface AnyAllOptionalObjectSchema extends AllOptionalObjectSchema<Record<string, AnySchema>> {}
|
||||
interface AllOptionalObjectSchema<Properties extends Record<string, AnySchema>> {
|
||||
additionalProperties?: boolean
|
||||
type: 'object'
|
||||
properties: Properties
|
||||
}
|
||||
|
||||
interface AnyNullableAllOptionalObjectSchema extends NullableAllOptionalObjectSchema<Record<string, AnySchema>> {}
|
||||
interface NullableAllOptionalObjectSchema<Properties extends Record<string, AnySchema>> {
|
||||
additionalProperties?: boolean
|
||||
type: ('object' | 'null')[]
|
||||
properties: Properties
|
||||
}
|
||||
|
||||
interface AnyOneOfSchema { oneOf: AnySchema[] }
|
||||
|
||||
interface ArrayFromSchema<ItemSchema extends AnySchema> extends Array<TypeFromSchema<ItemSchema>> {}
|
||||
|
||||
type ObjectFromSchema<Properties extends Record<string, AnySchema>, Required extends StringKeys<Properties>> = {
|
||||
[Key in keyof Properties]: (Key extends Required ? TypeFromSchema<Properties[Key]> : TypeFromSchema<Properties[Key]> | undefined)
|
||||
}
|
||||
|
||||
type TypeFromSchema<Schema extends AnySchema> = (
|
||||
Schema extends EnumSchema<infer Enum> ? Enum
|
||||
: Schema extends NullSchema ? null
|
||||
: Schema extends BooleanSchema ? boolean
|
||||
: Schema extends NullableBooleanSchema ? (boolean | null)
|
||||
: Schema extends NumberSchema ? number
|
||||
: Schema extends NullableNumberSchema ? (number | null)
|
||||
: Schema extends StringSchema ? string
|
||||
: Schema extends NullableStringSchema ? (string | null)
|
||||
: Schema extends ArraySchema<infer ItemSchema> ? ArrayFromSchema<ItemSchema>
|
||||
: Schema extends NullableArraySchema<infer ItemSchema> ? (ArrayFromSchema<ItemSchema> | null)
|
||||
: Schema extends ObjectSchema<infer Properties, infer Required> ? ObjectFromSchema<Properties, Required>
|
||||
: Schema extends NullableObjectSchema<infer Properties, infer Required> ? (ObjectFromSchema<Properties, Required> | null)
|
||||
: Schema extends AllOptionalObjectSchema<infer Properties> ? ObjectFromSchema<Properties, never>
|
||||
: Schema extends NullableAllOptionalObjectSchema<infer Properties> ? (ObjectFromSchema<Properties, never> | null)
|
||||
: never
|
||||
)
|
||||
|
||||
declare namespace factory {
|
||||
interface ValidationError {
|
||||
field: string
|
||||
message: string
|
||||
value: unknown
|
||||
type: string
|
||||
}
|
||||
}
|
||||
|
||||
interface Validator<Schema extends AnySchema, Output = TypeFromSchema<Schema>> {
|
||||
(input: unknown, options?: any): input is Output
|
||||
errors: factory.ValidationError[]
|
||||
toJSON(): Schema
|
||||
}
|
||||
|
||||
interface Filter<Schema extends AnySchema, Output = TypeFromSchema<Schema>> {
|
||||
(input: Output, options?: any): Output
|
||||
}
|
||||
|
||||
interface Factory {
|
||||
/* One of object schema */
|
||||
<Properties1 extends Record<string, AnySchema>, Required1 extends StringKeys<Properties1>, Properties2 extends Record<string, AnySchema>, Required2 extends StringKeys<Properties2>> (schema: { oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>] }, options?: any): Validator<{ oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>] }, ObjectFromSchema<Properties1, Required1> | ObjectFromSchema<Properties2, Required2>>
|
||||
createFilter<Properties1 extends Record<string, AnySchema>, Required1 extends StringKeys<Properties1>, Properties2 extends Record<string, AnySchema>, Required2 extends StringKeys<Properties2>> (schema: { oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>] }, options?: any): Filter<{ oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>] }, ObjectFromSchema<Properties1, Required1> | ObjectFromSchema<Properties2, Required2>>
|
||||
<Properties1 extends Record<string, AnySchema>, Required1 extends StringKeys<Properties1>, Properties2 extends Record<string, AnySchema>, Required2 extends StringKeys<Properties2>, Properties3 extends Record<string, AnySchema>, Required3 extends StringKeys<Properties3>> (schema: { oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>, ObjectSchema<Properties3, Required3>] }, options?: any): Validator<{ oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>, ObjectSchema<Properties3, Required3>] }, ObjectFromSchema<Properties1, Required1> | ObjectFromSchema<Properties2, Required2> | ObjectFromSchema<Properties3, Required3>>
|
||||
createFilter<Properties1 extends Record<string, AnySchema>, Required1 extends StringKeys<Properties1>, Properties2 extends Record<string, AnySchema>, Required2 extends StringKeys<Properties2>, Properties3 extends Record<string, AnySchema>, Required3 extends StringKeys<Properties3>> (schema: { oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>, ObjectSchema<Properties3, Required3>] }, options?: any): Filter<{ oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>, ObjectSchema<Properties3, Required3>] }, ObjectFromSchema<Properties1, Required1> | ObjectFromSchema<Properties2, Required2> | ObjectFromSchema<Properties3, Required3>>
|
||||
<Properties1 extends Record<string, AnySchema>, Required1 extends StringKeys<Properties1>, Properties2 extends Record<string, AnySchema>, Required2 extends StringKeys<Properties2>, Properties3 extends Record<string, AnySchema>, Required3 extends StringKeys<Properties3>, Properties4 extends Record<string, AnySchema>, Required4 extends StringKeys<Properties4>> (schema: { oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>, ObjectSchema<Properties3, Required3>, ObjectSchema<Properties4, Required4>] }, options?: any): Validator<{ oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>, ObjectSchema<Properties3, Required3>, ObjectSchema<Properties4, Required4>] }, ObjectFromSchema<Properties1, Required1> | ObjectFromSchema<Properties2, Required2> | ObjectFromSchema<Properties3, Required3> | ObjectFromSchema<Properties4, Required4>>
|
||||
createFilter<Properties1 extends Record<string, AnySchema>, Required1 extends StringKeys<Properties1>, Properties2 extends Record<string, AnySchema>, Required2 extends StringKeys<Properties2>, Properties3 extends Record<string, AnySchema>, Required3 extends StringKeys<Properties3>, Properties4 extends Record<string, AnySchema>, Required4 extends StringKeys<Properties4>> (schema: { oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>, ObjectSchema<Properties3, Required3>, ObjectSchema<Properties4, Required4>] }, options?: any): Filter<{ oneOf: [ObjectSchema<Properties1, Required1>, ObjectSchema<Properties2, Required2>, ObjectSchema<Properties3, Required3>, ObjectSchema<Properties4, Required4>] }, ObjectFromSchema<Properties1, Required1> | ObjectFromSchema<Properties2, Required2> | ObjectFromSchema<Properties3, Required3> | ObjectFromSchema<Properties4, Required4>>
|
||||
|
||||
/* One of plain schema */
|
||||
<Schema1 extends AnySchema, Schema2 extends AnySchema> (schema: { oneOf: [Schema1, Schema2] }, options?: any): Validator<{ oneOf: [Schema1, Schema2] }, TypeFromSchema<Schema1> | TypeFromSchema<Schema2>>
|
||||
createFilter<Schema1 extends AnySchema, Schema2 extends AnySchema> (schema: { oneOf: [Schema1, Schema2] }, options?: any): Filter<{ oneOf: [Schema1, Schema2] }, TypeFromSchema<Schema1> | TypeFromSchema<Schema2>>
|
||||
<Schema1 extends AnySchema, Schema2 extends AnySchema, Schema3 extends AnySchema> (schema: { oneOf: [Schema1, Schema2, Schema3] }, options?: any): Validator<{ oneOf: [Schema1, Schema2, Schema3] }, TypeFromSchema<Schema1> | TypeFromSchema<Schema2> | TypeFromSchema<Schema3>>
|
||||
createFilter<Schema1 extends AnySchema, Schema2 extends AnySchema, Schema3 extends AnySchema> (schema: { oneOf: [Schema1, Schema2, Schema3] }, options?: any): Filter<{ oneOf: [Schema1, Schema2, Schema3] }, TypeFromSchema<Schema1> | TypeFromSchema<Schema2> | TypeFromSchema<Schema3>>
|
||||
<Schema1 extends AnySchema, Schema2 extends AnySchema, Schema3 extends AnySchema, Schema4 extends AnySchema> (schema: { oneOf: [Schema1, Schema2, Schema3, Schema4] }, options?: any): Validator<{ oneOf: [Schema1, Schema2, Schema3, Schema4] }, TypeFromSchema<Schema1> | TypeFromSchema<Schema2> | TypeFromSchema<Schema3> | TypeFromSchema<Schema4>>
|
||||
createFilter<Schema1 extends AnySchema, Schema2 extends AnySchema, Schema3 extends AnySchema, Schema4 extends AnySchema> (schema: { oneOf: [Schema1, Schema2, Schema3, Schema4] }, options?: any): Filter<{ oneOf: [Schema1, Schema2, Schema3, Schema4] }, TypeFromSchema<Schema1> | TypeFromSchema<Schema2> | TypeFromSchema<Schema3> | TypeFromSchema<Schema4>>
|
||||
|
||||
/* Object schema */
|
||||
<Properties extends Record<string, AnySchema>, Required extends StringKeys<Properties>> (schema: ObjectSchema<Properties, Required>, options?: any): Validator<ObjectSchema<Properties, Required>>
|
||||
createFilter<Properties extends Record<string, AnySchema>, Required extends StringKeys<Properties>> (schema: ObjectSchema<Properties, Required>, options?: any): Filter<ObjectSchema<Properties, Required>>
|
||||
|
||||
/* Plain schema */
|
||||
<Schema extends AnySchema> (schema: Schema, options?: any): Validator<Schema>
|
||||
createFilter<Schema extends AnySchema> (schema: Schema, options?: any): Filter<Schema>
|
||||
}
|
||||
|
||||
declare const factory: Factory
|
||||
|
||||
export = factory
|
623
node_modules/is-my-json-valid/index.js
generated
vendored
Normal file
623
node_modules/is-my-json-valid/index.js
generated
vendored
Normal file
@@ -0,0 +1,623 @@
|
||||
var genobj = require('generate-object-property')
|
||||
var genfun = require('generate-function')
|
||||
var jsonpointer = require('jsonpointer')
|
||||
var xtend = require('xtend')
|
||||
var formats = require('./formats')
|
||||
|
||||
var get = function(obj, additionalSchemas, ptr) {
|
||||
|
||||
var visit = function(sub) {
|
||||
if (sub && sub.id === ptr) return sub
|
||||
if (typeof sub !== 'object' || !sub) return null
|
||||
return Object.keys(sub).reduce(function(res, k) {
|
||||
return res || visit(sub[k])
|
||||
}, null)
|
||||
}
|
||||
|
||||
var res = visit(obj)
|
||||
if (res) return res
|
||||
|
||||
ptr = ptr.replace(/^#/, '')
|
||||
ptr = ptr.replace(/\/$/, '')
|
||||
|
||||
try {
|
||||
return jsonpointer.get(obj, decodeURI(ptr))
|
||||
} catch (err) {
|
||||
var end = ptr.indexOf('#')
|
||||
var other
|
||||
// external reference
|
||||
if (end !== 0) {
|
||||
// fragment doesn't exist.
|
||||
if (end === -1) {
|
||||
other = additionalSchemas[ptr]
|
||||
} else {
|
||||
var ext = ptr.slice(0, end)
|
||||
other = additionalSchemas[ext]
|
||||
var fragment = ptr.slice(end).replace(/^#/, '')
|
||||
try {
|
||||
return jsonpointer.get(other, fragment)
|
||||
} catch (err) {}
|
||||
}
|
||||
} else {
|
||||
other = additionalSchemas[ptr]
|
||||
}
|
||||
return other || null
|
||||
}
|
||||
}
|
||||
|
||||
var types = {}
|
||||
|
||||
types.any = function() {
|
||||
return 'true'
|
||||
}
|
||||
|
||||
types.null = function(name) {
|
||||
return name+' === null'
|
||||
}
|
||||
|
||||
types.boolean = function(name) {
|
||||
return 'typeof '+name+' === "boolean"'
|
||||
}
|
||||
|
||||
types.array = function(name) {
|
||||
return 'Array.isArray('+name+')'
|
||||
}
|
||||
|
||||
types.object = function(name) {
|
||||
return 'typeof '+name+' === "object" && '+name+' && !Array.isArray('+name+')'
|
||||
}
|
||||
|
||||
types.number = function(name) {
|
||||
return 'typeof '+name+' === "number" && isFinite('+name+')'
|
||||
}
|
||||
|
||||
types.integer = function(name) {
|
||||
return 'typeof '+name+' === "number" && (Math.floor('+name+') === '+name+' || '+name+' > 9007199254740992 || '+name+' < -9007199254740992)'
|
||||
}
|
||||
|
||||
types.string = function(name) {
|
||||
return 'typeof '+name+' === "string"'
|
||||
}
|
||||
|
||||
var unique = function(array, len) {
|
||||
len = Math.min(len === -1 ? array.length : len, array.length)
|
||||
var list = []
|
||||
for (var i = 0; i < len; i++) {
|
||||
list.push(typeof array[i] === 'object' ? JSON.stringify(array[i]) : array[i])
|
||||
}
|
||||
for (var i = 1; i < list.length; i++) {
|
||||
if (list.indexOf(list[i]) !== i) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var isMultipleOf = function(name, multipleOf) {
|
||||
var res;
|
||||
var factor = ((multipleOf | 0) !== multipleOf) ? Math.pow(10, multipleOf.toString().split('.').pop().length) : 1
|
||||
if (factor > 1) {
|
||||
var factorName = ((name | 0) !== name) ? Math.pow(10, name.toString().split('.').pop().length) : 1
|
||||
if (factorName > factor) res = true
|
||||
else res = Math.round(factor * name) % (factor * multipleOf)
|
||||
}
|
||||
else res = name % multipleOf;
|
||||
return !res;
|
||||
}
|
||||
|
||||
var testLimitedRegex = function (r, s, maxLength) {
|
||||
if (maxLength > -1 && s.length > maxLength) return true
|
||||
return r.test(s)
|
||||
}
|
||||
|
||||
var compile = function(schema, cache, root, reporter, opts) {
|
||||
var fmts = opts ? xtend(formats, opts.formats) : formats
|
||||
var scope = {unique:unique, formats:fmts, isMultipleOf:isMultipleOf, testLimitedRegex:testLimitedRegex}
|
||||
var verbose = opts ? !!opts.verbose : false;
|
||||
var greedy = opts && opts.greedy !== undefined ?
|
||||
opts.greedy : false;
|
||||
|
||||
var syms = {}
|
||||
var allocated = []
|
||||
var gensym = function(name) {
|
||||
var res = name+(syms[name] = (syms[name] || 0)+1)
|
||||
allocated.push(res)
|
||||
return res
|
||||
}
|
||||
|
||||
var formatName = function(field) {
|
||||
var s = JSON.stringify(field)
|
||||
try {
|
||||
var pattern = /\[([^\[\]"]+)\]/
|
||||
while (pattern.test(s)) s = s.replace(pattern, replacer)
|
||||
return s
|
||||
} catch (_) {
|
||||
return JSON.stringify(field)
|
||||
}
|
||||
|
||||
function replacer (match, v) {
|
||||
if (allocated.indexOf(v) === -1) throw new Error('Unreplaceable')
|
||||
return '." + ' + v + ' + "'
|
||||
}
|
||||
}
|
||||
|
||||
var reversePatterns = {}
|
||||
var patterns = function(p) {
|
||||
if (reversePatterns[p]) return reversePatterns[p]
|
||||
var n = gensym('pattern')
|
||||
scope[n] = new RegExp(p)
|
||||
reversePatterns[p] = n
|
||||
return n
|
||||
}
|
||||
|
||||
var vars = ['i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','y','z']
|
||||
var genloop = function() {
|
||||
var v = vars.shift()
|
||||
vars.push(v+v[0])
|
||||
allocated.push(v)
|
||||
return v
|
||||
}
|
||||
|
||||
var visit = function(name, node, reporter, filter, schemaPath) {
|
||||
var properties = node.properties
|
||||
var type = node.type
|
||||
var tuple = false
|
||||
|
||||
if (Array.isArray(node.items)) { // tuple type
|
||||
properties = {}
|
||||
node.items.forEach(function(item, i) {
|
||||
properties[i] = item
|
||||
})
|
||||
type = 'array'
|
||||
tuple = true
|
||||
}
|
||||
|
||||
var indent = 0
|
||||
var error = function(msg, prop, value) {
|
||||
validate('errors++')
|
||||
if (reporter === true) {
|
||||
validate('if (validate.errors === null) validate.errors = []')
|
||||
if (verbose) {
|
||||
validate(
|
||||
'validate.errors.push({field:%s,message:%s,value:%s,type:%s,schemaPath:%s})',
|
||||
formatName(prop || name),
|
||||
JSON.stringify(msg),
|
||||
value || name,
|
||||
JSON.stringify(type),
|
||||
JSON.stringify(schemaPath)
|
||||
)
|
||||
} else {
|
||||
validate('validate.errors.push({field:%s,message:%s})', formatName(prop || name), JSON.stringify(msg))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (node.required === true) {
|
||||
indent++
|
||||
validate('if (%s === undefined) {', name)
|
||||
error('is required')
|
||||
validate('} else {')
|
||||
} else {
|
||||
indent++
|
||||
validate('if (%s !== undefined) {', name)
|
||||
}
|
||||
|
||||
var valid = [].concat(type)
|
||||
.map(function(t) {
|
||||
if (t && !types.hasOwnProperty(t)) {
|
||||
throw new Error('Unknown type: ' + t)
|
||||
}
|
||||
|
||||
return types[t || 'any'](name)
|
||||
})
|
||||
.join(' || ') || 'true'
|
||||
|
||||
if (valid !== 'true') {
|
||||
indent++
|
||||
validate('if (!(%s)) {', valid)
|
||||
error('is the wrong type')
|
||||
validate('} else {')
|
||||
}
|
||||
|
||||
if (tuple) {
|
||||
if (node.additionalItems === false) {
|
||||
validate('if (%s.length > %d) {', name, node.items.length)
|
||||
error('has additional items')
|
||||
validate('}')
|
||||
} else if (node.additionalItems) {
|
||||
var i = genloop()
|
||||
validate('for (var %s = %d; %s < %s.length; %s++) {', i, node.items.length, i, name, i)
|
||||
visit(name+'['+i+']', node.additionalItems, reporter, filter, schemaPath.concat('additionalItems'))
|
||||
validate('}')
|
||||
}
|
||||
}
|
||||
|
||||
if (node.format && fmts[node.format]) {
|
||||
if (type !== 'string' && formats[node.format]) validate('if (%s) {', types.string(name))
|
||||
var n = gensym('format')
|
||||
scope[n] = fmts[node.format]
|
||||
|
||||
if (typeof scope[n] === 'function') validate('if (!%s(%s)) {', n, name)
|
||||
else validate('if (!testLimitedRegex(%s, %s, %d)) {', n, name, typeof node.maxLength === 'undefined' ? -1 : node.maxLength)
|
||||
error('must be '+node.format+' format')
|
||||
validate('}')
|
||||
if (type !== 'string' && formats[node.format]) validate('}')
|
||||
}
|
||||
|
||||
if (Array.isArray(node.required)) {
|
||||
var n = gensym('missing')
|
||||
validate('var %s = 0', n)
|
||||
var checkRequired = function (req) {
|
||||
var prop = genobj(name, req);
|
||||
validate('if (%s === undefined) {', prop)
|
||||
error('is required', prop)
|
||||
validate('%s++', n)
|
||||
validate('}')
|
||||
}
|
||||
validate('if ((%s)) {', type !== 'object' ? types.object(name) : 'true')
|
||||
node.required.map(checkRequired)
|
||||
validate('}');
|
||||
if (!greedy) {
|
||||
validate('if (%s === 0) {', n)
|
||||
indent++
|
||||
}
|
||||
}
|
||||
|
||||
if (node.uniqueItems) {
|
||||
if (type !== 'array') validate('if (%s) {', types.array(name))
|
||||
validate('if (!(unique(%s, %d))) {', name, node.maxItems || -1)
|
||||
error('must be unique')
|
||||
validate('}')
|
||||
if (type !== 'array') validate('}')
|
||||
}
|
||||
|
||||
if (node.enum) {
|
||||
var complex = node.enum.some(function(e) {
|
||||
return typeof e === 'object'
|
||||
})
|
||||
|
||||
var compare = complex ?
|
||||
function(e) {
|
||||
return 'JSON.stringify('+name+')'+' !== JSON.stringify('+JSON.stringify(e)+')'
|
||||
} :
|
||||
function(e) {
|
||||
return name+' !== '+JSON.stringify(e)
|
||||
}
|
||||
|
||||
validate('if (%s) {', node.enum.map(compare).join(' && ') || 'false')
|
||||
error('must be an enum value')
|
||||
validate('}')
|
||||
}
|
||||
|
||||
if (node.dependencies) {
|
||||
if (type !== 'object') validate('if (%s) {', types.object(name))
|
||||
|
||||
Object.keys(node.dependencies).forEach(function(key) {
|
||||
var deps = node.dependencies[key]
|
||||
if (typeof deps === 'string') deps = [deps]
|
||||
|
||||
var exists = function(k) {
|
||||
return genobj(name, k) + ' !== undefined'
|
||||
}
|
||||
|
||||
if (Array.isArray(deps)) {
|
||||
validate('if (%s !== undefined && !(%s)) {', genobj(name, key), deps.map(exists).join(' && ') || 'true')
|
||||
error('dependencies not set')
|
||||
validate('}')
|
||||
}
|
||||
if (typeof deps === 'object') {
|
||||
validate('if (%s !== undefined) {', genobj(name, key))
|
||||
visit(name, deps, reporter, filter, schemaPath.concat(['dependencies', key]))
|
||||
validate('}')
|
||||
}
|
||||
})
|
||||
|
||||
if (type !== 'object') validate('}')
|
||||
}
|
||||
|
||||
if (node.additionalProperties || node.additionalProperties === false) {
|
||||
if (type !== 'object') validate('if (%s) {', types.object(name))
|
||||
|
||||
var i = genloop()
|
||||
var keys = gensym('keys')
|
||||
|
||||
var toCompare = function(p) {
|
||||
return keys+'['+i+'] !== '+JSON.stringify(p)
|
||||
}
|
||||
|
||||
var toTest = function(p) {
|
||||
return '!'+patterns(p)+'.test('+keys+'['+i+'])'
|
||||
}
|
||||
|
||||
var additionalProp = Object.keys(properties || {}).map(toCompare)
|
||||
.concat(Object.keys(node.patternProperties || {}).map(toTest))
|
||||
.join(' && ') || 'true'
|
||||
|
||||
validate('var %s = Object.keys(%s)', keys, name)
|
||||
('for (var %s = 0; %s < %s.length; %s++) {', i, i, keys, i)
|
||||
('if (%s) {', additionalProp)
|
||||
|
||||
if (node.additionalProperties === false) {
|
||||
if (filter) validate('delete %s', name+'['+keys+'['+i+']]')
|
||||
error('has additional properties', null, JSON.stringify(name+'.') + ' + ' + keys + '['+i+']')
|
||||
} else {
|
||||
visit(name+'['+keys+'['+i+']]', node.additionalProperties, reporter, filter, schemaPath.concat(['additionalProperties']))
|
||||
}
|
||||
|
||||
validate
|
||||
('}')
|
||||
('}')
|
||||
|
||||
if (type !== 'object') validate('}')
|
||||
}
|
||||
|
||||
if (node.$ref) {
|
||||
var sub = get(root, opts && opts.schemas || {}, node.$ref)
|
||||
if (sub) {
|
||||
var fn = cache[node.$ref]
|
||||
if (!fn) {
|
||||
cache[node.$ref] = function proxy(data) {
|
||||
return fn(data)
|
||||
}
|
||||
fn = compile(sub, cache, root, false, opts)
|
||||
}
|
||||
var n = gensym('ref')
|
||||
scope[n] = fn
|
||||
validate('if (!(%s(%s))) {', n, name)
|
||||
error('referenced schema does not match')
|
||||
validate('}')
|
||||
}
|
||||
}
|
||||
|
||||
if (node.not) {
|
||||
var prev = gensym('prev')
|
||||
validate('var %s = errors', prev)
|
||||
visit(name, node.not, false, filter, schemaPath.concat('not'))
|
||||
validate('if (%s === errors) {', prev)
|
||||
error('negative schema matches')
|
||||
validate('} else {')
|
||||
('errors = %s', prev)
|
||||
('}')
|
||||
}
|
||||
|
||||
if (node.items && !tuple) {
|
||||
if (type !== 'array') validate('if (%s) {', types.array(name))
|
||||
|
||||
var i = genloop()
|
||||
validate('for (var %s = 0; %s < %s.length; %s++) {', i, i, name, i)
|
||||
visit(name+'['+i+']', node.items, reporter, filter, schemaPath.concat('items'))
|
||||
validate('}')
|
||||
|
||||
if (type !== 'array') validate('}')
|
||||
}
|
||||
|
||||
if (node.patternProperties) {
|
||||
if (type !== 'object') validate('if (%s) {', types.object(name))
|
||||
var keys = gensym('keys')
|
||||
var i = genloop()
|
||||
validate
|
||||
('var %s = Object.keys(%s)', keys, name)
|
||||
('for (var %s = 0; %s < %s.length; %s++) {', i, i, keys, i)
|
||||
|
||||
Object.keys(node.patternProperties).forEach(function(key) {
|
||||
var p = patterns(key)
|
||||
validate('if (%s.test(%s)) {', p, keys+'['+i+']')
|
||||
visit(name+'['+keys+'['+i+']]', node.patternProperties[key], reporter, filter, schemaPath.concat(['patternProperties', key]))
|
||||
validate('}')
|
||||
})
|
||||
|
||||
validate('}')
|
||||
if (type !== 'object') validate('}')
|
||||
}
|
||||
|
||||
if (node.pattern) {
|
||||
var p = patterns(node.pattern)
|
||||
if (type !== 'string') validate('if (%s) {', types.string(name))
|
||||
validate('if (!(testLimitedRegex(%s, %s, %d))) {', p, name, typeof node.maxLength === 'undefined' ? -1 : node.maxLength)
|
||||
error('pattern mismatch')
|
||||
validate('}')
|
||||
if (type !== 'string') validate('}')
|
||||
}
|
||||
|
||||
if (node.allOf) {
|
||||
node.allOf.forEach(function(sch, key) {
|
||||
visit(name, sch, reporter, filter, schemaPath.concat(['allOf', key]))
|
||||
})
|
||||
}
|
||||
|
||||
if (node.anyOf && node.anyOf.length) {
|
||||
var prev = gensym('prev')
|
||||
|
||||
node.anyOf.forEach(function(sch, i) {
|
||||
if (i === 0) {
|
||||
validate('var %s = errors', prev)
|
||||
} else {
|
||||
validate('if (errors !== %s) {', prev)
|
||||
('errors = %s', prev)
|
||||
}
|
||||
visit(name, sch, false, false, schemaPath)
|
||||
})
|
||||
node.anyOf.forEach(function(sch, i) {
|
||||
if (i) validate('}')
|
||||
})
|
||||
validate('if (%s !== errors) {', prev)
|
||||
error('no schemas match')
|
||||
validate('}')
|
||||
}
|
||||
|
||||
if (node.oneOf && node.oneOf.length) {
|
||||
var prev = gensym('prev')
|
||||
var passes = gensym('passes')
|
||||
|
||||
validate
|
||||
('var %s = errors', prev)
|
||||
('var %s = 0', passes)
|
||||
|
||||
node.oneOf.forEach(function(sch, i) {
|
||||
visit(name, sch, false, false, schemaPath)
|
||||
validate('if (%s === errors) {', prev)
|
||||
('%s++', passes)
|
||||
('} else {')
|
||||
('errors = %s', prev)
|
||||
('}')
|
||||
})
|
||||
|
||||
validate('if (%s !== 1) {', passes)
|
||||
error('no (or more than one) schemas match')
|
||||
validate('}')
|
||||
}
|
||||
|
||||
if (node.multipleOf !== undefined) {
|
||||
if (type !== 'number' && type !== 'integer') validate('if (%s) {', types.number(name))
|
||||
|
||||
validate('if (!isMultipleOf(%s, %d)) {', name, node.multipleOf)
|
||||
|
||||
error('has a remainder')
|
||||
validate('}')
|
||||
|
||||
if (type !== 'number' && type !== 'integer') validate('}')
|
||||
}
|
||||
|
||||
if (node.maxProperties !== undefined) {
|
||||
if (type !== 'object') validate('if (%s) {', types.object(name))
|
||||
|
||||
validate('if (Object.keys(%s).length > %d) {', name, node.maxProperties)
|
||||
error('has more properties than allowed')
|
||||
validate('}')
|
||||
|
||||
if (type !== 'object') validate('}')
|
||||
}
|
||||
|
||||
if (node.minProperties !== undefined) {
|
||||
if (type !== 'object') validate('if (%s) {', types.object(name))
|
||||
|
||||
validate('if (Object.keys(%s).length < %d) {', name, node.minProperties)
|
||||
error('has less properties than allowed')
|
||||
validate('}')
|
||||
|
||||
if (type !== 'object') validate('}')
|
||||
}
|
||||
|
||||
if (node.maxItems !== undefined) {
|
||||
if (type !== 'array') validate('if (%s) {', types.array(name))
|
||||
|
||||
validate('if (%s.length > %d) {', name, node.maxItems)
|
||||
error('has more items than allowed')
|
||||
validate('}')
|
||||
|
||||
if (type !== 'array') validate('}')
|
||||
}
|
||||
|
||||
if (node.minItems !== undefined) {
|
||||
if (type !== 'array') validate('if (%s) {', types.array(name))
|
||||
|
||||
validate('if (%s.length < %d) {', name, node.minItems)
|
||||
error('has less items than allowed')
|
||||
validate('}')
|
||||
|
||||
if (type !== 'array') validate('}')
|
||||
}
|
||||
|
||||
if (node.maxLength !== undefined) {
|
||||
if (type !== 'string') validate('if (%s) {', types.string(name))
|
||||
|
||||
validate('if (%s.length > %d) {', name, node.maxLength)
|
||||
error('has longer length than allowed')
|
||||
validate('}')
|
||||
|
||||
if (type !== 'string') validate('}')
|
||||
}
|
||||
|
||||
if (node.minLength !== undefined) {
|
||||
if (type !== 'string') validate('if (%s) {', types.string(name))
|
||||
|
||||
validate('if (%s.length < %d) {', name, node.minLength)
|
||||
error('has less length than allowed')
|
||||
validate('}')
|
||||
|
||||
if (type !== 'string') validate('}')
|
||||
}
|
||||
|
||||
if (node.minimum !== undefined) {
|
||||
if (type !== 'number' && type !== 'integer') validate('if (%s) {', types.number(name))
|
||||
|
||||
validate('if (%s %s %d) {', name, node.exclusiveMinimum ? '<=' : '<', node.minimum)
|
||||
error('is less than minimum')
|
||||
validate('}')
|
||||
|
||||
if (type !== 'number' && type !== 'integer') validate('}')
|
||||
}
|
||||
|
||||
if (node.maximum !== undefined) {
|
||||
if (type !== 'number' && type !== 'integer') validate('if (%s) {', types.number(name))
|
||||
|
||||
validate('if (%s %s %d) {', name, node.exclusiveMaximum ? '>=' : '>', node.maximum)
|
||||
error('is more than maximum')
|
||||
validate('}')
|
||||
|
||||
if (type !== 'number' && type !== 'integer') validate('}')
|
||||
}
|
||||
|
||||
if (properties) {
|
||||
Object.keys(properties).forEach(function(p) {
|
||||
if (Array.isArray(type) && type.indexOf('null') !== -1) validate('if (%s !== null) {', name)
|
||||
|
||||
visit(
|
||||
genobj(name, p),
|
||||
properties[p],
|
||||
reporter,
|
||||
filter,
|
||||
schemaPath.concat(tuple ? p : ['properties', p])
|
||||
)
|
||||
|
||||
if (Array.isArray(type) && type.indexOf('null') !== -1) validate('}')
|
||||
})
|
||||
}
|
||||
|
||||
while (indent--) validate('}')
|
||||
}
|
||||
|
||||
var validate = genfun
|
||||
('function validate(data) {')
|
||||
// Since undefined is not a valid JSON value, we coerce to null and other checks will catch this
|
||||
('if (data === undefined) data = null')
|
||||
('validate.errors = null')
|
||||
('var errors = 0')
|
||||
|
||||
visit('data', schema, reporter, opts && opts.filter, [])
|
||||
|
||||
validate
|
||||
('return errors === 0')
|
||||
('}')
|
||||
|
||||
validate = validate.toFunction(scope)
|
||||
validate.errors = null
|
||||
|
||||
if (Object.defineProperty) {
|
||||
Object.defineProperty(validate, 'error', {
|
||||
get: function() {
|
||||
if (!validate.errors) return ''
|
||||
return validate.errors.map(function(err) {
|
||||
return err.field + ' ' + err.message;
|
||||
}).join('\n')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
validate.toJSON = function() {
|
||||
return schema
|
||||
}
|
||||
|
||||
return validate
|
||||
}
|
||||
|
||||
module.exports = function(schema, opts) {
|
||||
if (typeof schema === 'string') schema = JSON.parse(schema)
|
||||
return compile(schema, {}, schema, true, opts)
|
||||
}
|
||||
|
||||
module.exports.filter = function(schema, opts) {
|
||||
var validate = module.exports(schema, xtend(opts, {filter: true}))
|
||||
return function(sch) {
|
||||
validate(sch)
|
||||
return sch
|
||||
}
|
||||
}
|
130
node_modules/is-my-json-valid/package.json
generated
vendored
Normal file
130
node_modules/is-my-json-valid/package.json
generated
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
{
|
||||
"name": "is-my-json-valid",
|
||||
"raw": "is-my-json-valid@^2.12.4",
|
||||
"rawSpec": "^2.12.4",
|
||||
"scope": null,
|
||||
"spec": ">=2.12.4 <3.0.0",
|
||||
"type": "range"
|
||||
},
|
||||
"/root/gitbook/node_modules/phantomjs/node_modules/har-validator"
|
||||
]
|
||||
],
|
||||
"_from": "is-my-json-valid@>=2.12.4 <3.0.0",
|
||||
"_hasShrinkwrap": false,
|
||||
"_id": "is-my-json-valid@2.20.6",
|
||||
"_inCache": true,
|
||||
"_installable": true,
|
||||
"_location": "/is-my-json-valid",
|
||||
"_nodeVersion": "16.11.1",
|
||||
"_npmOperationalInternal": {
|
||||
"host": "s3://npm-registry-packages",
|
||||
"tmp": "tmp/is-my-json-valid_2.20.6_1636548621688_0.3290085683043438"
|
||||
},
|
||||
"_npmUser": {
|
||||
"email": "linus@folkdatorn.se",
|
||||
"name": "linusu"
|
||||
},
|
||||
"_npmVersion": "8.0.0",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"name": "is-my-json-valid",
|
||||
"raw": "is-my-json-valid@^2.12.4",
|
||||
"rawSpec": "^2.12.4",
|
||||
"scope": null,
|
||||
"spec": ">=2.12.4 <3.0.0",
|
||||
"type": "range"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/phantomjs/har-validator"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/is-my-json-valid/-/is-my-json-valid-2.20.6.tgz",
|
||||
"_shasum": "a9d89e56a36493c77bda1440d69ae0dc46a08387",
|
||||
"_shrinkwrap": null,
|
||||
"_spec": "is-my-json-valid@^2.12.4",
|
||||
"_where": "/root/gitbook/node_modules/phantomjs/node_modules/har-validator",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mafintosh/is-my-json-valid/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"generate-function": "^2.0.0",
|
||||
"generate-object-property": "^1.1.0",
|
||||
"is-my-ip-valid": "^1.0.0",
|
||||
"jsonpointer": "^5.0.0",
|
||||
"xtend": "^4.0.0"
|
||||
},
|
||||
"description": "A [JSONSchema](https://json-schema.org/) validator that uses code generation to be extremely fast.",
|
||||
"devDependencies": {
|
||||
"safe-regex": "^1.1.0",
|
||||
"tape": "^2.13.4",
|
||||
"typescript": "^3.0.1"
|
||||
},
|
||||
"directories": {},
|
||||
"dist": {
|
||||
"fileCount": 7,
|
||||
"integrity": "sha512-1JQwulVNjx8UqkPE/bqDaxtH4PXCe/2VRh/y3p99heOV87HG4Id5/VfDswd+YiAfHcRTfDlWgISycnHuhZq1aw==",
|
||||
"npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh2wFNCRA9TVsSAnZWagAAxwIP/RpL3r1qkGN36oyaxaw9\ni6ZGo6KZAxljN4p4BxJ6dbJLgTv2zb7PdCVK1xx5vJ3xcui6aQ/F3gki75Nj\nt2Ndp6jtXqjE24UksBiSjmiiLKPoqhET+joPtf+eRTIGX0rYfKpuVHxDgPfY\nfrq+TEysyjFdajtXYjJckVSAW5qwAN/oHHsgoPlZHoguICpi6e84tUy6ci+5\nmkPYdv3ulqMKL6Xqc9usSXXuzEEqaKkpiuS/RXFddkYBpumSElaDYBqGpWrf\nVQ/2kcYu0kCY3qXgci/gHR9nfq2i2OooMZKWbDEV93gNNfe98/7VBI7jFtxL\nN4QfaXPrct7g3Kvb3ZsX4J4zcAtM+QAGeR9Th/N6Jf+FvECeVSJFHLHJDiip\nt54uWaQCr4qeRv5Tgyt8JZIAuxHXjsiF62rgXx1rnpqNPVsFKo64wX1CIdXG\nuNZKSIyvj0yrWRKVyReyWhUM0gLYrht4hrKp/xtydtybmj2CeA6LvLu2/9Ct\neoSv2Z3lqeofvKPdr4mNoH2B5pHSeEjr2vbL29Grh14xQbq88bvS4xseoAst\noQt6BGef8h4DTHDzUynxEDZeDUrPoyyGLt/4XecEIGN/mN76wqh00L7BGF/Q\nL/afLeL6vlGFXGAONw1vDlwH33SpzYNlstXreNMjzcIyZ43+9SNmLEe7JFRo\nXd8U\r\n=nzgZ\r\n-----END PGP SIGNATURE-----\r\n",
|
||||
"shasum": "a9d89e56a36493c77bda1440d69ae0dc46a08387",
|
||||
"signatures": [
|
||||
{
|
||||
"keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
|
||||
"sig": "MEQCIDh63qktmABys9LZHsbt7pN6hR3ZQw5TjFFFm84ZuBYOAiB7J6a3XeQozc7FzNVBz/HE+WHH3DiMYHnbBK/DXHHWWg=="
|
||||
}
|
||||
],
|
||||
"tarball": "https://registry.npmjs.org/is-my-json-valid/-/is-my-json-valid-2.20.6.tgz",
|
||||
"unpackedSize": 40182
|
||||
},
|
||||
"gitHead": "58d30cbf99a22d8a3ff36771bc8e588e68f3847e",
|
||||
"homepage": "https://github.com/mafintosh/is-my-json-valid#readme",
|
||||
"keywords": [
|
||||
"json",
|
||||
"schema",
|
||||
"orderly",
|
||||
"jsonschema"
|
||||
],
|
||||
"license": "MIT",
|
||||
"maintainers": [
|
||||
{
|
||||
"email": "freeall@gmail.com",
|
||||
"name": "freeall"
|
||||
},
|
||||
{
|
||||
"email": "linus@folkdatorn.se",
|
||||
"name": "linusu"
|
||||
},
|
||||
{
|
||||
"email": "mathiasbuus@gmail.com",
|
||||
"name": "mafintosh"
|
||||
},
|
||||
{
|
||||
"email": "yoshuawuyts@gmail.com",
|
||||
"name": "yoshuawuyts"
|
||||
},
|
||||
{
|
||||
"email": "github@tixz.dk",
|
||||
"name": "emilbay"
|
||||
},
|
||||
{
|
||||
"email": "w@tson.dk",
|
||||
"name": "watson"
|
||||
},
|
||||
{
|
||||
"email": "github@tixz.dk",
|
||||
"name": "emilbayes"
|
||||
}
|
||||
],
|
||||
"name": "is-my-json-valid",
|
||||
"optionalDependencies": {},
|
||||
"readme": "ERROR: No README data found!",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mafintosh/is-my-json-valid.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tape test/*.js && tsc"
|
||||
},
|
||||
"types": "./index.d.ts",
|
||||
"version": "2.20.6"
|
||||
}
|
12
node_modules/is-my-json-valid/require.js
generated
vendored
Normal file
12
node_modules/is-my-json-valid/require.js
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
var fs = require('fs')
|
||||
var path = require('path')
|
||||
var compile = require('./')
|
||||
|
||||
delete require.cache[require.resolve(__filename)]
|
||||
|
||||
module.exports = function(file, opts) {
|
||||
file = path.join(path.dirname(module.parent.filename), file)
|
||||
if (!fs.existsSync(file) && fs.existsSync(file+'.schema')) file += '.schema'
|
||||
if (!fs.existsSync(file) && fs.existsSync(file+'.json')) file += '.json'
|
||||
return compile(fs.readFileSync(file, 'utf-8'), opts)
|
||||
}
|
Reference in New Issue
Block a user