LogoLogo

Schema Benchmarks

    • home
      Home

    • download_2
      Download

    • timer
      Initialization
    • check_circle
      Validation
    • output_circle
      Parsing
    • swap_horiz
      Codec
    • schema
      Standard Schema
    • format_quote
      String

    • error
      Stack

    • article
      Blog
Code styleCode style
Normal styleNormal style
System themeSystem theme
Light themeLight theme
Dark themeDark theme
Expand sidebarExpand sidebar
runtypes/SourceSource
Code styleCode style
Normal styleNormal style
System themeSystem theme
Light themeLight theme
Dark themeDark theme
github
GitHubGitHub
DiscordDiscord
schemas/libraries/runtypes/download.ts
Copy to clipboardCopy to clipboard
import { Object, String, Number, Array, Literal, Union, InstanceOf } from "runtypes"; import type { ProductData } from "#src"; const StringWithLength = (min: number, max: number) => String.withConstraint((s) => s.length >= min && s.length <= max); const NumberInRange = (min: number, max: number) => Number.withConstraint((n) => n >= min && n <= max); const Image = Object({ id: Number, created: InstanceOf(Date), title: StringWithLength(1, 100), type: Union(Literal("jpg"), Literal("png")), size: Number, url: String.withConstraint((s) => URL.canParse(s)), }); const Rating = Object({ id: Number, stars: NumberInRange(0, 5), title: StringWithLength(1, 100), text: StringWithLength(1, 1000), images: Array(Image), }); const productSchema = Object({ id: Number, created: InstanceOf(Date), title: StringWithLength(1, 100), brand: StringWithLength(1, 30), description: StringWithLength(1, 500), price: NumberInRange(1, 10000), discount: NumberInRange(1, 100).nullable(), quantity: NumberInRange(0, 10), tags: Array(StringWithLength(1, 30)), images: Array(Image), ratings: Array(Rating), }).conform<ProductData>(); productSchema.parse({});
Created by eskimojo for Open Circle