Huge News!Announcing our $40M Series B led by Abstract Ventures.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 3.4.0 to 3.5.0

4

changelog.md

@@ -0,1 +1,5 @@

## 3.5.0/2018-04-11
- add `_customErrorMsg`
## 3.4.0/2018-01-09

@@ -2,0 +6,0 @@

2

helpers.js

@@ -50,3 +50,3 @@ const toString = Object.prototype.toString

exports.default = function (actual, expected, key, parent) {
parent[key] = actual || expected
parent[key] = actual != null ? actual : expected
return true

@@ -53,0 +53,0 @@ }

@@ -216,3 +216,3 @@ const helpersFuncs = require('./helpers')

for (let helper in ctx._children) {
if (['type', 'default', 'required'].indexOf(helper) !== -1 || (opts[helper] != null && !opts[helper])) {
if (['type', 'default', 'required', '_customErrorMsg'].indexOf(helper) !== -1 || (opts[helper] != null && !opts[helper])) {
continue

@@ -242,2 +242,3 @@ }

let error
const _customErrorMsg = ctx._schema._customErrorMsg || {}
if (!type) {

@@ -247,16 +248,17 @@ if (helper) {

if (typeof helperEntry === 'function') {
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry.name + ')')
error = new Error('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry.name + ')')
} else {
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry + ')')
error = new Error('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry + ')')
}
error.validator = helper
} else {
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + JSON.stringify(ctx._children) + ')')
error = new Error('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + JSON.stringify(ctx._children) + ')')
error.validator = 'type'
}
} else {
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (type: ' + type + ')')
error = new Error('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (type: ' + type + ')')
error.validator = 'type'
}
error.message = _customErrorMsg[helper] || error.message
error.path = ctx._path

@@ -263,0 +265,0 @@ error.actual = value

{
"name": "another-json-schema",
"version": "3.4.0",
"version": "3.5.0",
"description": "Another JSON Schema, simple & flexible & intuitive.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -35,3 +35,3 @@ ### another-json-schema

error:
{ TypeError: ($.name: undefined) ✖ (required: true)
{ Error: ($.name: undefined) ✖ (required: true)
validator: 'required',

@@ -61,3 +61,3 @@ actual: undefined,

error:
{ TypeError: ($.gender: "lalala") ✖ (enum: male,female)
{ Error: ($.gender: "lalala") ✖ (enum: male,female)
validator: 'enum',

@@ -76,3 +76,3 @@ actual: 'lalala',

error:
{ TypeError: ($.age: 17) ✖ (gte: 18)
{ Error: ($.age: 17) ✖ (gte: 18)
validator: 'gte',

@@ -156,2 +156,37 @@ actual: 17,

### Custom Error Message
```js
const AJS = require('another-json-schema')
const userSchema = AJS('userSchema', {
name: { type: 'string', required: true },
age: {
type: 'number',
gte: 18,
_customErrorMsg: {
gte: '您未满 18 岁'
}
}
})
// test `_customErrorMsg`
console.log(userSchema.validate({
name: 'nswbmw',
age: 17
}))
/*
{ valid: false,
error:
{ Error: 您未满 18 岁
validator: 'gte',
path: '$.age',
actual: 17,
expected: { type: 'number', gte: 18, _customErrorMsg: [Object] },
schema: 'userSchema' },
result: { name: 'nswbmw', age: 17 } }
*/
```
### Register validator

@@ -173,3 +208,3 @@

error:
{ TypeError: ($: 17) ✖ (adult: true)
{ Error: ($: 17) ✖ (adult: true)
validator: 'adult',

@@ -224,3 +259,3 @@ actual: 17,

error:
{ TypeError: ($.commentIds[]: "lalala") ✖ (type: ObjectId)
{ Error: ($.commentIds[]: "lalala") ✖ (type: ObjectId)
validator: 'type',

@@ -252,3 +287,3 @@ path: '$.commentIds[]',

error:
{ TypeError: ($._id: 0) ✖ (range: 1,100)
{ Error: ($._id: 0) ✖ (range: 1,100)
validator: 'range',

@@ -255,0 +290,0 @@ actual: 0,

@@ -14,2 +14,20 @@ const AJS = require('..')

it('_customErrorMsg', function () {
const userSchema = AJS('userSchema', {
name: { type: 'string', required: true },
age: {
type: 'number',
gte: 18,
_customErrorMsg: {
gte: '您未满 18 岁'
}
}
})
assert.deepEqual(userSchema.validate({
name: 'nswbmw',
age: 17
}).error.message, '您未满 18 岁')
})
it('additionalProperties', function () {

@@ -16,0 +34,0 @@ var userSchema = AJS('userSchema', {

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