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
superstruct/SourceSource
Code styleCode style
Normal styleNormal style
System themeSystem theme
Light themeLight theme
Dark themeDark theme
github
GitHubGitHub
DiscordDiscord
schemas/libraries/superstruct/download.ts
Copy to clipboardCopy to clipboard
import { object, number, date, string, array, nullable, enums, refine, min, max, create, type Struct, } from "superstruct"; import type { ProductData } from "#src"; function stringWithLength(min: number, max: number) { return refine( string(), `string with length between ${min} and ${max}`, (value) => value.length >= min && value.length <= max, ); } const urlSchema = refine(string(), "url", (value) => URL.canParse(value)); const imageSchema = object({ id: number(), created: date(), title: stringWithLength(1, 100), type: enums(["jpg", "png"]), size: number(), url: urlSchema, }); const ratingSchema = object({ id: number(), stars: max(min(number(), 0), 5), title: stringWithLength(1, 100), text: stringWithLength(1, 1000), images: array(imageSchema), }); const productSchema = object({ id: number(), created: date(), title: stringWithLength(1, 100), brand: stringWithLength(1, 30), description: stringWithLength(1, 500), price: max(min(number(), 1), 10000), discount: nullable(max(min(number(), 1), 100)), quantity: max(min(number(), 0), 10), tags: array(stringWithLength(1, 30)), images: array(imageSchema), ratings: array(ratingSchema), }) satisfies Struct<ProductData>; create({}, productSchema);
Created by eskimojo for Open Circle