New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

another-json-schema

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

another-json-schema - npm Package Compare versions

Comparing version 2.1.1 to 2.1.2

.eslintrc.json

48

helpers.js

@@ -1,19 +0,17 @@

'use strict';
const toString = Object.prototype.toString
var toString = Object.prototype.toString;
//return value or throw error
// return value or throw error
exports.type = function (actual, expected) {
if (expected === 'any') return actual;
if ('function' === typeof expected) {
return expected.call(this, actual);
if (expected === 'any') return actual
if (typeof expected === 'function') {
return expected.call(this, actual)
}
if (expected === toString.call(actual).match(/^\[object\s(.*)\]$/)[1].toLowerCase()) {
return actual;
return actual
} else {
throw null;
throw null
}
};
}
//return true|false
// return true|false
/*

@@ -23,20 +21,20 @@ * Number

exports.gt = function (actual, expected) {
return actual > expected;
};
return actual > expected
}
exports.gte = function (actual, expected) {
return actual >= expected;
};
return actual >= expected
}
exports.lt = function (actual, expected) {
return actual < expected;
};
return actual < expected
}
exports.lte = function (actual, expected) {
return actual <= expected;
};
return actual <= expected
}
exports.range = function (actual, expected) {
return (actual >= expected[0]) && (actual <= expected[1]);
};
return (actual >= expected[0]) && (actual <= expected[1])
}

@@ -47,4 +45,4 @@ /*

exports.enum = function (actual, expected) {
return expected.indexOf(actual) !== -1;
};
return expected.indexOf(actual) !== -1
}

@@ -55,3 +53,3 @@ /*

exports.pattern = function (actual, expected) {
return expected.test(actual);
};
return expected.test(actual)
}

@@ -1,59 +0,56 @@

'use strict';
const helpersFuncs = require('./helpers')
var isBuffer = require('is-buffer');
var helpersFuncs = require('./helpers');
function AJS(name, schema) {
function AJS (name, schema) {
if (!(this instanceof AJS)) {
return new AJS(name, schema);
return new AJS(name, schema)
}
if (name) {
this.compile(name, schema);
this.compile(name, schema)
}
}
AJS.register = function register(name, fn) {
if (!name || !fn) throw new TypeError('Missing name or fn');
helpersFuncs[name] = fn;
};
AJS.register = function register (name, fn) {
if (!name || !fn) throw new TypeError('Missing name or fn')
helpersFuncs[name] = fn
}
AJS.prototype.compile = function compile(name, schema) {
AJS.prototype.compile = function compile (name, schema) {
if (!schema) {
schema = name;
schema = name
} else {
this._name = name;
this._name = name
}
if ('object' !== typeof schema) throw new TypeError('Schema must be object or array');
if (typeof schema !== 'object') throw new TypeError('Schema must be object or array')
this._children = _compileSchema(schema, this);
this._children = _compileSchema(schema, this)
_iteratorSchema(this);
return this;
};
_iteratorSchema(this)
return this
}
AJS.prototype.validate = function validate(obj, opts) {
if (!this._schema) throw new TypeError('No schema assigned, please call .compile(schema)');
opts = opts || {};
return _validateObject(obj, opts, this);
};
AJS.prototype.validate = function validate (obj, opts) {
if (!this._schema) throw new TypeError('No schema assigned, please call .compile(schema)')
opts = opts || {}
return _validateObject(obj, opts, this)
}
function _compileSchema(schema, ctx) {
var children = {};
var isArray = Array.isArray(schema);
schema = isArray ? schema[0] : schema;
ctx[isArray ? '_array' : '_object'] = true;
function _compileSchema (schema, ctx) {
const children = {}
const isArray = Array.isArray(schema)
schema = isArray ? schema[0] : schema
ctx[isArray ? '_array' : '_object'] = true
if (schema instanceof AJS) {
if (schema._name) ctx._name = schema._name;
if (schema._name) ctx._name = schema._name
if (schema._leaf) {
ctx._leaf = schema._leaf;
delete ctx._object;
ctx._leaf = schema._leaf
delete ctx._object
}
// consider [JAS(...)] & AJS([...])
if (schema._array) {
ctx._array = schema._array;
delete ctx._object;
ctx._array = schema._array
delete ctx._object
}
return schema._children;
return schema._children
}

@@ -63,76 +60,76 @@

if (schema.type && !(schema.type instanceof AJS) && !schema.type.type) {
ctx._leaf = true;
ctx._leaf = true
// leaf should not be object
delete ctx._object;
return schema;
delete ctx._object
return schema
}
for (var key in schema) {
children[key] = AJS(schema[key]);
for (let key in schema) {
children[key] = AJS(schema[key])
}
return children;
return children
}
function _iteratorSchema(ctx, parentKey, currentKey, parentNode) {
ctx._name = ctx._name || (parentNode && parentNode._name);
if (!ctx._name) delete ctx._name;
ctx._parent = parentNode || null;
ctx._path = !parentKey ? '$' : (parentKey + '.' + currentKey);
function _iteratorSchema (ctx, parentKey, currentKey, parentNode) {
ctx._name = ctx._name || (parentNode && parentNode._name)
if (!ctx._name) delete ctx._name
ctx._parent = parentNode || null
ctx._path = !parentKey ? '$' : (parentKey + '.' + currentKey)
if (ctx._array) {
ctx._path = ctx._path + '[]';
ctx._path = ctx._path + '[]'
}
if (ctx._leaf) {
ctx._schema = ctx._array ? [ctx._children] : ctx._children;
ctx._schema = ctx._array ? [ctx._children] : ctx._children
} else if (ctx._array) {
ctx._schema = [iterator(ctx._children)];
ctx._schema = [iterator(ctx._children)]
} else {
ctx._schema = iterator(ctx._children);
ctx._schema = iterator(ctx._children)
}
return ctx._schema;
return ctx._schema
function iterator(children) {
var result = {};
for (var key in children) {
result[key] = _iteratorSchema(children[key], ctx._path, key, ctx);
function iterator (children) {
const result = {}
for (let key in children) {
result[key] = _iteratorSchema(children[key], ctx._path, key, ctx)
}
return result;
return result
}
}
function _validateObject(obj, opts, ctx) {
var additionalProperties = opts.additionalProperties;
var error = null;
function _validateObject (obj, opts, ctx) {
const additionalProperties = opts.additionalProperties
let error = null
try {
obj = iterator(obj, opts, ctx);
obj = iterator(obj, opts, ctx)
} catch (e) {
error = e;
error = e
}
function iterator(children, opts, ctx) {
var isObject = 'object' === typeof children;
var isArray = Array.isArray(children);
function iterator (children, opts, ctx) {
const isObject = typeof children === 'object'
const isArray = Array.isArray(children)
if (!opts.ignoreNodeType) {
validateType(children, ctx);
validateType(children, ctx)
}
if (ctx._leaf || isBuffer(children) || !isObject) {
return validateLeaf(children, opts, ctx);
if (ctx._leaf || Buffer.isBuffer(children) || !isObject) {
return validateLeaf(children, opts, ctx)
} else {
if (isArray) {
return children.map(function (item) {
return iterator(item, opts, ctx);
});
return iterator(item, opts, ctx)
})
} else {
for (var key in children) {
for (let key in children) {
if (!ctx._children || !ctx._children[key]) {
if (!additionalProperties) {
delete children[key];
delete children[key]
}
} else {
children[key] = iterator(children[key], opts, ctx._children[key]);
children[key] = iterator(children[key], opts, ctx._children[key])
}
}
return children;
return children
}

@@ -146,21 +143,21 @@ }

result: obj
};
}
}
function validateType(value, ctx) {
var isObject = 'object' === typeof value;
var isArray = Array.isArray(value);
function validateType (value, ctx) {
var isObject = typeof value === 'object'
var isArray = Array.isArray(value)
if (ctx._leaf) {
if ('function' === typeof ctx._children.type) {
return;
if (typeof ctx._children.type === 'function') {
return
}
if (isArray && !ctx._array) {
throwError(value, ctx);
throwError(value, ctx)
} else if (ctx._array && !isArray) {
throwError(value, ctx, null, 'array');
throwError(value, ctx, null, 'array')
}
} else {
if (ctx._object && !isObject) {
throwError(value, ctx, null, 'object');
throwError(value, ctx, null, 'object')
}

@@ -170,73 +167,73 @@ }

function validateLeaf(value, opts, ctx) {
function validateLeaf (value, opts, ctx) {
// leaf also is array
if (Array.isArray(value)) {
return value.map(function (item) {
return validate(item);
});
return validate(item)
})
} else {
return validate(value);
return validate(value)
}
function validate(value) {
var valid = true;//default passed
//check type first, can modify `value`
function validate (value) {
let valid = true// default passed
// check type first, can modify `value`
try {
value = helpersFuncs.type.call(ctx, value, ctx._children.type);
value = helpersFuncs.type.call(ctx, value, ctx._children.type)
} catch (e) {
throwError(value, ctx, 'type', null, e);
throwError(value, ctx, 'type', null, e)
}
//check others, can not modify `value`
for (var helper in ctx._children) {
if ('type' === helper || (opts[helper] != null && !opts[helper])) {
continue;
// check others, can not modify `value`
for (let helper in ctx._children) {
if (helper === 'type' || (opts[helper] != null && !opts[helper])) {
continue
}
if ('function' === typeof ctx._children[helper]) {
//custom function validator
valid = ctx._children[helper].call(ctx, value);
if (typeof ctx._children[helper] === 'function') {
// custom function validator
valid = ctx._children[helper].call(ctx, value)
} else if (helpersFuncs[helper]) {
//registered helpers
valid = helpersFuncs[helper].call(ctx, value, ctx._children[helper]);
// registered helpers
valid = helpersFuncs[helper].call(ctx, value, ctx._children[helper])
}
if (!valid) {
throwError(value, ctx, helper);
throwError(value, ctx, helper)
}
}
return value;
return value
}
}
function throwError(value, ctx, helper, type, originError) {
var error = null;
function throwError (value, ctx, helper, type, originError) {
let error = null
if (!type) {
if (helper) {
var helperEntry = ctx._children[helper];
if ('function' === typeof helperEntry) {
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + (helperEntry.name || 'Function') + ')');
const helperEntry = ctx._children[helper]
if (typeof helperEntry === 'function') {
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + (helperEntry.name || 'Function') + ')')
} else {
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry + ')');
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry + ')')
}
error.validator = helper;
error.validator = helper
} else {
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + JSON.stringify(ctx._children) + ')');
error.validator = 'type';
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + JSON.stringify(ctx._children) + ')')
error.validator = 'type'
}
} else {
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (type: ' + type + ')');
error.validator = 'type';
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (type: ' + type + ')')
error.validator = 'type'
}
error.actual = value;
error.expected = ctx._children;
error.path = ctx._path;
error.schema = ctx._name;
error.actual = value
error.expected = ctx._children
error.path = ctx._path
error.schema = ctx._name
if (originError) {
error.originError = originError;
error.originError = originError
}
throw error;
throw error
}
module.exports = AJS;
module.exports.helpers = helpersFuncs;
module.exports = AJS
module.exports.helpers = helpersFuncs
{
"name": "another-json-schema",
"version": "2.1.1",
"version": "2.1.2",
"description": "Another JSON Schema, simple & flexible & intuitive.",

@@ -20,3 +20,2 @@ "main": "index.js",

"dependencies": {
"is-buffer": "^1.1.2"
},

@@ -28,2 +27,7 @@ "repository": {

"devDependencies": {
"eslint-config-standard": "10.2.1",
"eslint-plugin-import": "2.7.0",
"eslint-plugin-node": "5.2.0",
"eslint-plugin-promise": "3.5.0",
"eslint-plugin-standard": "3.0.1",
"istanbul": "^0.4.2",

@@ -30,0 +34,0 @@ "mocha": "^2.4.5"

@@ -13,5 +13,5 @@ ### another-json-schema

```sh
$ npm i another-json-schema --save
```
npm i another-json-schema --save
```

@@ -22,3 +22,3 @@ ### Usage

```
```js
var AJS = require('another-json-schema');

@@ -52,3 +52,3 @@

```
```js
var AJS = require('another-json-schema');

@@ -122,3 +122,3 @@

```
```js
var validator = require('validator');

@@ -180,3 +180,3 @@ var toObjectId = require('mongodb').ObjectId;

```
```js
AJS.register('gt', function (actual, expected, key, parentNode) {

@@ -191,3 +191,3 @@ return actual > expected;

```
```js
var userSchema = AJS('userSchema', {

@@ -201,3 +201,3 @@ _id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },

```
```js
var newSchema = new AJS();

@@ -204,0 +204,0 @@ var userSchema = newSchema.compile('userSchema', {

@@ -1,17 +0,15 @@

'use strict';
const AJS = require('..')
const assert = require('assert')
var AJS = require('..');
var assert = require('assert');
describe('compile', function () {
it('error', function () {
try {
AJS(111, 222);
} catch(e) {
assert.equal(e.message, 'Schema must be object or array');
AJS(111, 222)
} catch (e) {
assert.equal(e.message, 'Schema must be object or array')
}
});
})
it('leaf', function () {
var schema1 = AJS({ type: 'string' });
const schema1 = AJS({ type: 'string' })
assert.deepEqual(schema1, {

@@ -23,5 +21,5 @@ _leaf: true,

_schema: { type: 'string' }
});
})
var schema2 = AJS([{ type: 'string' }]);
const schema2 = AJS([{ type: 'string' }])
assert.deepEqual(schema2, {

@@ -34,5 +32,5 @@ _array: true,

_schema: [{ type: 'string' }]
});
})
var schema3 = AJS('stringSchema', [{ type: 'string' }]);
const schema3 = AJS('stringSchema', [{ type: 'string' }])
assert.deepEqual(schema3, {

@@ -46,7 +44,7 @@ _array: true,

_schema: [{ type: 'string' }]
});
});
})
})
it('object', function () {
var schema1 = AJS({
let schema1 = AJS({
author: {

@@ -56,4 +54,4 @@ type: 'string',

}
});
var schema2 = AJS({
})
let schema2 = AJS({
author: AJS({

@@ -63,11 +61,11 @@ type: 'string',

})
});
})
try {
assert.deepEqual(schema1, schema2);
} catch(e) {
assert.equal(e.message, 'Maximum call stack size exceeded');
assert.deepEqual(schema1, schema2)
} catch (e) {
assert.equal(e.message, 'Maximum call stack size exceeded')
}
var schema1 = AJS({
schema1 = AJS({
author: {

@@ -77,4 +75,4 @@ type: { type: 'string' },

}
});
var schema2 = AJS({
})
schema2 = AJS({
author: AJS({

@@ -84,11 +82,11 @@ type: AJS({ type: 'string' }),

})
});
})
try {
assert.deepEqual(schema1, schema2);
} catch(e) {
assert.equal(e.message, 'Maximum call stack size exceeded');
assert.deepEqual(schema1, schema2)
} catch (e) {
assert.equal(e.message, 'Maximum call stack size exceeded')
}
var schema1 = AJS({
schema1 = AJS({
author: {

@@ -98,4 +96,4 @@ name: { type: 'string' },

}
});
var schema2 = AJS({
})
schema2 = AJS({
author: AJS({

@@ -105,4 +103,4 @@ name: { type: 'string' },

})
});
var schema3 = AJS({
})
const schema3 = AJS({
author: AJS({

@@ -112,17 +110,17 @@ name: AJS({ type: 'string' }),

})
});
})
try {
assert.deepEqual(schema1, schema2);
} catch(e) {
assert.equal(e.message, 'Maximum call stack size exceeded');
assert.deepEqual(schema1, schema2)
} catch (e) {
assert.equal(e.message, 'Maximum call stack size exceeded')
}
try {
assert.deepEqual(schema1, schema3);
} catch(e) {
assert.equal(e.message, 'Maximum call stack size exceeded');
assert.deepEqual(schema1, schema3)
} catch (e) {
assert.equal(e.message, 'Maximum call stack size exceeded')
}
});
})
it('array', function () {
var schema1 = AJS([{
const schema1 = AJS([{
authors: [{

@@ -132,4 +130,4 @@ names: [{ type: 'string' }],

}]
}]);
var schema2 = AJS([{
}])
const schema2 = AJS([{
authors: AJS([{

@@ -139,4 +137,4 @@ names: [{ type: 'string' }],

}])
}]);
var schema3 = AJS([{
}])
const schema3 = AJS([{
authors: AJS([{

@@ -146,14 +144,14 @@ names: AJS([{ type: 'string' }]),

}])
}]);
}])
try {
assert.deepEqual(schema1, schema2);
} catch(e) {
assert.equal(e.message, 'Maximum call stack size exceeded');
assert.deepEqual(schema1, schema2)
} catch (e) {
assert.equal(e.message, 'Maximum call stack size exceeded')
}
try {
assert.deepEqual(schema1, schema3);
} catch(e) {
assert.equal(e.message, 'Maximum call stack size exceeded');
assert.deepEqual(schema1, schema3)
} catch (e) {
assert.equal(e.message, 'Maximum call stack size exceeded')
}
});
});
})
})

@@ -1,20 +0,18 @@

'use strict';
const AJS = require('..')
const assert = require('assert')
var AJS = require('..');
var assert = require('assert');
describe('helper', function () {
it('error', function () {
try {
AJS.register('gt');
} catch(e) {
assert.equal(e.message, 'Missing name or fn');
AJS.register('gt')
} catch (e) {
assert.equal(e.message, 'Missing name or fn')
}
});
})
it('.gt18', function () {
AJS.register('gt18', function (actual, expected) {
return expected ? (actual > 18) : (actual <= 18);
});
var schema = AJS('adultSchema', { type: 'number', gt18: true });
return expected ? (actual > 18) : (actual <= 18)
})
const schema = AJS('adultSchema', { type: 'number', gt18: true })
assert.deepEqual(schema, {

@@ -27,4 +25,4 @@ _leaf: true,

_schema: { type: 'number', gt18: true }
});
assert.deepEqual(schema.validate(19), { valid: true, error: null, result: 19 });
})
assert.deepEqual(schema.validate(19), { valid: true, error: null, result: 19 })
assert.deepEqual(schema.validate(0), { valid: false,

@@ -39,16 +37,16 @@ error:

result: 0
});
})
//gt18: false
assert.deepEqual(schema.validate(0, { gt18: false }), { valid: true, error: null, result: 0 });
});
assert.deepEqual(schema.validate(0, { gt18: false }), { valid: true, error: null, result: 0 })
})
it('.type', function () {
var schema = AJS('typeSchema', { type: 'any' });
assert.deepEqual(schema.validate(0), { valid: true, error: null, result: 0 });
assert.deepEqual(schema.validate('0'), { valid: true, error: null, result: '0' });
assert.deepEqual(schema.validate(true), { valid: true, error: null, result: true });
let schema = AJS('typeSchema', { type: 'any' })
assert.deepEqual(schema.validate(0), { valid: true, error: null, result: 0 })
assert.deepEqual(schema.validate('0'), { valid: true, error: null, result: '0' })
assert.deepEqual(schema.validate(true), { valid: true, error: null, result: true })
var schema = AJS('typeSchema', { type: 'number' });
assert.deepEqual(schema.validate(0), { valid: true, error: null, result: 0 });
schema = AJS('typeSchema', { type: 'number' })
assert.deepEqual(schema.validate(0), { valid: true, error: null, result: 0 })
assert.deepEqual(schema.validate('0'), { valid: false,

@@ -63,14 +61,14 @@ error:

result: '0'
});
})
function checkIsObject (actual) {
if (typeof actual === 'object') {
return actual;
return actual
} else {
throw 'Not object!';
throw 'Not object!'
}
}
var schema = AJS('typeSchema', { type: checkIsObject });
assert.deepEqual(schema.validate([]), { valid: true, error: null, result: [] });
assert.deepEqual(schema.validate({}), { valid: true, error: null, result: {} });
schema = AJS('typeSchema', { type: checkIsObject })
assert.deepEqual(schema.validate([]), { valid: true, error: null, result: [] })
assert.deepEqual(schema.validate({}), { valid: true, error: null, result: {} })
assert.deepEqual(schema.validate(0), { valid: false,

@@ -86,15 +84,15 @@ error:

result: 0
});
})
function toUpperCase(value) {
if ('string' !== typeof value) {
throw 'name is not String';
function toUpperCase (value) {
if (typeof value !== 'string') {
throw 'name is not String'
}
return value.toUpperCase();
return value.toUpperCase()
}
var schema = AJS('typeSchema', {
schema = AJS('typeSchema', {
name: { type: toUpperCase }
});
})
assert.deepEqual(schema._children.name.validate('a'), { valid: true, error: null, result: 'A' });
assert.deepEqual(schema._children.name.validate('a'), { valid: true, error: null, result: 'A' })
assert.deepEqual(schema._children.name.validate(0), {

@@ -111,5 +109,5 @@ valid: false,

result: 0
});
})
var schema = AJS('typeSchema', [{ type: 'string' }]);
schema = AJS('typeSchema', [{ type: 'string' }])
assert.deepEqual(schema.validate('a'), {

@@ -125,5 +123,5 @@ valid: false,

result: 'a'
});
})
var schema = AJS('typeSchema', { type: 'string' });
schema = AJS('typeSchema', { type: 'string' })
assert.deepEqual(schema.validate(['a']), {

@@ -139,8 +137,8 @@ valid: false,

result: [ 'a' ]
});
});
})
})
it('.gt', function () {
var schema = AJS('numberSchema', { type: 'number', gt: 0 });
assert.deepEqual(schema.validate(1), { valid: true, error: null, result: 1 });
const schema = AJS('numberSchema', { type: 'number', gt: 0 })
assert.deepEqual(schema.validate(1), { valid: true, error: null, result: 1 })
assert.deepEqual(schema.validate(0), { valid: false,

@@ -155,8 +153,8 @@ error:

result: 0
});
});
})
})
it('.gte', function () {
var schema = AJS('numberSchema', { type: 'number', gte: 0 });
assert.deepEqual(schema.validate(1), { valid: true, error: null, result: 1 });
const schema = AJS('numberSchema', { type: 'number', gte: 0 })
assert.deepEqual(schema.validate(1), { valid: true, error: null, result: 1 })
assert.deepEqual(schema.validate(-1), { valid: false,

@@ -171,8 +169,8 @@ error:

result: -1
});
});
})
})
it('.lt', function () {
var schema = AJS('numberSchema', { type: 'number', lt: 0 });
assert.deepEqual(schema.validate(-1), { valid: true, error: null, result: -1 });
const schema = AJS('numberSchema', { type: 'number', lt: 0 })
assert.deepEqual(schema.validate(-1), { valid: true, error: null, result: -1 })
assert.deepEqual(schema.validate(0), { valid: false,

@@ -187,8 +185,8 @@ error:

result: 0
});
});
})
})
it('.lte', function () {
var schema = AJS('numberSchema', { type: 'number', lte: 0 });
assert.deepEqual(schema.validate(0), { valid: true, error: null, result: 0 });
const schema = AJS('numberSchema', { type: 'number', lte: 0 })
assert.deepEqual(schema.validate(0), { valid: true, error: null, result: 0 })
assert.deepEqual(schema.validate(1), { valid: false,

@@ -203,9 +201,9 @@ error:

result: 1
});
});
})
})
it('.range', function () {
var schema = AJS('numberSchema', { type: 'number', range: [0, 10] });
assert.deepEqual(schema.validate(0), { valid: true, error: null, result: 0 });
assert.deepEqual(schema.validate(10), { valid: true, error: null, result: 10 });
const schema = AJS('numberSchema', { type: 'number', range: [0, 10] })
assert.deepEqual(schema.validate(0), { valid: true, error: null, result: 0 })
assert.deepEqual(schema.validate(10), { valid: true, error: null, result: 10 })
assert.deepEqual(schema.validate(-1), { valid: false,

@@ -220,8 +218,8 @@ error:

result: -1
});
});
})
})
it('.enum', function () {
var schema = AJS('enumSchema', { type: 'string', enum: ['aaa', 'bbb'] });
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' });
const schema = AJS('enumSchema', { type: 'string', enum: ['aaa', 'bbb'] })
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' })
assert.deepEqual(schema.validate('ccc'), { valid: false,

@@ -236,11 +234,11 @@ error:

result: 'ccc'
});
});
})
})
it('.required custom', function () {
function required(value) {
return !!value;
function required (value) {
return !!value
}
var schema = AJS('requiredSchema', { type: 'string', required: required });
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' });
let schema = AJS('requiredSchema', { type: 'string', required: required })
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' })
assert.deepEqual(schema.validate(''), { valid: false,

@@ -255,12 +253,12 @@ error:

result: ''
});
})
var schema = AJS('requiredSchema', { type: 'string', required: 'will be ignore' });
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' });
assert.deepEqual(schema.validate(''), { valid: true, error: null, result: '' });
});
schema = AJS('requiredSchema', { type: 'string', required: 'will be ignore' })
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' })
assert.deepEqual(schema.validate(''), { valid: true, error: null, result: '' })
})
it('.pattern', function () {
var schema = AJS('patternSchema', { type: 'string', pattern: /^a/ });
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' });
const schema = AJS('patternSchema', { type: 'string', pattern: /^a/ })
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' })
assert.deepEqual(schema.validate('bbb'), { valid: false,

@@ -275,11 +273,11 @@ error:

result: 'bbb'
});
});
})
})
it('.validate custom', function () {
var validate = function (actual) {
return actual === 'aaa';
};
var schema = AJS('validateSchema', { type: 'string', validate: validate });
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' });
const validate = function (actual) {
return actual === 'aaa'
}
const schema = AJS('validateSchema', { type: 'string', validate: validate })
assert.deepEqual(schema.validate('aaa'), { valid: true, error: null, result: 'aaa' })
assert.deepEqual(schema.validate('bbb'), { valid: false,

@@ -294,4 +292,4 @@ error:

result: 'bbb'
});
});
});
})
})
})

@@ -1,15 +0,13 @@

'use strict';
const AJS = require('..')
const assert = require('assert')
var AJS = require('..');
var assert = require('assert');
describe('validate', function () {
it('error', function () {
var schema = AJS();
const schema = AJS()
try {
schema.validate(0);
} catch(e) {
assert.equal(e.message, 'No schema assigned, please call .compile(schema)');
schema.validate(0)
} catch (e) {
assert.equal(e.message, 'No schema assigned, please call .compile(schema)')
}
});
})

@@ -22,3 +20,3 @@ it('additionalProperties', function () {

gender: { type: 'string', enum: ['male', 'female'] }
});
})

@@ -31,3 +29,6 @@ assert.deepEqual(userSchema.validate({

pet: 'cat'
}), { valid: true, error: null, result:
}), {
valid: true,
error: null,
result:
{ _id: '111111111111111111111111',

@@ -37,3 +38,3 @@ name: 'nswbmw',

gender: 'male' }
});
})

@@ -46,3 +47,6 @@ assert.deepEqual(userSchema.validate({

pet: 'cat'
}, { additionalProperties: true }), { valid: true, error: null, result:
}, { additionalProperties: true }), {
valid: true,
error: null,
result:
{ _id: '111111111111111111111111',

@@ -53,8 +57,8 @@ name: 'nswbmw',

pet: 'cat' }
});
});
})
})
it('normal', function () {
var schema = AJS({ type: 'string' });
assert.deepEqual(schema.validate('1'), { valid: true, error: null, result: '1' });
let schema = AJS({ type: 'string' })
assert.deepEqual(schema.validate('1'), { valid: true, error: null, result: '1' })
assert.deepEqual(schema.validate(1), { valid: false,

@@ -69,7 +73,7 @@ error:

result: 1
});
})
var schema = AJS([{ type: 'string' }]);
assert.deepEqual(schema.validate('1', { ignoreNodeType: true }), { valid: true, error: null, result: '1' });
assert.deepEqual(schema.validate(['1']), { valid: true, error: null, result: ['1'] });
schema = AJS([{ type: 'string' }])
assert.deepEqual(schema.validate('1', { ignoreNodeType: true }), { valid: true, error: null, result: '1' })
assert.deepEqual(schema.validate(['1']), { valid: true, error: null, result: ['1'] })
assert.deepEqual(schema.validate([2, '1']), { valid: false,

@@ -84,6 +88,6 @@ error:

result: [2, '1']
});
})
var schema = AJS([AJS({ type: 'string' })]);
assert.deepEqual(schema.validate(['1']), { valid: true, error: null, result: ['1'] });
schema = AJS([AJS({ type: 'string' })])
assert.deepEqual(schema.validate(['1']), { valid: true, error: null, result: ['1'] })
assert.deepEqual(schema.validate(['1', 2]), { valid: false,

@@ -98,8 +102,8 @@ error:

result: ['1', 2]
});
})
var userSchema = AJS('userSchema', {
let userSchema = AJS('userSchema', {
_id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
nicknames: [{ type: 'string' }],
});
nicknames: [{ type: 'string' }]
})
assert.deepEqual(userSchema._children._id.validate('1'), {

@@ -113,3 +117,3 @@ valid: false,

schema: 'userSchema' },
result: '1' });
result: '1' })
assert.deepEqual(userSchema.validate({

@@ -124,8 +128,8 @@ _id: '111111111111111111111111',

nicknames: [ 'nswbmw', 'xiaoxingxing' ] }
});
})
var userSchema = AJS('userSchema', {
userSchema = AJS('userSchema', {
_id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
nicknames: [{ type: 'string' }],
});
nicknames: [{ type: 'string' }]
})

@@ -135,3 +139,3 @@ assert.equal(userSchema.validate({

nicknames: 'nswbmw'
}).error.message, '($.nicknames[]: "nswbmw") ✖ (type: array)');
}).error.message, '($.nicknames[]: "nswbmw") ✖ (type: array)')
assert.deepEqual(userSchema.validate({

@@ -149,14 +153,14 @@ _id: '111111111111111111111111',

result: { _id: '111111111111111111111111', nicknames: 'nswbmw' }
});
})
var userSchema = AJS('userSchema', {
userSchema = AJS('userSchema', {
_id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
nicknames: [{ type: 'string' }],
});
assert.equal(userSchema.validate(1).error.message, '($: 1) ✖ (type: object)');
nicknames: [{ type: 'string' }]
})
assert.equal(userSchema.validate(1).error.message, '($: 1) ✖ (type: object)')
var commentSchema = AJS('commentSchema', {
const commentSchema = AJS('commentSchema', {
_id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
content: { type: 'string', required: true }
});
})

@@ -174,4 +178,4 @@ assert.deepEqual(commentSchema.validate({

schema: 'commentSchema' },
result: { _id: '111111111111111111111111', content: [ 'haha', 'hehe' ] } });
});
result: { _id: '111111111111111111111111', content: [ 'haha', 'hehe' ] } })
})

@@ -184,3 +188,3 @@ it('complex', function () {

gender: { type: 'string', enum: ['male', 'female'] }
});
})
var commentSchema = AJS('commentSchema', {

@@ -190,3 +194,3 @@ _id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },

content: { type: 'string', required: true }
});
})
var postSchema = AJS('postSchema', {

@@ -197,3 +201,3 @@ _id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },

comments: [commentSchema]
});
})

@@ -229,32 +233,36 @@ var post1 = {

}]
};
assert.deepEqual(postSchema.validate(post1), { valid: true, error: null, result: {
_id: 'post11111111111111111111',
author: {
_id: 'user11111111111111111111',
name: 'nswbmw',
age: 100,
gender: 'male'
},
content: 'lalala',
comments: [{
_id: 'comment11111111111111111',
user: {
}
assert.deepEqual(postSchema.validate(post1), {
valid: true,
error: null,
result: {
_id: 'post11111111111111111111',
author: {
_id: 'user11111111111111111111',
name: 'user1',
name: 'nswbmw',
age: 100,
gender: 'male'
},
content: 'sofa'
}, {
_id: 'comment22222222222222222',
user: {
_id: 'user22222222222222222222',
name: 'user2',
age: 100,
gender: 'female'
},
content: 'bench'
}]
} });
content: 'lalala',
comments: [{
_id: 'comment11111111111111111',
user: {
_id: 'user11111111111111111111',
name: 'user1',
age: 100,
gender: 'male'
},
content: 'sofa'
}, {
_id: 'comment22222222222222222',
user: {
_id: 'user22222222222222222222',
name: 'user2',
age: 100,
gender: 'female'
},
content: 'bench'
}]
}
})

@@ -290,4 +298,4 @@ var post2 = {

}]
};
assert.deepEqual(postSchema.validate(post2).error.message, '($.comments[].user._id: "wrong_id") ✖ (pattern: /^[0-9a-z]{24}$/)');
}
assert.deepEqual(postSchema.validate(post2).error.message, '($.comments[].user._id: "wrong_id") ✖ (pattern: /^[0-9a-z]{24}$/)')
assert.deepEqual(postSchema.validate(post2), {

@@ -329,4 +337,4 @@ valid: false,

}
});
});
});
})
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc