Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@bett3r-dev/jsonschema-definer

Package Overview
Dependencies
Maintainers
3
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bett3r-dev/jsonschema-definer

JSON Schema constructor and validator

latest
Source
npmnpm
Version
1.4.17
Version published
Maintainers
3
Created
Source

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

Full documentation available here

Main exported variable S: SchemaFactory extends BaseSchema

MethodDescriptionJSON Schema
S.any(): BaseSchemaCorrespond to any type{ }
S.string(): StringSchemaFor strings validation{ "type": "string" }
S.number(): NumericSchemaFor float/integer validation{ "type": "number" }
S.integer(): NumericSchemaFor integer values validation{ "type": "integer" }
S.boolean(): BaseSchemaFor boolean values{ "type": "boolean" }
S.null(): BaseSchemaFor null value validation{ "type": "null" }
S.array(): ArraySchemaArray validation{ "type": "array" }
S.list(itemType: T): ArraySchemaValidation of lists. Example: S.list(S.string()): ArraySchema{ "type": "array", "items": { ... } }
S.object(): ObjectSchemaValidation of object{ "type": "object" }
S.shape({ key: Schema }: T): ObjectSchemaValidation of objects{ "type": "object", properties: T, additionalProperties: false } }
S.instanceOf(type: T): BaseSchemaFor validating instanceOf data. (Custom keyword used) { instanceOf: T.name }
S.enum(...constants: T[]): BaseSchemaEnumerable schema{ enum: [ T[0], T[1] ] }
S.const(constant: T): BaseSchemaConstant value{ const: T }
S.anyOf(...schemas: BaseSchema[]): BaseSchemaAny (one or more) of given types{ anyOf: [ T[0], T[1], ... ] }
S.oneOf(...schemas: BaseSchema[]): BaseSchemaValue shoud correspond to ONE of given types{ oneOf: [ T[0], T[1], ... ] }
S.allOf(...schemas: BaseSchema[]): BaseSchemaValue should correspond to ALL of given type{ allOf: [ T[0], T[1], ... ] }
S.raw(values: any): BaseSchemaSet custom schema values (For Swagger definitions for example){ ...values }
S.custom(...validators: (value: T) => boolean): BaseSchemaAdd custom validation functions to schema. Supported by AJV custom keywordDoes not supported by standard JSON Schema (Ajv support)

🤝 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

  • Github: @Sujimoshi

📝 License

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

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

Keywords

json

FAQs

Package last updated on 18 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts