JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 123
  • Score
    100M100P100Q88260F
  • License ISC

JSON Schema constructor and validator

Package Exports

  • jsonschema-definer

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jsonschema-definer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Welcome to jsonschema-definer 👋

Version License: ISC Release License: ISC

This package provides simple, well typed API for creating and validating JSON Schemas

Install

npm install jsonschema-definer

Usage

This package was inspired by fluent-schema and prop-types, and is used to create and validate JSON Schema. It was written in typescript and provide a lot of usefull info from typings, such as infering interface types from schema. Here is an example:

import S from 'jsonschema-definer'

// Lets define a simple object schema
const UserSchema = S.shape({
  name: S.string(),
  email: S.string().format('email').optional(),
  password: S.string().minLength(8),
  role: S.enum('client', 'suplier'),
  birthday: S.instanceOf(Date)
})

// Now lets get interface of User from schema
type User = typeof UserSchema.type
/*
  type User = {
    name: string,
    email?: string | undefined,
    password: string,
    role: 'client' | 'suplier',
    birthday: Date
  }
*/

// We can validate user using .validate(data) function (ajv used)
const [valid, errors] = UserSchema.validate({
  name: 'Igor',
  email: 'fumo.sujimoshi@gmail.com',
  password: '12345678',
  role: 'client',
  birthday: new Date()
})

console.log(valid, errors) // [boolean, Error[]]

// Or get plain JSON Schema using .valueOf()
console.log(UserSchema.valueOf())

Show your support

Give a ⭐️ if this project helped you!

Documentation

S: SchemaFactory extends BaseSchema

Method Return Description JSON Schema
S.any() BaseSchema<any> Correspond to any type { }
S.string() StringSchema<string> For strings validation { "type": "string" }
S.number() NumericSchema<number> For float/integer validation { "type": "number" }
S.integer() NumericSchema<number> For integer values validation { "type": "integer" }
S.boolean() BaseSchema<boolean> For boolean values { "type": "boolean" }
S.null() BaseSchema<null> For null value validation { "type": "null" }
S.array() ArraySchema<any[]> For arrays { "type": "array" }
S.list(itemType: T) ArraySchema<T[]> Validation of lists. Example: S.list(S.string()): ArraySchema<string[]>> { "type": "array", "items": { ... } }
S.object() ObjectSchema<{}> Validation of object { "type": "object" }
S.shape({ key: Schema }: T) ObjectSchema<T> Validation of objects { "type": "object", properties: T, additionalProperties: false } }
S.instanceOf(type: T) BaseSchema<T> For validating instanceOf data. (Custom keyword used) { instanceOf: T.name }
S.enum(...constants: T[]) BaseSchema<T[0] | T[1] | ...> Enumerable schema { enum: [ T[0], T[1] ] }
S.const(constant: T) BaseSchema<T> Constant value { const: T }
S.anyOf(...schemas: BaseSchema<T>[]) BaseSchema<T[0] | T[1] | ...> Any (one or more) of given types { anyOf: [ T[0], T[1], ... ] }
S.oneOf(...schemas: BaseSchema<T>[]) BaseSchema<T[0] | T[1] | ...> Value shoud correspond to ONE of given types { oneOf: [ T[0], T[1], ... ] }
S.allOf(...schemas: BaseSchema<T>[]) BaseSchema<T[0] & T[1] & ...> Value should correspond to ALL of given type { allOf: [ T[0], T[1], ... ] }
S.raw(values: any) BaseSchema<any> Set custom schema values (For Swagger definitions for example) { ...values }
S.custom(...validators: (value: T) => boolean) BaseSchema<any> Add custom validation functions to schema. Supported by AJV custom keyword { custom: [] } Not standard schema key (Used by AJV)

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Run tests

npm run test

Author

👤 Igor Solomakha fumo.sujimoshi@gmail.com

📝 License

Copyright © 2020 Igor Solomakha fumo.sujimoshi@gmail.com.
This project is ISC licensed.


This README was generated with ❤️ by readme-md-generator