Skip to content

dynamic and Schema

Goal: Parse JSON into typed values with Schema.parse.

@superjs/std-schema provides composable validators returning Validated<T>.

Example

import { string, object, field } from "@superjs/std-schema"

const NameSchema = object([field("name", string())])

function parseName(doc: dynamic): string? {
  const v = NameSchema.parse(doc)
  return match v {
    Valid(payload) => payload.name as string,
    Invalid(_) => null,
  }
}

Open in playground

Key takeaways

  • Schemas are reified — compose with object, optional, etc.
  • Prefer schema validation over repeated typeof chains.
  • See generated std-schema API.

Next: Errors and Result

Documentation