Coder Social home page Coder Social logo

Schema of JSON Schema about typebox HOT 3 CLOSED

MatanYadaev avatar MatanYadaev commented on June 15, 2024
Schema of JSON Schema

from typebox.

Comments (3)

sinclairzx81 avatar sinclairzx81 commented on June 15, 2024 1

@MatanYadaev Hi,

Is there a way to make a schema of JSON schema with TypeBox?

Yes, partially. However you may run into some challenges adequately expressing both the Json Schema specification while retaining type inference (as schematics can get recursive). This said, The following sets up a few Json Schema types and shows the Recursive / Generic pattern self referential schematics.

import { Type, TSchema, Static } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

export const AnyOf = <T extends TSchema>(This: T) => Type.Object({
  anyOf: Type.Array(This)
})
export const AllOf = <T extends TSchema>(This: T) => Type.Object({
  allOf: Type.Array(This)
})
export const Object = <T extends TSchema>(This: T) => Type.Object({
  type: Type.Literal('object'),
  properties: Type.Record(Type.String(), This),
  required: Type.Array(Type.String())
})
export const Array = <T extends TSchema>(This: T) => Type.Object({
  type: Type.Literal('array'),
  items: This
})
export const String = Type.Object({ type: Type.Literal('string') })
export const Number = Type.Object({ type: Type.Literal('number') })
export const Boolean = Type.Object({ type: Type.Literal('boolean') })
export const Null = Type.Object({ type: Type.Literal('null') })

export type Schema = Static<typeof Schema>
export const Schema = Type.Recursive(This => Type.Union([
  AllOf(This),
  AnyOf(This),
  Object(This),
  Array(This),
  String,
  Number,
  Boolean,
  Null
]), { $id: 'Schema' })

// some tests
console.log(Schema)
console.log(Value.Check(Schema, { type: 'null' }))
console.log(Value.Check(Schema, { type: 'string' }))
console.log(Value.Check(Schema, { type: 'object', properties: {
  x: { type: 'number' }
}, required: ['x'] }))

Hope this helps
S

from typebox.

MatanYadaev avatar MatanYadaev commented on June 15, 2024

Thanks @sinclairzx81 for the quick reply.

from typebox.

nounderline avatar nounderline commented on June 15, 2024

Thank you @sinclairzx81

Creating JSON Schema of JSON Schema in TypeBox feels quite meta and powerful :)

from typebox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.