All Projects → nswbmw → another-json-schema

nswbmw / another-json-schema

Licence: other
Another JSON Schema validator, simple & flexible & intuitive.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to another-json-schema

ty
Here is a schema checker which can return well typed results. Tell your friends!
Stars: ✭ 21 (-56.25%)
Mutual labels:  schema, json-schema, validator
Schm
Composable schemas for JavaScript and Node.js
Stars: ✭ 498 (+937.5%)
Mutual labels:  schema, json-schema
Plank
A tool for generating immutable model objects
Stars: ✭ 449 (+835.42%)
Mutual labels:  schema, json-schema
Schema Registry
Confluent Schema Registry for Kafka
Stars: ✭ 1,647 (+3331.25%)
Mutual labels:  schema, json-schema
Jsonschema
An implementation of the JSON Schema specification for Python
Stars: ✭ 3,474 (+7137.5%)
Mutual labels:  schema, json-schema
Json Schema To Ts
Infer TS types from JSON schemas 📝
Stars: ✭ 261 (+443.75%)
Mutual labels:  schema, json-schema
Autocomplete Json
Atom autocomplete for JSON files
Stars: ✭ 60 (+25%)
Mutual labels:  schema, json-schema
sf-java-ui
Json Schema Form java based library allow developers to define schema and form using field annotations
Stars: ✭ 23 (-52.08%)
Mutual labels:  schema, json-schema
Awesome Python Models
A curated list of awesome Python libraries, which implement models, schemas, serializers/deserializers, ODM's/ORM's, Active Records or similar patterns.
Stars: ✭ 124 (+158.33%)
Mutual labels:  schema, validator
Schema Typed
Schema for data modeling & validation
Stars: ✭ 150 (+212.5%)
Mutual labels:  schema, validator
Schema Utils
Options Validation
Stars: ✭ 162 (+237.5%)
Mutual labels:  schema, validator
fform
Flexibile and extendable form builder with constructor
Stars: ✭ 26 (-45.83%)
Mutual labels:  schema, json-schema
schema-shot
Framework-agnostic snapshot testing using "schema by example" for highly dynamic data
Stars: ✭ 34 (-29.17%)
Mutual labels:  schema, json-schema
Vue Json Ui Editor
Edit JSON in UI form with JSON Schema and Vue.js
Stars: ✭ 392 (+716.67%)
Mutual labels:  schema, json-schema
Joiful
TypeScript Declarative Validation for Joi
Stars: ✭ 177 (+268.75%)
Mutual labels:  schema, validator
Fastest Validator
⚡️ The fastest JS validator library for NodeJS
Stars: ✭ 923 (+1822.92%)
Mutual labels:  schema, validator
volder
volder is powerful Object schema validation lets you describe your data using a simple and readable schema and transform a value to match the requirements
Stars: ✭ 106 (+120.83%)
Mutual labels:  schema, validator
element-schema-form
A schema-based element-ui form component for Vue2.x.
Stars: ✭ 31 (-35.42%)
Mutual labels:  schema, json-schema
Typedload
Python library to load dynamically typed data into statically typed data structures
Stars: ✭ 120 (+150%)
Mutual labels:  schema, json-schema
Newtonsoft.json.schema
Json.NET Schema is a powerful, complete and easy to use JSON Schema framework for .NET
Stars: ✭ 167 (+247.92%)
Mutual labels:  schema, json-schema

another-json-schema

Another JSON Schema, simple & flexible & intuitive.

NPM version Build status Dependency Status License Downloads

Install

$ npm i another-json-schema --save

Usage

Simple

const AJS = require('another-json-schema')

const userSchema = AJS('userSchema', {
  name: { type: 'string', required: true },
  age: { type: 'number', gte: 18 },
  gender: { type: 'string', enum: ['male', 'female'], default: 'male' },
  email: { type: 'string', isEmail: true }
})

// test `required`
console.log(userSchema.validate({ age: 18 }))
/*
{ valid: false,
  error:
   { Error: ($.name: undefined) ✖ (required: true)
     validator: 'required',
     actual: undefined,
     expected: { type: 'string', required: true },
     path: '$.name',
     schema: 'userSchema' },
  result: { age: 18 } }
*/

// test `default`
const data = { name: 'nswbmw', age: 18 }
console.log(userSchema.validate(data))
/*
{ valid: true,
  error: null,
  result: { name: 'nswbmw', age: 18, gender: 'male' } }
*/
console.log(data)
// { name: 'nswbmw', age: 18, gender: 'male' }

// test `enum`
console.log(userSchema.validate({ name: 'nswbmw', age: 18, gender: 'lalala' }))
/*
{ valid: false,
  error:
   { Error: ($.gender: "lalala") ✖ (enum: male,female)
     validator: 'enum',
     actual: 'lalala',
     expected: { type: 'string', enum: ['male', 'female'], default: 'male' },
     path: '$.gender',
     schema: 'userSchema' },
  result: { name: 'nswbmw', age: 18, gender: 'lalala' } }
*/

// test `gte`
console.log(userSchema.validate({ name: 'nswbmw', age: 17 }))
/*
{ valid: false,
  error:
   { Error: ($.age: 17) ✖ (gte: 18)
     validator: 'gte',
     actual: 17,
     expected: { type: 'number', gte: 18 },
     path: '$.age',
     schema: 'userSchema' },
  result: { name: 'nswbmw', age: 17 } }
*/

// test `isEmail`
console.log(userSchema.validate({ name: 'nswbmw', email: 'myEmail' }))
/*
{ valid: false,
  error:
   { Error: ($.email: "myEmail") ✖ (isEmail: true)
     validator: 'isEmail',
     path: '$.email',
     actual: 'myEmail',
     expected: { type: 'string', isEmail: true },
     schema: 'userSchema' },
  result: { name: 'nswbmw', email: 'myEmail', gender: 'male' } }
*/

Nested

const AJS = require('another-json-schema')

const userSchema = AJS('userSchema', {
  _id: { type: 'string', pattern: /^[0-9a-z]{24}$/, required: true },
  name: { type: 'string' },
  age: { type: 'number', gte: 18 },
  gender: { type: 'string', enum: ['male', 'female'] }
})

const commentSchema = AJS('commentSchema', {
  _id: { type: 'string', pattern: /^[0-9a-z]{24}$/, required: true },
  user: userSchema,
  content: { type: 'string' }
})

const postSchema = AJS('postSchema', {
  _id: { type: 'string', pattern: /^[0-9a-z]{24}$/, required: true },
  author: userSchema,
  content: { type: 'string' },
  comments: [commentSchema]
})

const post = {
  _id: 'post11111111111111111111',
  author: {
    _id: 'user11111111111111111111',
    name: 'nswbmw',
    age: 100,
    gender: 'male',
    pet: 'cat'
  },
  content: 'lalala',
  comments: [{
    _id: 'comment11111111111111111',
    user: {
      _id: 'wrong_id',
      name: 'user1',
      age: 100,
      gender: 'male'
    },
    content: 'sofa'
  }]
}

console.log(postSchema.validate(post))
/*
{ valid: false,
  error:
   { [Error: ($.comments[].user._id: "wrong_id") ✖ (pattern: /^[0-9a-z]{24}$/)]
     validator: 'pattern',
     actual: 'wrong_id',
     expected: { type: 'string', pattern: /^[0-9a-z]{24}$/, required: true },
     path: '$.comments[].user._id',
     schema: 'userSchema' },
  result:
   { _id: 'post11111111111111111111',
     author:
      { _id: 'user11111111111111111111',
        name: 'nswbmw',
        age: 100,
        gender: 'male' },
     content: 'lalala',
     comments: [ [Object] ] } }
*/

Custom Error Message

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

const AJS = require('another-json-schema')

AJS.register('adult', function (actual, expected, key, parent) {
  return expected ? (actual > 18) : (actual <= 18)
})
const adultSchema = AJS('adultSchema', { type: 'number', adult: true })

console.log(adultSchema.validate(19))
// { valid: true, error: null, result: 19 }
console.log(adultSchema.validate(17))
/*
{ valid: false,
  error:
   { Error: ($: 17) ✖ (adult: true)
     validator: 'adult',
     actual: 17,
     expected: { type: 'number', adult: true },
     path: '$',
     schema: 'adultSchema' },
  result: 17 }
*/

Custom type validate function

Custom ObjectId validator, check whether ObjectId then wrap _id string to ObjectId.

const AJS = require('another-json-schema')
const validator = require('validator')
const toObjectId = require('mongodb').ObjectId

function ObjectId(actual, key, parent) {
  if (!actual || !validator.isMongoId(actual.toString())) {
    return false
  }
  parent[key] = toObjectId(actual)
  return true
}

const postSchema = AJS('postSchema', {
  commentIds: [{ type: ObjectId }]
})

const user = {
  commentIds: [
    '111111111111111111111111',
    '222222222222222222222222'
  ]
}

console.log(postSchema.validate(user))
/*
{ valid: true,
  error: null,
  result: { commentIds: [ 111111111111111111111111, 222222222222222222222222 ] } }
*/

//validate specific field
console.log(postSchema._children.commentIds.validate('lalala'))
/*
{ valid: false,
  error:
   { Error: ($.commentIds[]: "lalala") ✖ (type: ObjectId)
     validator: 'type',
     path: '$.commentIds[]',
     actual: 'lalala',
     expected: [ [Object] ],
     schema: 'postSchema' },
  result: 'lalala' }
*/

Internal type validator

  • 'string'
  • 'number'
  • 'boolean'
  • AJS.Types.ObjectId
  • AJS.Types.String
  • AJS.Types.Number
  • AJS.Types.Integer
  • AJS.Types.Double
  • AJS.Types.Date
  • AJS.Types.Buffer
  • AJS.Types.Boolean
  • AJS.Types.Mixed

What's difference between number and AJS.Types.Number ? number only check type, AJS.Types.Number will try to convert value to a number, if failed then throw error.

const AJS = require('/Users/nswbmw/work/GitHub/Node.js/another-json-schema')

const postSchema = AJS('postSchema', {
  commentIds: [{ type: AJS.Types.ObjectId }]
})

const user = {
  commentIds: [
    '111111111111111111111111',
    '222222222222222222222222'
  ]
}

console.log(postSchema.validate(user))
/*
{ valid: true,
  error: null,
  result: { commentIds: [ 111111111111111111111111, 222222222222222222222222 ] } }
*/

//validate specific field
console.log(postSchema._children.commentIds.validate('lalala'))
/*
{ valid: false,
  error:
   { Error: ($.commentIds[]: "lalala") ✖ (type: ObjectId)
     validator: 'type',
     path: '$.commentIds[]',
     actual: 'lalala',
     expected: [ [Object] ],
     schema: 'postSchema' },
  result: 'lalala' }
*/

Ignore validator

const AJS = require('another-json-schema')

const userSchema = AJS('userSchema', {
  _id: { type: 'number', range: [1, 100] }
})

const user = {
  _id: 0
}

console.log(userSchema.validate(user))
/*
{ valid: false,
  error:
   { Error: ($._id: 0) ✖ (range: 1,100)
     validator: 'range',
     actual: 0,
     expected: { type: 'number', range: [Array] },
     path: '$._id',
     schema: 'userSchema' },
  result: { _id: 0 } }
*/
console.log(userSchema.validate(user, { range: false }))
// { valid: true, error: null, result: { _id: 0 } }

NB: type validator cannot ignore by passing false.

API

AJS([name], schema)

Constructor.

AJS.register(name, fn)

Register a validator. eg:

AJS.register('adult', function (actual, expected, key, parent) {
  return expected ? (actual > 18) : (actual <= 18)
})

ajs.compile([name], schema)

Compile a schema. The following two ways are the same:

const userSchema = AJS('userSchema', {
  _id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
  name: { type: 'string' },
  age: { type: 'number', gte: 18 },
  gender: { type: 'string', enum: ['male', 'female'] }
})
const newSchema = new AJS()
const userSchema = newSchema.compile('userSchema', {
  _id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
  name: { type: 'string' },
  age: { type: 'number', gte: 18 },
  gender: { type: 'string', enum: ['male', 'female'] }
})

compiledSchema.validate(data, [opts])

Use the compiled validator to validate an object. it will modify the original object and return it:

  • valid: {Boolean} wether a valid object
  • error: {Error|null}
    • message: error message, eg: ($.comments[].user._id: "wrong_id") ✖ (pattern: /^[0-9a-z]{24}$/)
    • validator: validator name, eg: pattern,
    • actual: actual value, eg: wrong_id,
    • expected: expected schema, eg: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
    • path: path in object, eg: $.comments[].user._id,
    • schema: schema name, eg: userSchema
    • originError: original error thrown from validator
  • result: {Any}

opts:

  • additionalProperties: {Boolean} if true, retain the original field. default false
  • ignoreNodeType: {Boolean} if true, ignore check node type, like: []. default: false
  • gt, gte, lt, lte ...: {Boolean} if false, will not execute this validator.

Built-in validators

  • type
  • eq|equal
  • gt
  • gte
  • lt
  • lte
  • range
  • enum
  • pattern
  • default
  • required
  • all validator's isXxx validators, eg: isEmail. type validator must be string or AJS.Types.String.

More examples

see test.

Test

$ npm test (coverage 100%)

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].