sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.56k stars 148 forks source link

Built-in types for JSON? #863

Closed aleclarson closed 2 months ago

aleclarson commented 2 months ago

Would you consider adding types for JSON values? With "native" support, we could have better error messages (I think? because Union types don't have great errors last I checked), and I wouldn't have to maintain the nightmare below (😆).

My Implementation

import {
  SchemaOptions,
  Static,
  TLiteral,
  TOptional,
  TSchema,
  TUnsafe,
  Type,
} from 'typebox'

const primitiveTypes = [
  Type.String(),
  Type.Number(),
  Type.Boolean(),
  Type.Null(),
  Type.Undefined(),
]

export const JsonPrimitive = (options?: SchemaOptions) =>
  Type.Union(primitiveTypes, options)

export const JsonValue = (options?: SchemaOptions): TUnsafe<JsonValue> =>
  Type.Recursive(JsonValue => {
    return Type.Union([
      ...primitiveTypes,
      Type.Record(Type.String(), JsonValue),
      Type.Array(JsonValue),
    ])
  }, options) as any

export const JsonObject = (options?: SchemaOptions) =>
  Type.Record(Type.String(), JsonValue(), options)

export const JsonArray = (options?: SchemaOptions) =>
  Type.Array(JsonValue(), options)

export type JsonValue =
  | Static<TJsonPrimitive>
  | Static<TJsonObject>
  | JsonValue[]

export type JsonPrimitive = Static<TJsonPrimitive>
export type JsonObject = Static<TJsonObject>
export type JsonArray = Static<TJsonArray>

export type TJsonValue =
  | TJsonPrimitive
  | TJsonObject
  | TJsonArray
  | TJsonUnion
  | TJsonUnsafe

export type TJsonPrimitive =
  | ReturnType<typeof JsonPrimitive>['anyOf'][number]
  | TLiteral

export type TJsonObject = TSchema & {
  properties: TJsonProperties
  static: { [key: string]: JsonValue }
}

export type TJsonProperties = {
  [key: string]: TJsonValue | TOptional<TJsonValue>
}

export type TJsonArray = TSchema & {
  items: TJsonValue
  static: JsonValue[]
}

export type TJsonUnion = TSchema & {
  anyOf: TJsonValue[]
  static: JsonValue
}

export type TJsonUnsafe = TSchema & {
  static: JsonValue
}
sinclairzx81 commented 2 months ago

@aleclarson Hi! Hey, sorry for the delay in reply (have been a bit under the weather this week)

Would you consider adding types for JSON values? With "native" support, we could have better error messages (I think? because Union types don't have great errors last I checked), and I wouldn't have to maintain the nightmare below (😆).

So, thanks for the suggestion. Unfortunately, I don't think I can take on this type on as native. But can maybe offer some help to try and simplify the representation. The following is how I'd represent this type in TypeScript (with the intent to only allow Json encodable values)

type JsonObject = { [key: string]: JsonValue }
type JsonArray = JsonValue[]
type JsonValue = (
  JsonObject | 
  JsonArray | 
  string | 
  number | 
  boolean | 
  null
)

The following is the TB representation (which is close to what you have), but where the static types are derived via instantiation expression (ReturnType + Generic Parameter)

const JsonObject = <T extends TSchema>(schema: T) => Type.Record(Type.String(), schema)
const JsonArray = <T extends TSchema>(schema: T) => Type.Array(schema)
const JsonValue = Type.Recursive(This => Type.Union([
    JsonObject(This),
    JsonArray(This),
    Type.String(),
    Type.Number(),
    Type.Boolean(),
    Type.Null()
]))

// static types derived from instantiation expression (passing JsonValue)
type JsonObject = Static<ReturnType<typeof JsonObject<typeof JsonValue>>>
type JsonArray = Static<ReturnType<typeof JsonArray<typeof JsonValue>>>
type JsonString = string
type JsonNumber = number
type JsonBoolean = boolean
type JsonNull = null

Alternatively, this type can be represented as the following (which is a bit more loose but works)

type JsonObject = { [key: string]: JsonValue }
type JsonArray = JsonValue[]
type JsonValue = (
  JsonObject | 
  JsonArray | 
  string | 
  number | 
  boolean | 
  null
)

const JsonValue = Type.Unsafe<JsonValue>(Type.Any())

So, TypeBox mostly tries to provide analogs for types that can be written in TypeScript, but not to implement specific types such as JsonValue (as these should be constructable via the type builder). The goal is mostly to push the capabilities of TB's type builder such that it has 1-1 parity with TypeScript, and where providing specific constructs (such as JsonValue) moves away from this goal (even though the implementation of analogous types can be somewhat cumbersome). Making representing types of this nature easier from a library design standpoint, that is very much in scope! :) I have been keeping a side project below to explore analogous mappings from TS to TB if you're interested (which is a work in progress)

https://sinclairzx81.github.io/typebox-workbench

Will close off this issue for now, but let me know if the above works for your use case. I am currently carrying out preliminary design work for the next significant iteration of the TB type builder (which should go out on 0.33.x or 0.34.x), so during these phases, I've very much open to discussing how the builder can be improved and factor user feedback into newer builder / inference designs.

Thanks again S