Skip to content

shqld/simple-shape

Repository files navigation

simple-shape

A simple function for building JSON Schema.

Basic Usage

import Ajv from "ajv"
import { shape } from 'simple-shape'

const ajv = new Ajv()

const Message = shape({
  id: Number,
  type: anyOf('short', 'long'),
  text: String,
})

// TypeOf<typeof Message> -> { id: number, type: 'short' | 'long', text: string }

const message = {
    id: 1,
    type: 'short',
    text: 'Hello, world!',
}

if (ajv.validate<TypeOf<typeof Message>>(shape, message)) {
  message
  // -> { id: number, type: 'short' | 'long', text: string }
}

For details: https://github.com/shqld/simple-shape/blob/main/example.ts

About

A simple function for building JSON Schema

Topics

Resources

Stars

Watchers

Forks