| 1 | /** |
| 2 | * Open, compiler-extensible representation of Effect schemas. |
| 3 | * |
| 4 | * @since 4.0.0 |
| 5 | */ |
| 6 | import * as InternalRecord from "./internal/record.ts" |
| 7 | import * as InternalFromJsonSchemaDocument from "./internal/schema/fromJsonSchemaDocument.ts" |
| 8 | import * as InternalFromRepresentation from "./internal/schema/fromRepresentation.ts" |
| 9 | import * as InternalToCodeDocument from "./internal/schema/toCodeDocument.ts" |
| 10 | import * as InternalToJsonSchemaDocument from "./internal/schema/toJsonSchemaDocument.ts" |
| 11 | import * as InternalToRepresentation from "./internal/schema/toRepresentation.ts" |
| 12 | import type * as JsonSchema from "./JsonSchema.ts" |
| 13 | import * as Option from "./Option.ts" |
| 14 | import * as Schema from "./Schema.ts" |
| 15 | import * as SchemaAST from "./SchemaAST.ts" |
| 16 | import * as InternalGetter from "./SchemaGetter.ts" |
| 17 | |
| 18 | /** |
| 19 | * Open persistence identity carried by declarations and opaque checks. |
| 20 | * |
| 21 | * @category annotations |
| 22 | * @since 4.0.0 |
| 23 | */ |
| 24 | export interface RepresentationAnnotation { |
| 25 | readonly id: string |
| 26 | readonly payload: Schema.Json |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Open persistence identity and schema dependencies carried by opaque checks. |
| 31 | * |
| 32 | * @category annotations |
| 33 | * @since 4.0.0 |
| 34 | */ |
| 35 | export interface CheckRepresentationAnnotation<S> extends RepresentationAnnotation { |
| 36 | readonly schemas?: ReadonlyArray<S> | undefined |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Input and output contracts for JSON Schema compiler annotations. |
| 41 | * |
| 42 | * @since 4.0.0 |
| 43 | */ |
| 44 | export declare namespace ToJsonSchema { |
| 45 | /** |
| 46 | * Input for a check compiler. |
| 47 | * |
| 48 | * @category models |
| 49 | * @since 4.0.0 |
| 50 | */ |
| 51 | export interface CheckInput { |
| 52 | readonly type: JsonSchema.Type | undefined |
| 53 | readonly schemas: ReadonlyArray<JsonSchema.JsonSchema> |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * The result of compiling a check to JSON Schema. |
| 58 | * |
| 59 | * **Details** |
| 60 | * |
| 61 | * Return the JSON Schema fragment directly when it represents the check |
| 62 | * exactly. Return `[schema, true]` when the fragment is a safe, looser |
| 63 | * approximation: it must accept every value accepted by the check, but may |
| 64 | * accept additional values. Use `[{}, true]` when the constraint is omitted; |
| 65 | * a bare `{}` declares that the check imposes no constraint. |
| 66 | * |
| 67 | * Approximation propagates through enclosing schemas and dependencies listed |
| 68 | * in `representation.schemas`, including recursive references. A `oneOf` |
| 69 | * union with an approximate branch exports as `anyOf`. Approximate record-key |
| 70 | * patterns cannot be used as `patternProperties` selectors. |
| 71 | * |
| 72 | * **Gotchas** |
| 73 | * |
| 74 | * The compiler trusts the callback's declaration; it does not prove that the |
| 75 | * fragment is exact or safely looser. Returning a plain fragment does not |
| 76 | * override approximation inherited from a schema dependency. |
| 77 | * |
| 78 | * **Example** (Declaring a Unicode length approximation) |
| 79 | * |
| 80 | * ```ts import.meta.vitest |
| 81 | * import { Schema } from "effect" |
| 82 | * |
| 83 | * const short = Schema.String.check(Schema.makeFilter( |
| 84 | * (value: string) => value.length <= 1, |
| 85 | * { toJsonSchema: () => [{ maxLength: 1 }, true] } |
| 86 | * )) |
| 87 | * const schema = Schema.Union([short, Schema.Literal("😀")], { mode: "oneOf" }) |
| 88 | * const document = Schema.toJsonSchemaDocument(schema) |
| 89 | * |
| 90 | * Schema.is(schema)("😀") // => true |
| 91 | * document.schema.oneOf // => undefined |
| 92 | * document.schema.anyOf // => [{ type: "string", maxLength: 1 }, { type: "string", enum: ["😀"] }] |
| 93 | * ``` |
| 94 | * |
| 95 | * @category models |
| 96 | * @since 4.0.0 |
| 97 | */ |
| 98 | export type CheckOutput = JsonSchema.JsonSchema | readonly [schema: JsonSchema.JsonSchema, approximate: true] |
| 99 | |
| 100 | /** |
| 101 | * Compiles a check to a JSON Schema fragment. |
| 102 | * |
| 103 | * **Details** |
| 104 | * |
| 105 | * Return a fragment for an exact translation or `[fragment, true]` for a safe, |
| 106 | * looser approximation. Dependencies in `representation.schemas` are compiled |
| 107 | * into the input's `schemas` array; their approximation status propagates automatically. |
| 108 | * |
| 109 | * **Gotchas** |
| 110 | * |
| 111 | * Treat the input schemas as immutable. The returned fragment must be a valid JSON Schema object graph and must not be |
| 112 | * mutated after this function returns. Local `$defs` references must use valid JSON Pointer URI fragments. Return a |
| 113 | * new object graph to produce different output during a later compilation. |
| 114 | * |
| 115 | * @see {@link CheckOutput} for exact and approximate results |
| 116 | * |
| 117 | * @category models |
| 118 | * @since 4.0.0 |
| 119 | */ |
| 120 | export type Check = (input: CheckInput) => CheckOutput |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Input and output contracts for code generation annotations. |
| 125 | * |
| 126 | * @since 4.0.0 |
| 127 | */ |
| 128 | export declare namespace Generation { |
| 129 | /** |
| 130 | * Input for declaration code generation. |
| 131 | * |
| 132 | * @category models |
| 133 | * @since 4.0.0 |
| 134 | */ |
| 135 | export interface DeclarationInput { |
| 136 | readonly typeParameters: ReadonlyArray<Code> |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Output of declaration code generation. |
| 141 | * |
| 142 | * @category models |
| 143 | * @since 4.0.0 |
| 144 | */ |
| 145 | export interface DeclarationOutput { |
| 146 | readonly runtime: string |
| 147 | readonly Type: string |
| 148 | readonly importDeclarations?: ReadonlyArray<string> | undefined |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Declaration code generator. |
| 153 | * |
| 154 | * @category models |
| 155 | * @since 4.0.0 |
| 156 | */ |
| 157 | export type Declaration = (input: DeclarationInput) => DeclarationOutput |
| 158 | |
| 159 | /** |
| 160 | * Input for check code generation. |
| 161 | * |
| 162 | * @category models |
| 163 | * @since 4.0.0 |
| 164 | */ |
| 165 | export interface CheckInput { |
| 166 | readonly schemas: ReadonlyArray<Code> |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Output of check code generation. |
| 171 | * |
| 172 | * @category models |
| 173 | * @since 4.0.0 |
| 174 | */ |
| 175 | export interface CheckOutput { |
| 176 | readonly runtime: string |
| 177 | readonly importDeclarations?: ReadonlyArray<string> | undefined |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Check code generator. |
| 182 | * |
| 183 | * @category models |
| 184 | * @since 4.0.0 |
| 185 | */ |
| 186 | export type Check = (input: CheckInput) => CheckOutput |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * A custom opaque declaration. |
| 191 | * |
| 192 | * @category models |
| 193 | * @since 4.0.0 |
| 194 | */ |
| 195 | export interface Declaration { |
| 196 | readonly _tag: "Declaration" |
| 197 | readonly representation?: RepresentationAnnotation | undefined |
| 198 | readonly annotations?: Schema.Annotations.Annotations | undefined |
| 199 | readonly typeParameters: ReadonlyArray<Representation> |
| 200 | readonly checks: ReadonlyArray<Check> |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * A lazily resolved representation. |
| 205 | * |
| 206 | * @category models |
| 207 | * @since 4.0.0 |
| 208 | */ |
| 209 | export interface Suspend { |
| 210 | readonly _tag: "Suspend" |
| 211 | readonly annotations?: Schema.Annotations.Annotations | undefined |
| 212 | readonly checks: readonly [] |
| 213 | readonly thunk: Representation |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * A named reference. |
| 218 | * |
| 219 | * @category models |
| 220 | * @since 4.0.0 |
| 221 | */ |
| 222 | export interface Reference { |
| 223 | readonly _tag: "Reference" |
| 224 | readonly $ref: string |
| 225 | } |
| 226 | |
| 227 | interface Keyword<Tag extends string> { |
| 228 | readonly _tag: Tag |
| 229 | readonly annotations?: Schema.Annotations.Annotations | undefined |
| 230 | readonly checks: ReadonlyArray<Check> |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * The null keyword representation. |
| 235 | * |
| 236 | * @category models |
| 237 | * @since 4.0.0 |
| 238 | */ |
| 239 | export interface Null extends Keyword<"Null"> {} |
| 240 | /** |
| 241 | * The undefined keyword representation. |
| 242 | * |
| 243 | * @category models |
| 244 | * @since 4.0.0 |
| 245 | */ |
| 246 | export interface Undefined extends Keyword<"Undefined"> {} |
| 247 | /** |
| 248 | * The void keyword representation. |
| 249 | * |
| 250 | * @category models |
| 251 | * @since 4.0.0 |
| 252 | */ |
| 253 | export interface Void extends Keyword<"Void"> {} |
| 254 | /** |
| 255 | * The never keyword representation. |
| 256 | * |
| 257 | * @category models |
| 258 | * @since 4.0.0 |
| 259 | */ |
| 260 | export interface Never extends Keyword<"Never"> {} |
| 261 | /** |
| 262 | * The unknown keyword representation. |
| 263 | * |
| 264 | * @category models |
| 265 | * @since 4.0.0 |
| 266 | */ |
| 267 | export interface Unknown extends Keyword<"Unknown"> {} |
| 268 | /** |
| 269 | * The any keyword representation. |
| 270 | * |
| 271 | * @category models |
| 272 | * @since 4.0.0 |
| 273 | */ |
| 274 | export interface Any extends Keyword<"Any"> {} |
| 275 | |
| 276 | /** |
| 277 | * A string representation. |
| 278 | * |
| 279 | * @category models |
| 280 | * @since 4.0.0 |
| 281 | */ |
| 282 | export interface String extends Keyword<"String"> {} |
| 283 | |
| 284 | /** |
| 285 | * A number representation. |
| 286 | * |
| 287 | * @category models |
| 288 | * @since 4.0.0 |
| 289 | */ |
| 290 | export interface Number extends Keyword<"Number"> {} |
| 291 | /** |
| 292 | * A boolean representation. |
| 293 | * |
| 294 | * @category models |
| 295 | * @since 4.0.0 |
| 296 | */ |
| 297 | export interface Boolean extends Keyword<"Boolean"> {} |
| 298 | /** |
| 299 | * A bigint representation. |
| 300 | * |
| 301 | * @category models |
| 302 | * @since 4.0.0 |
| 303 | */ |
| 304 | export interface BigInt extends Keyword<"BigInt"> {} |
| 305 | /** |
| 306 | * A symbol representation. |
| 307 | * |
| 308 | * @category models |
| 309 | * @since 4.0.0 |
| 310 | */ |
| 311 | export interface Symbol extends Keyword<"Symbol"> {} |
| 312 | |
| 313 | /** |
| 314 | * A literal representation. |
| 315 | * |
| 316 | * **Details** |
| 317 | * |
| 318 | * The live representation stores the native literal value. Persistent codecs |
| 319 | * add an explicit type discriminator when encoding it. |
| 320 | * |
| 321 | * @category models |
| 322 | * @since 4.0.0 |
| 323 | */ |
| 324 | export interface Literal extends Keyword<"Literal"> { |
| 325 | readonly literal: SchemaAST.LiteralValue |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * A unique symbol representation. |
| 330 | * |
| 331 | * **Details** |
| 332 | * |
| 333 | * Globally registered symbols have an exact JSON string representation. Local |
| 334 | * symbols have no JSON representation. |
| 335 | * |
| 336 | * @category models |
| 337 | * @since 4.0.0 |
| 338 | */ |
| 339 | export interface UniqueSymbol extends Keyword<"UniqueSymbol"> { |
| 340 | readonly symbol: symbol |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * The object keyword representation. |
| 345 | * |
| 346 | * @category models |
| 347 | * @since 4.0.0 |
| 348 | */ |
| 349 | export interface ObjectKeyword extends Keyword<"ObjectKeyword"> {} |
| 350 | |
| 351 | /** |
| 352 | * An enum representation. |
| 353 | * |
| 354 | * **Details** |
| 355 | * |
| 356 | * Enum members are stored as native string or finite number values. Persistent |
| 357 | * codecs add an explicit type discriminator when encoding them. |
| 358 | * |
| 359 | * @category models |
| 360 | * @since 4.0.0 |
| 361 | */ |
| 362 | export interface Enum extends Keyword<"Enum"> { |
| 363 | readonly enums: ReadonlyArray<readonly [string, string | number]> |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * A template literal representation. |
| 368 | * |
| 369 | * @category models |
| 370 | * @since 4.0.0 |
| 371 | */ |
| 372 | export interface TemplateLiteral extends Keyword<"TemplateLiteral"> { |
| 373 | readonly parts: ReadonlyArray<Representation> |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * A tuple element. |
| 378 | * |
| 379 | * @category models |
| 380 | * @since 4.0.0 |
| 381 | */ |
| 382 | export interface Element { |
| 383 | readonly isOptional: boolean |
| 384 | readonly type: Representation |
| 385 | readonly annotations?: Schema.Annotations.Annotations | undefined |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * An array or tuple representation. |
| 390 | * |
| 391 | * @category models |
| 392 | * @since 4.0.0 |
| 393 | */ |
| 394 | export interface Arrays extends Keyword<"Arrays"> { |
| 395 | readonly elements: ReadonlyArray<Element> |
| 396 | readonly rest: ReadonlyArray<Representation> |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * A property signature. |
| 401 | * |
| 402 | * **Details** |
| 403 | * |
| 404 | * The live representation stores the native property key. Persistent codecs |
| 405 | * add an explicit type discriminator when encoding it. |
| 406 | * |
| 407 | * **Gotchas** |
| 408 | * |
| 409 | * Local symbols can be represented while the schema is live, but persistent |
| 410 | * codecs reject them because they cannot be reconstructed by identity. |
| 411 | * |
| 412 | * @category models |
| 413 | * @since 4.0.0 |
| 414 | */ |
| 415 | export interface PropertySignature { |
| 416 | readonly name: PropertyKey |
| 417 | readonly type: Representation |
| 418 | readonly isOptional: boolean |
| 419 | readonly isMutable: boolean |
| 420 | readonly annotations?: Schema.Annotations.Annotations | undefined |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * An index signature. |
| 425 | * |
| 426 | * @category models |
| 427 | * @since 4.0.0 |
| 428 | */ |
| 429 | export interface IndexSignature { |
| 430 | readonly parameter: Representation |
| 431 | readonly type: Representation |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * An object representation. |
| 436 | * |
| 437 | * @category models |
| 438 | * @since 4.0.0 |
| 439 | */ |
| 440 | export interface Objects extends Keyword<"Objects"> { |
| 441 | readonly propertySignatures: ReadonlyArray<PropertySignature> |
| 442 | readonly indexSignatures: ReadonlyArray<IndexSignature> |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * A union representation. |
| 447 | * |
| 448 | * @category models |
| 449 | * @since 4.0.0 |
| 450 | */ |
| 451 | export interface Union extends Keyword<"Union"> { |
| 452 | readonly types: ReadonlyArray<Representation> |
| 453 | readonly options?: SchemaAST.UnionOptions | undefined |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * The structural schema representation. |
| 458 | * |
| 459 | * @category models |
| 460 | * @since 4.0.0 |
| 461 | */ |
| 462 | export type Representation = |
| 463 | | Declaration |
| 464 | | Reference |
| 465 | | Suspend |
| 466 | | Null |
| 467 | | Undefined |
| 468 | | Void |
| 469 | | Never |
| 470 | | Unknown |
| 471 | | Any |
| 472 | | String |
| 473 | | Number |
| 474 | | Boolean |
| 475 | | BigInt |
| 476 | | Symbol |
| 477 | | Literal |
| 478 | | UniqueSymbol |
| 479 | | ObjectKeyword |
| 480 | | Enum |
| 481 | | TemplateLiteral |
| 482 | | Arrays |
| 483 | | Objects |
| 484 | | Union |
| 485 | |
| 486 | /** |
| 487 | * A structural check. |
| 488 | * |
| 489 | * @category models |
| 490 | * @since 4.0.0 |
| 491 | */ |
| 492 | export type Check = Filter | FilterGroup |
| 493 | |
| 494 | /** |
| 495 | * An opaque leaf check. |
| 496 | * |
| 497 | * @category models |
| 498 | * @since 4.0.0 |
| 499 | */ |
| 500 | export interface Filter { |
| 501 | readonly _tag: "Filter" |
| 502 | readonly representation?: CheckRepresentationAnnotation<Representation> | undefined |
| 503 | readonly annotations?: Schema.Annotations.Annotations | undefined |
| 504 | readonly aborted: boolean |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * A non-empty group of checks. |
| 509 | * |
| 510 | * @category models |
| 511 | * @since 4.0.0 |
| 512 | */ |
| 513 | export interface FilterGroup { |
| 514 | readonly _tag: "FilterGroup" |
| 515 | readonly representation?: CheckRepresentationAnnotation<Representation> | undefined |
| 516 | readonly annotations?: Schema.Annotations.Annotations | undefined |
| 517 | readonly checks: readonly [Check, ...Array<Check>] |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Named representation definitions. |
| 522 | * |
| 523 | * @category models |
| 524 | * @since 4.0.0 |
| 525 | */ |
| 526 | export interface References { |
| 527 | readonly [$ref: string]: Representation |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * A single representation and its definitions. |
| 532 | * |
| 533 | * @category models |
| 534 | * @since 4.0.0 |
| 535 | */ |
| 536 | export interface Document { |
| 537 | readonly representation: Representation |
| 538 | readonly references: References |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Multiple representations sharing definitions. |
| 543 | * |
| 544 | * @category models |
| 545 | * @since 4.0.0 |
| 546 | */ |
| 547 | export interface MultiDocument { |
| 548 | readonly representations: readonly [Representation, ...Array<Representation>] |
| 549 | readonly references: References |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Reviver for a declaration. |
| 554 | * |
| 555 | * @category models |
| 556 | * @since 4.0.0 |
| 557 | */ |
| 558 | export interface DeclarationReviver<P> { |
| 559 | readonly id: string |
| 560 | readonly payloadSchema: Schema.Decoder<P> |
| 561 | readonly revive: (input: { |
| 562 | readonly payload: P |
| 563 | readonly typeParameters: ReadonlyArray<Schema.Top> |
| 564 | readonly annotations: Schema.Annotations.Annotations | undefined |
| 565 | }) => Schema.Top |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Reviver for a leaf check. |
| 570 | * |
| 571 | * @category models |
| 572 | * @since 4.0.0 |
| 573 | */ |
| 574 | export interface FilterReviver<P> { |
| 575 | readonly id: string |
| 576 | readonly payloadSchema: Schema.Decoder<P> |
| 577 | readonly revive: (input: { |
| 578 | readonly payload: P |
| 579 | readonly schemas: ReadonlyArray<Schema.Top> |
| 580 | readonly annotations: Schema.Annotations.Filter | undefined |
| 581 | }) => SchemaAST.Filter<any> |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Reviver for a check group. |
| 586 | * |
| 587 | * @category models |
| 588 | * @since 4.0.0 |
| 589 | */ |
| 590 | export interface FilterGroupReviver<P> { |
| 591 | readonly id: string |
| 592 | readonly payloadSchema: Schema.Decoder<P> |
| 593 | readonly revive: (input: { |
| 594 | readonly payload: P |
| 595 | readonly schemas: ReadonlyArray<Schema.Top> |
| 596 | readonly annotations: Schema.Annotations.Filter | undefined |
| 597 | }) => SchemaAST.FilterGroup<any> |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * A check reviver. |
| 602 | * |
| 603 | * @category models |
| 604 | * @since 4.0.0 |
| 605 | */ |
| 606 | export type CheckReviver<P> = FilterReviver<P> | FilterGroupReviver<P> |
| 607 | |
| 608 | /** |
| 609 | * A typed reviver. |
| 610 | * |
| 611 | * @category models |
| 612 | * @since 4.0.0 |
| 613 | */ |
| 614 | export type Reviver<P> = DeclarationReviver<P> | CheckReviver<P> |
| 615 | |
| 616 | /** |
| 617 | * A reviver erased only at collection boundaries. |
| 618 | * |
| 619 | * @category models |
| 620 | * @since 4.0.0 |
| 621 | */ |
| 622 | export type AnyReviver = Reviver<any> |
| 623 | |
| 624 | /** |
| 625 | * Creates a declaration reviver while inferring its payload type from `payloadSchema`. |
| 626 | * |
| 627 | * @category constructors |
| 628 | * @since 4.0.0 |
| 629 | */ |
| 630 | export const makeReviverDeclaration: <P>( |
| 631 | id: string, |
| 632 | payloadSchema: Schema.Decoder<P>, |
| 633 | revive: DeclarationReviver<P>["revive"] |
| 634 | ) => DeclarationReviver<P> = (id, payloadSchema, revive) => ({ id, payloadSchema, revive }) |
| 635 | |
| 636 | /** |
| 637 | * Creates a filter reviver while inferring its payload type from `payloadSchema`. |
| 638 | * |
| 639 | * @category constructors |
| 640 | * @since 4.0.0 |
| 641 | */ |
| 642 | export const makeReviverFilter: <P>( |
| 643 | id: string, |
| 644 | payloadSchema: Schema.Decoder<P>, |
| 645 | revive: FilterReviver<P>["revive"] |
| 646 | ) => FilterReviver<P> = (id, payloadSchema, revive) => ({ id, payloadSchema, revive }) |
| 647 | |
| 648 | /** |
| 649 | * Creates a filter group reviver while inferring its payload type from `payloadSchema`. |
| 650 | * |
| 651 | * @category constructors |
| 652 | * @since 4.0.0 |
| 653 | */ |
| 654 | export const makeReviverFilterGroup: <P>( |
| 655 | id: string, |
| 656 | payloadSchema: Schema.Decoder<P>, |
| 657 | revive: FilterGroupReviver<P>["revive"] |
| 658 | ) => FilterGroupReviver<P> = (id, payloadSchema, revive) => ({ id, payloadSchema, revive }) |
| 659 | |
| 660 | function makeFixedDeclarationReviver(id: string, schema: Schema.Top): DeclarationReviver<null> { |
| 661 | return makeReviverDeclaration( |
| 662 | id, |
| 663 | Schema.Null, |
| 664 | ({ annotations }) => annotations === undefined ? schema : schema.annotate(annotations) |
| 665 | ) |
| 666 | } |
| 667 | |
| 668 | const IsPatternPayload = Schema.Struct({ |
| 669 | source: Schema.String, |
| 670 | flags: Schema.String |
| 671 | }).check(Schema.makeFilter((payload: { readonly source: string; readonly flags: string }) => { |
| 672 | try { |
| 673 | const regExp = new globalThis.RegExp(payload.source, payload.flags) |
| 674 | return regExp.source === payload.source && regExp.flags === payload.flags |
| 675 | } catch { |
| 676 | return false |
| 677 | } |
| 678 | })) |
| 679 | |
| 680 | type ErrorRepresentationOptions = { |
| 681 | readonly includeStack?: true | undefined |
| 682 | readonly excludeCause?: true | undefined |
| 683 | } |
| 684 | type ErrorRepresentationPayload = ErrorRepresentationOptions | null |
| 685 | const ErrorOptionsPayload = Schema.declare((input): input is ErrorRepresentationOptions => { |
| 686 | if (typeof input !== "object" || input === null) return false |
| 687 | const object = input as Record<string, unknown> |
| 688 | const keys = globalThis.Object.keys(input) |
| 689 | return keys.length > 0 && |
| 690 | keys.every((key) => (key === "includeStack" || key === "excludeCause") && object[key] === true) |
| 691 | }) |
| 692 | const ErrorRepresentationPayload: Schema.Decoder<ErrorRepresentationPayload> = Schema.Union([ |
| 693 | Schema.Null, |
| 694 | ErrorOptionsPayload |
| 695 | ]) |
| 696 | |
| 697 | type RedactedRepresentationOptions = { |
| 698 | readonly label?: string | undefined |
| 699 | readonly disallowJsonEncode?: true | undefined |
| 700 | } |
| 701 | type RedactedRepresentationPayload = RedactedRepresentationOptions | null |
| 702 | const RedactedOptionsPayload = Schema.declare((input): input is RedactedRepresentationOptions => { |
| 703 | if (typeof input !== "object" || input === null) return false |
| 704 | const object = input as Record<string, unknown> |
| 705 | const keys = globalThis.Object.keys(input) |
| 706 | return keys.length > 0 && keys.every((key) => { |
| 707 | switch (key) { |
| 708 | case "label": |
| 709 | return typeof object[key] === "string" |
| 710 | case "disallowJsonEncode": |
| 711 | return object[key] === true |
| 712 | default: |
| 713 | return false |
| 714 | } |
| 715 | }) |
| 716 | }) |
| 717 | const RedactedRepresentationPayload: Schema.Decoder<RedactedRepresentationPayload> = Schema.Union([ |
| 718 | Schema.Null, |
| 719 | RedactedOptionsPayload |
| 720 | ]) |
| 721 | |
| 722 | /** |
| 723 | * Reviver for persisted `isTrimmed` checks. |
| 724 | * |
| 725 | * **When to use** |
| 726 | * |
| 727 | * Use when reconstructing documents that may contain checks created by {@link Schema.isTrimmed}. |
| 728 | * |
| 729 | * @see {@link Schema.isTrimmed} for creating the corresponding check |
| 730 | * |
| 731 | * @category validation |
| 732 | * @since 4.0.0 |
| 733 | */ |
| 734 | export const isTrimmedReviver: FilterReviver<null> = makeReviverFilter( |
| 735 | "effect/schema/isTrimmed", |
| 736 | Schema.Null, |
| 737 | ({ annotations }) => Schema.isTrimmed(annotations) |
| 738 | ) |
| 739 | |
| 740 | /** |
| 741 | * Reviver for persisted `isPattern` checks. |
| 742 | * |
| 743 | * **When to use** |
| 744 | * |
| 745 | * Use when reconstructing documents that may contain checks created by {@link Schema.isPattern}. |
| 746 | * |
| 747 | * @see {@link Schema.isPattern} for creating the corresponding check |
| 748 | * |
| 749 | * @category validation |
| 750 | * @since 4.0.0 |
| 751 | */ |
| 752 | export const isPatternReviver: FilterReviver<{ |
| 753 | readonly source: string |
| 754 | readonly flags: string |
| 755 | }> = makeReviverFilter( |
| 756 | "effect/schema/isPattern", |
| 757 | IsPatternPayload, |
| 758 | ({ annotations, payload }) => Schema.isPattern(new globalThis.RegExp(payload.source, payload.flags), annotations) |
| 759 | ) |
| 760 | |
| 761 | /** |
| 762 | * Reviver for persisted `isStringFinite` checks. |
| 763 | * |
| 764 | * **When to use** |
| 765 | * |
| 766 | * Use when reconstructing documents that may contain checks created by {@link Schema.isStringFinite}. |
| 767 | * |
| 768 | * @see {@link Schema.isStringFinite} for creating the corresponding check |
| 769 | * |
| 770 | * @category validation |
| 771 | * @since 4.0.0 |
| 772 | */ |
| 773 | export const isStringFiniteReviver: FilterReviver<null> = makeReviverFilter( |
| 774 | "effect/schema/isStringFinite", |
| 775 | Schema.Null, |
| 776 | ({ annotations }) => Schema.isStringFinite(annotations) |
| 777 | ) |
| 778 | |
| 779 | /** |
| 780 | * Reviver for persisted `isStringBigInt` checks. |
| 781 | * |
| 782 | * **When to use** |
| 783 | * |
| 784 | * Use when reconstructing documents that may contain checks created by {@link Schema.isStringBigInt}. |
| 785 | * |
| 786 | * @see {@link Schema.isStringBigInt} for creating the corresponding check |
| 787 | * |
| 788 | * @category validation |
| 789 | * @since 4.0.0 |
| 790 | */ |
| 791 | export const isStringBigIntReviver: FilterReviver<null> = makeReviverFilter( |
| 792 | "effect/schema/isStringBigInt", |
| 793 | Schema.Null, |
| 794 | ({ annotations }) => Schema.isStringBigInt(annotations) |
| 795 | ) |
| 796 | |
| 797 | /** |
| 798 | * Reviver for persisted `isStringSymbol` checks. |
| 799 | * |
| 800 | * **When to use** |
| 801 | * |
| 802 | * Use when reconstructing documents that may contain checks created by {@link Schema.isStringSymbol}. |
| 803 | * |
| 804 | * @see {@link Schema.isStringSymbol} for creating the corresponding check |
| 805 | * |
| 806 | * @category validation |
| 807 | * @since 4.0.0 |
| 808 | */ |
| 809 | export const isStringSymbolReviver: FilterReviver<null> = makeReviverFilter( |
| 810 | "effect/schema/isStringSymbol", |
| 811 | Schema.Null, |
| 812 | ({ annotations }) => Schema.isStringSymbol(annotations) |
| 813 | ) |
| 814 | |
| 815 | /** |
| 816 | * Reviver for persisted `isUUID` checks. |
| 817 | * |
| 818 | * **When to use** |
| 819 | * |
| 820 | * Use when reconstructing documents that may contain checks created by {@link Schema.isUUID}. |
| 821 | * |
| 822 | * @see {@link Schema.isUUID} for creating the corresponding check |
| 823 | * |
| 824 | * @category validation |
| 825 | * @since 4.0.0 |
| 826 | */ |
| 827 | export const isUUIDReviver: FilterReviver<{ |
| 828 | readonly version: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | null |
| 829 | }> = makeReviverFilter( |
| 830 | "effect/schema/isUUID", |
| 831 | Schema.Struct({ version: Schema.Union([Schema.Literals([1, 2, 3, 4, 5, 6, 7, 8]), Schema.Null]) }), |
| 832 | ({ annotations, payload }) => Schema.isUUID(payload.version ?? undefined, annotations) |
| 833 | ) |
| 834 | |
| 835 | /** |
| 836 | * Reviver for persisted `isGUID` checks. |
| 837 | * |
| 838 | * **When to use** |
| 839 | * |
| 840 | * Use when reconstructing documents that may contain checks created by {@link Schema.isGUID}. |
| 841 | * |
| 842 | * @see {@link Schema.isGUID} for creating the corresponding check |
| 843 | * |
| 844 | * @category validation |
| 845 | * @since 4.0.0 |
| 846 | */ |
| 847 | export const isGUIDReviver: FilterReviver<null> = makeReviverFilter( |
| 848 | "effect/schema/isGUID", |
| 849 | Schema.Null, |
| 850 | ({ annotations }) => Schema.isGUID(annotations) |
| 851 | ) |
| 852 | |
| 853 | /** |
| 854 | * Reviver for persisted `isULID` checks. |
| 855 | * |
| 856 | * **When to use** |
| 857 | * |
| 858 | * Use when reconstructing documents that may contain checks created by {@link Schema.isULID}. |
| 859 | * |
| 860 | * @see {@link Schema.isULID} for creating the corresponding check |
| 861 | * |
| 862 | * @category validation |
| 863 | * @since 4.0.0 |
| 864 | */ |
| 865 | export const isULIDReviver: FilterReviver<null> = makeReviverFilter( |
| 866 | "effect/schema/isULID", |
| 867 | Schema.Null, |
| 868 | ({ annotations }) => Schema.isULID(annotations) |
| 869 | ) |
| 870 | |
| 871 | /** |
| 872 | * Reviver for persisted `isBase64` checks. |
| 873 | * |
| 874 | * **When to use** |
| 875 | * |
| 876 | * Use when reconstructing documents that may contain checks created by {@link Schema.isBase64}. |
| 877 | * |
| 878 | * @see {@link Schema.isBase64} for creating the corresponding check |
| 879 | * |
| 880 | * @category validation |
| 881 | * @since 4.0.0 |
| 882 | */ |
| 883 | export const isBase64Reviver: FilterReviver<null> = makeReviverFilter( |
| 884 | "effect/schema/isBase64", |
| 885 | Schema.Null, |
| 886 | ({ annotations }) => Schema.isBase64(annotations) |
| 887 | ) |
| 888 | |
| 889 | /** |
| 890 | * Reviver for persisted `isBase64Url` checks. |
| 891 | * |
| 892 | * **When to use** |
| 893 | * |
| 894 | * Use when reconstructing documents that may contain checks created by {@link Schema.isBase64Url}. |
| 895 | * |
| 896 | * @see {@link Schema.isBase64Url} for creating the corresponding check |
| 897 | * |
| 898 | * @category validation |
| 899 | * @since 4.0.0 |
| 900 | */ |
| 901 | export const isBase64UrlReviver: FilterReviver<null> = makeReviverFilter( |
| 902 | "effect/schema/isBase64Url", |
| 903 | Schema.Null, |
| 904 | ({ annotations }) => Schema.isBase64Url(annotations) |
| 905 | ) |
| 906 | |
| 907 | /** |
| 908 | * Reviver for persisted `isStartingWith` checks. |
| 909 | * |
| 910 | * **When to use** |
| 911 | * |
| 912 | * Use when reconstructing documents that may contain checks created by {@link Schema.isStartingWith}. |
| 913 | * |
| 914 | * @see {@link Schema.isStartingWith} for creating the corresponding check |
| 915 | * |
| 916 | * @category validation |
| 917 | * @since 4.0.0 |
| 918 | */ |
| 919 | export const isStartingWithReviver: FilterReviver<{ |
| 920 | readonly startsWith: string |
| 921 | }> = makeReviverFilter( |
| 922 | "effect/schema/isStartingWith", |
| 923 | Schema.Struct({ startsWith: Schema.String }), |
| 924 | ({ annotations, payload }) => Schema.isStartingWith(payload.startsWith, annotations) |
| 925 | ) |
| 926 | |
| 927 | /** |
| 928 | * Reviver for persisted `isEndingWith` checks. |
| 929 | * |
| 930 | * **When to use** |
| 931 | * |
| 932 | * Use when reconstructing documents that may contain checks created by {@link Schema.isEndingWith}. |
| 933 | * |
| 934 | * @see {@link Schema.isEndingWith} for creating the corresponding check |
| 935 | * |
| 936 | * @category validation |
| 937 | * @since 4.0.0 |
| 938 | */ |
| 939 | export const isEndingWithReviver: FilterReviver<{ |
| 940 | readonly endsWith: string |
| 941 | }> = makeReviverFilter( |
| 942 | "effect/schema/isEndingWith", |
| 943 | Schema.Struct({ endsWith: Schema.String }), |
| 944 | ({ annotations, payload }) => Schema.isEndingWith(payload.endsWith, annotations) |
| 945 | ) |
| 946 | |
| 947 | /** |
| 948 | * Reviver for persisted `isIncluding` checks. |
| 949 | * |
| 950 | * **When to use** |
| 951 | * |
| 952 | * Use when reconstructing documents that may contain checks created by {@link Schema.isIncluding}. |
| 953 | * |
| 954 | * @see {@link Schema.isIncluding} for creating the corresponding check |
| 955 | * |
| 956 | * @category validation |
| 957 | * @since 4.0.0 |
| 958 | */ |
| 959 | export const isIncludingReviver: FilterReviver<{ |
| 960 | readonly includes: string |
| 961 | }> = makeReviverFilter( |
| 962 | "effect/schema/isIncluding", |
| 963 | Schema.Struct({ includes: Schema.String }), |
| 964 | ({ annotations, payload }) => Schema.isIncluding(payload.includes, annotations) |
| 965 | ) |
| 966 | |
| 967 | /** |
| 968 | * Reviver for persisted `isUppercased` checks. |
| 969 | * |
| 970 | * **When to use** |
| 971 | * |
| 972 | * Use when reconstructing documents that may contain checks created by {@link Schema.isUppercased}. |
| 973 | * |
| 974 | * @see {@link Schema.isUppercased} for creating the corresponding check |
| 975 | * |
| 976 | * @category validation |
| 977 | * @since 4.0.0 |
| 978 | */ |
| 979 | export const isUppercasedReviver: FilterReviver<null> = makeReviverFilter( |
| 980 | "effect/schema/isUppercased", |
| 981 | Schema.Null, |
| 982 | ({ annotations }) => Schema.isUppercased(annotations) |
| 983 | ) |
| 984 | |
| 985 | /** |
| 986 | * Reviver for persisted `isLowercased` checks. |
| 987 | * |
| 988 | * **When to use** |
| 989 | * |
| 990 | * Use when reconstructing documents that may contain checks created by {@link Schema.isLowercased}. |
| 991 | * |
| 992 | * @see {@link Schema.isLowercased} for creating the corresponding check |
| 993 | * |
| 994 | * @category validation |
| 995 | * @since 4.0.0 |
| 996 | */ |
| 997 | export const isLowercasedReviver: FilterReviver<null> = makeReviverFilter( |
| 998 | "effect/schema/isLowercased", |
| 999 | Schema.Null, |
| 1000 | ({ annotations }) => Schema.isLowercased(annotations) |
| 1001 | ) |
| 1002 | |
| 1003 | /** |
| 1004 | * Reviver for persisted `isCapitalized` checks. |
| 1005 | * |
| 1006 | * **When to use** |
| 1007 | * |
| 1008 | * Use when reconstructing documents that may contain checks created by {@link Schema.isCapitalized}. |
| 1009 | * |
| 1010 | * @see {@link Schema.isCapitalized} for creating the corresponding check |
| 1011 | * |
| 1012 | * @category validation |
| 1013 | * @since 4.0.0 |
| 1014 | */ |
| 1015 | export const isCapitalizedReviver: FilterReviver<null> = makeReviverFilter( |
| 1016 | "effect/schema/isCapitalized", |
| 1017 | Schema.Null, |
| 1018 | ({ annotations }) => Schema.isCapitalized(annotations) |
| 1019 | ) |
| 1020 | |
| 1021 | /** |
| 1022 | * Reviver for persisted `isUncapitalized` checks. |
| 1023 | * |
| 1024 | * **When to use** |
| 1025 | * |
| 1026 | * Use when reconstructing documents that may contain checks created by {@link Schema.isUncapitalized}. |
| 1027 | * |
| 1028 | * @see {@link Schema.isUncapitalized} for creating the corresponding check |
| 1029 | * |
| 1030 | * @category validation |
| 1031 | * @since 4.0.0 |
| 1032 | */ |
| 1033 | export const isUncapitalizedReviver: FilterReviver<null> = makeReviverFilter( |
| 1034 | "effect/schema/isUncapitalized", |
| 1035 | Schema.Null, |
| 1036 | ({ annotations }) => Schema.isUncapitalized(annotations) |
| 1037 | ) |
| 1038 | |
| 1039 | /** |
| 1040 | * Reviver for persisted `isFinite` checks. |
| 1041 | * |
| 1042 | * **When to use** |
| 1043 | * |
| 1044 | * Use when reconstructing documents that may contain checks created by {@link Schema.isFinite}. |
| 1045 | * |
| 1046 | * @see {@link Schema.isFinite} for creating the corresponding check |
| 1047 | * |
| 1048 | * @category validation |
| 1049 | * @since 4.0.0 |
| 1050 | */ |
| 1051 | export const isFiniteReviver: FilterReviver<null> = makeReviverFilter( |
| 1052 | "effect/schema/isFinite", |
| 1053 | Schema.Null, |
| 1054 | ({ annotations }) => Schema.isFinite(annotations) |
| 1055 | ) |
| 1056 | |
| 1057 | /** |
| 1058 | * Reviver for persisted `isGreaterThan` checks. |
| 1059 | * |
| 1060 | * **When to use** |
| 1061 | * |
| 1062 | * Use when reconstructing documents that may contain checks created by {@link Schema.isGreaterThan}. |
| 1063 | * |
| 1064 | * @see {@link Schema.isGreaterThan} for creating the corresponding check |
| 1065 | * |
| 1066 | * @category validation |
| 1067 | * @since 4.0.0 |
| 1068 | */ |
| 1069 | export const isGreaterThanReviver: FilterReviver<{ |
| 1070 | readonly exclusiveMinimum: number |
| 1071 | }> = makeReviverFilter( |
| 1072 | "effect/schema/isGreaterThan", |
| 1073 | Schema.Struct({ exclusiveMinimum: Schema.Finite }), |
| 1074 | ({ annotations, payload }) => Schema.isGreaterThan(payload.exclusiveMinimum, annotations) |
| 1075 | ) |
| 1076 | |
| 1077 | /** |
| 1078 | * Reviver for persisted `isGreaterThanOrEqualTo` checks. |
| 1079 | * |
| 1080 | * **When to use** |
| 1081 | * |
| 1082 | * Use when reconstructing documents that may contain checks created by {@link Schema.isGreaterThanOrEqualTo}. |
| 1083 | * |
| 1084 | * @see {@link Schema.isGreaterThanOrEqualTo} for creating the corresponding check |
| 1085 | * |
| 1086 | * @category validation |
| 1087 | * @since 4.0.0 |
| 1088 | */ |
| 1089 | export const isGreaterThanOrEqualToReviver: FilterReviver<{ |
| 1090 | readonly minimum: number |
| 1091 | }> = makeReviverFilter( |
| 1092 | "effect/schema/isGreaterThanOrEqualTo", |
| 1093 | Schema.Struct({ minimum: Schema.Finite }), |
| 1094 | ({ annotations, payload }) => Schema.isGreaterThanOrEqualTo(payload.minimum, annotations) |
| 1095 | ) |
| 1096 | |
| 1097 | /** |
| 1098 | * Reviver for persisted `isLessThan` checks. |
| 1099 | * |
| 1100 | * **When to use** |
| 1101 | * |
| 1102 | * Use when reconstructing documents that may contain checks created by {@link Schema.isLessThan}. |
| 1103 | * |
| 1104 | * @see {@link Schema.isLessThan} for creating the corresponding check |
| 1105 | * |
| 1106 | * @category validation |
| 1107 | * @since 4.0.0 |
| 1108 | */ |
| 1109 | export const isLessThanReviver: FilterReviver<{ |
| 1110 | readonly exclusiveMaximum: number |
| 1111 | }> = makeReviverFilter( |
| 1112 | "effect/schema/isLessThan", |
| 1113 | Schema.Struct({ exclusiveMaximum: Schema.Finite }), |
| 1114 | ({ annotations, payload }) => Schema.isLessThan(payload.exclusiveMaximum, annotations) |
| 1115 | ) |
| 1116 | |
| 1117 | /** |
| 1118 | * Reviver for persisted `isLessThanOrEqualTo` checks. |
| 1119 | * |
| 1120 | * **When to use** |
| 1121 | * |
| 1122 | * Use when reconstructing documents that may contain checks created by {@link Schema.isLessThanOrEqualTo}. |
| 1123 | * |
| 1124 | * @see {@link Schema.isLessThanOrEqualTo} for creating the corresponding check |
| 1125 | * |
| 1126 | * @category validation |
| 1127 | * @since 4.0.0 |
| 1128 | */ |
| 1129 | export const isLessThanOrEqualToReviver: FilterReviver<{ |
| 1130 | readonly maximum: number |
| 1131 | }> = makeReviverFilter( |
| 1132 | "effect/schema/isLessThanOrEqualTo", |
| 1133 | Schema.Struct({ maximum: Schema.Finite }), |
| 1134 | ({ annotations, payload }) => Schema.isLessThanOrEqualTo(payload.maximum, annotations) |
| 1135 | ) |
| 1136 | |
| 1137 | /** |
| 1138 | * Reviver for persisted `isBetween` checks. |
| 1139 | * |
| 1140 | * **When to use** |
| 1141 | * |
| 1142 | * Use when reconstructing documents that may contain checks created by {@link Schema.isBetween}. |
| 1143 | * |
| 1144 | * @see {@link Schema.isBetween} for creating the corresponding check |
| 1145 | * |
| 1146 | * @category validation |
| 1147 | * @since 4.0.0 |
| 1148 | */ |
| 1149 | export const isBetweenReviver: FilterReviver<{ |
| 1150 | readonly minimum: number |
| 1151 | readonly maximum: number |
| 1152 | readonly exclusiveMinimum?: true | undefined |
| 1153 | readonly exclusiveMaximum?: true | undefined |
| 1154 | }> = makeReviverFilter( |
| 1155 | "effect/schema/isBetween", |
| 1156 | Schema.Struct({ |
| 1157 | minimum: Schema.Finite, |
| 1158 | maximum: Schema.Finite, |
| 1159 | exclusiveMinimum: Schema.optional(Schema.Literal(true)), |
| 1160 | exclusiveMaximum: Schema.optional(Schema.Literal(true)) |
| 1161 | }), |
| 1162 | ({ annotations, payload }) => Schema.isBetween(payload, annotations) |
| 1163 | ) |
| 1164 | |
| 1165 | /** |
| 1166 | * Reviver for persisted `isMultipleOf` checks. |
| 1167 | * |
| 1168 | * **When to use** |
| 1169 | * |
| 1170 | * Use when reconstructing documents that may contain checks created by {@link Schema.isMultipleOf}. |
| 1171 | * |
| 1172 | * @see {@link Schema.isMultipleOf} for creating the corresponding check |
| 1173 | * |
| 1174 | * @category validation |
| 1175 | * @since 4.0.0 |
| 1176 | */ |
| 1177 | export const isMultipleOfReviver: FilterReviver<{ |
| 1178 | readonly divisor: number |
| 1179 | }> = makeReviverFilter( |
| 1180 | "effect/schema/isMultipleOf", |
| 1181 | Schema.Struct({ divisor: Schema.Finite }), |
| 1182 | ({ annotations, payload }) => Schema.isMultipleOf(payload.divisor, annotations) |
| 1183 | ) |
| 1184 | |
| 1185 | /** |
| 1186 | * Reviver for persisted `isInt` checks. |
| 1187 | * |
| 1188 | * **When to use** |
| 1189 | * |
| 1190 | * Use when reconstructing documents that may contain checks created by {@link Schema.isInt}. |
| 1191 | * |
| 1192 | * @see {@link Schema.isInt} for creating the corresponding check |
| 1193 | * |
| 1194 | * @category validation |
| 1195 | * @since 4.0.0 |
| 1196 | */ |
| 1197 | export const isIntReviver: FilterReviver<null> = makeReviverFilter( |
| 1198 | "effect/schema/isInt", |
| 1199 | Schema.Null, |
| 1200 | ({ annotations }) => Schema.isInt(annotations) |
| 1201 | ) |
| 1202 | |
| 1203 | /** |
| 1204 | * Reviver for persisted `isMinLength` checks. |
| 1205 | * |
| 1206 | * **When to use** |
| 1207 | * |
| 1208 | * Use when reconstructing documents that may contain checks created by {@link Schema.isMinLength}. |
| 1209 | * |
| 1210 | * @see {@link Schema.isMinLength} for creating the corresponding check |
| 1211 | * |
| 1212 | * @category validation |
| 1213 | * @since 4.0.0 |
| 1214 | */ |
| 1215 | export const isMinLengthReviver: FilterReviver<{ |
| 1216 | readonly minLength: number |
| 1217 | }> = makeReviverFilter( |
| 1218 | "effect/schema/isMinLength", |
| 1219 | Schema.Struct({ minLength: Schema.Natural }), |
| 1220 | ({ annotations, payload }) => Schema.isMinLength(payload.minLength, annotations) |
| 1221 | ) |
| 1222 | |
| 1223 | /** |
| 1224 | * Reviver for persisted `isMaxLength` checks. |
| 1225 | * |
| 1226 | * **When to use** |
| 1227 | * |
| 1228 | * Use when reconstructing documents that may contain checks created by {@link Schema.isMaxLength}. |
| 1229 | * |
| 1230 | * @see {@link Schema.isMaxLength} for creating the corresponding check |
| 1231 | * |
| 1232 | * @category validation |
| 1233 | * @since 4.0.0 |
| 1234 | */ |
| 1235 | export const isMaxLengthReviver: FilterReviver<{ |
| 1236 | readonly maxLength: number |
| 1237 | }> = makeReviverFilter( |
| 1238 | "effect/schema/isMaxLength", |
| 1239 | Schema.Struct({ maxLength: Schema.Natural }), |
| 1240 | ({ annotations, payload }) => Schema.isMaxLength(payload.maxLength, annotations) |
| 1241 | ) |
| 1242 | |
| 1243 | /** |
| 1244 | * Reviver for persisted `isBetweenLength` checks. |
| 1245 | * |
| 1246 | * **When to use** |
| 1247 | * |
| 1248 | * Use when reconstructing documents that may contain checks created by {@link Schema.isBetweenLength}. |
| 1249 | * |
| 1250 | * @see {@link Schema.isBetweenLength} for creating the corresponding check |
| 1251 | * |
| 1252 | * @category validation |
| 1253 | * @since 4.0.0 |
| 1254 | */ |
| 1255 | export const isBetweenLengthReviver: FilterReviver<{ |
| 1256 | readonly minimum: number |
| 1257 | readonly maximum: number |
| 1258 | }> = makeReviverFilter( |
| 1259 | "effect/schema/isBetweenLength", |
| 1260 | Schema.Struct({ minimum: Schema.Natural, maximum: Schema.Natural }), |
| 1261 | ({ annotations, payload }) => Schema.isBetweenLength(payload.minimum, payload.maximum, annotations) |
| 1262 | ) |
| 1263 | |
| 1264 | /** |
| 1265 | * Reviver for persisted `isMinCodePoints` checks. |
| 1266 | * |
| 1267 | * @see {@link Schema.isMinCodePoints} for creating the corresponding check |
| 1268 | * @category validation |
| 1269 | * @since 4.0.0 |
| 1270 | */ |
| 1271 | export const isMinCodePointsReviver: FilterReviver<{ |
| 1272 | readonly minCodePoints: number |
| 1273 | }> = makeReviverFilter( |
| 1274 | "effect/schema/isMinCodePoints", |
| 1275 | Schema.Struct({ minCodePoints: Schema.Natural }), |
| 1276 | ({ annotations, payload }) => Schema.isMinCodePoints(payload.minCodePoints, annotations) |
| 1277 | ) |
| 1278 | |
| 1279 | /** |
| 1280 | * Reviver for persisted `isMaxCodePoints` checks. |
| 1281 | * |
| 1282 | * @see {@link Schema.isMaxCodePoints} for creating the corresponding check |
| 1283 | * @category validation |
| 1284 | * @since 4.0.0 |
| 1285 | */ |
| 1286 | export const isMaxCodePointsReviver: FilterReviver<{ |
| 1287 | readonly maxCodePoints: number |
| 1288 | }> = makeReviverFilter( |
| 1289 | "effect/schema/isMaxCodePoints", |
| 1290 | Schema.Struct({ maxCodePoints: Schema.Natural }), |
| 1291 | ({ annotations, payload }) => Schema.isMaxCodePoints(payload.maxCodePoints, annotations) |
| 1292 | ) |
| 1293 | |
| 1294 | /** |
| 1295 | * Reviver for persisted `isBetweenCodePoints` checks. |
| 1296 | * |
| 1297 | * @see {@link Schema.isBetweenCodePoints} for creating the corresponding check |
| 1298 | * @category validation |
| 1299 | * @since 4.0.0 |
| 1300 | */ |
| 1301 | export const isBetweenCodePointsReviver: FilterReviver<{ |
| 1302 | readonly minimum: number |
| 1303 | readonly maximum: number |
| 1304 | }> = makeReviverFilter( |
| 1305 | "effect/schema/isBetweenCodePoints", |
| 1306 | Schema.Struct({ minimum: Schema.Natural, maximum: Schema.Natural }), |
| 1307 | ({ annotations, payload }) => Schema.isBetweenCodePoints(payload.minimum, payload.maximum, annotations) |
| 1308 | ) |
| 1309 | |
| 1310 | /** |
| 1311 | * Reviver for persisted `isMinSize` checks. |
| 1312 | * |
| 1313 | * **When to use** |
| 1314 | * |
| 1315 | * Use when reconstructing documents that may contain checks created by {@link Schema.isMinSize}. |
| 1316 | * |
| 1317 | * @see {@link Schema.isMinSize} for creating the corresponding check |
| 1318 | * |
| 1319 | * @category validation |
| 1320 | * @since 4.0.0 |
| 1321 | */ |
| 1322 | export const isMinSizeReviver: FilterReviver<{ |
| 1323 | readonly minSize: number |
| 1324 | }> = makeReviverFilter( |
| 1325 | "effect/schema/isMinSize", |
| 1326 | Schema.Struct({ minSize: Schema.Natural }), |
| 1327 | ({ annotations, payload }) => Schema.isMinSize(payload.minSize, annotations) |
| 1328 | ) |
| 1329 | |
| 1330 | /** |
| 1331 | * Reviver for persisted `isMaxSize` checks. |
| 1332 | * |
| 1333 | * **When to use** |
| 1334 | * |
| 1335 | * Use when reconstructing documents that may contain checks created by {@link Schema.isMaxSize}. |
| 1336 | * |
| 1337 | * @see {@link Schema.isMaxSize} for creating the corresponding check |
| 1338 | * |
| 1339 | * @category validation |
| 1340 | * @since 4.0.0 |
| 1341 | */ |
| 1342 | export const isMaxSizeReviver: FilterReviver<{ |
| 1343 | readonly maxSize: number |
| 1344 | }> = makeReviverFilter( |
| 1345 | "effect/schema/isMaxSize", |
| 1346 | Schema.Struct({ maxSize: Schema.Natural }), |
| 1347 | ({ annotations, payload }) => Schema.isMaxSize(payload.maxSize, annotations) |
| 1348 | ) |
| 1349 | |
| 1350 | /** |
| 1351 | * Reviver for persisted `isBetweenSize` checks. |
| 1352 | * |
| 1353 | * **When to use** |
| 1354 | * |
| 1355 | * Use when reconstructing documents that may contain checks created by {@link Schema.isBetweenSize}. |
| 1356 | * |
| 1357 | * @see {@link Schema.isBetweenSize} for creating the corresponding check |
| 1358 | * |
| 1359 | * @category validation |
| 1360 | * @since 4.0.0 |
| 1361 | */ |
| 1362 | export const isBetweenSizeReviver: FilterReviver<{ |
| 1363 | readonly minimum: number |
| 1364 | readonly maximum: number |
| 1365 | }> = makeReviverFilter( |
| 1366 | "effect/schema/isBetweenSize", |
| 1367 | Schema.Struct({ minimum: Schema.Natural, maximum: Schema.Natural }), |
| 1368 | ({ annotations, payload }) => Schema.isBetweenSize(payload.minimum, payload.maximum, annotations) |
| 1369 | ) |
| 1370 | |
| 1371 | /** |
| 1372 | * Reviver for persisted `isMinProperties` checks. |
| 1373 | * |
| 1374 | * **When to use** |
| 1375 | * |
| 1376 | * Use when reconstructing documents that may contain checks created by {@link Schema.isMinProperties}. |
| 1377 | * |
| 1378 | * @see {@link Schema.isMinProperties} for creating the corresponding check |
| 1379 | * |
| 1380 | * @category validation |
| 1381 | * @since 4.0.0 |
| 1382 | */ |
| 1383 | export const isMinPropertiesReviver: FilterReviver<{ |
| 1384 | readonly minProperties: number |
| 1385 | }> = makeReviverFilter( |
| 1386 | "effect/schema/isMinProperties", |
| 1387 | Schema.Struct({ minProperties: Schema.Natural }), |
| 1388 | ({ annotations, payload }) => Schema.isMinProperties(payload.minProperties, annotations) |
| 1389 | ) |
| 1390 | |
| 1391 | /** |
| 1392 | * Reviver for persisted `isMaxProperties` checks. |
| 1393 | * |
| 1394 | * **When to use** |
| 1395 | * |
| 1396 | * Use when reconstructing documents that may contain checks created by {@link Schema.isMaxProperties}. |
| 1397 | * |
| 1398 | * @see {@link Schema.isMaxProperties} for creating the corresponding check |
| 1399 | * |
| 1400 | * @category validation |
| 1401 | * @since 4.0.0 |
| 1402 | */ |
| 1403 | export const isMaxPropertiesReviver: FilterReviver<{ |
| 1404 | readonly maxProperties: number |
| 1405 | }> = makeReviverFilter( |
| 1406 | "effect/schema/isMaxProperties", |
| 1407 | Schema.Struct({ maxProperties: Schema.Natural }), |
| 1408 | ({ annotations, payload }) => Schema.isMaxProperties(payload.maxProperties, annotations) |
| 1409 | ) |
| 1410 | |
| 1411 | /** |
| 1412 | * Reviver for persisted `isBetweenProperties` checks. |
| 1413 | * |
| 1414 | * **When to use** |
| 1415 | * |
| 1416 | * Use when reconstructing documents that may contain checks created by {@link Schema.isBetweenProperties}. |
| 1417 | * |
| 1418 | * @see {@link Schema.isBetweenProperties} for creating the corresponding check |
| 1419 | * |
| 1420 | * @category validation |
| 1421 | * @since 4.0.0 |
| 1422 | */ |
| 1423 | export const isBetweenPropertiesReviver: FilterReviver<{ |
| 1424 | readonly minimum: number |
| 1425 | readonly maximum: number |
| 1426 | }> = makeReviverFilter( |
| 1427 | "effect/schema/isBetweenProperties", |
| 1428 | Schema.Struct({ minimum: Schema.Natural, maximum: Schema.Natural }), |
| 1429 | ({ annotations, payload }) => Schema.isBetweenProperties(payload.minimum, payload.maximum, annotations) |
| 1430 | ) |
| 1431 | |
| 1432 | /** |
| 1433 | * Reviver for persisted `isPropertyNames` checks. |
| 1434 | * |
| 1435 | * **When to use** |
| 1436 | * |
| 1437 | * Use when reconstructing documents that may contain checks created by {@link Schema.isPropertyNames}. |
| 1438 | * |
| 1439 | * @see {@link Schema.isPropertyNames} for creating the corresponding check |
| 1440 | * |
| 1441 | * @category validation |
| 1442 | * @since 4.0.0 |
| 1443 | */ |
| 1444 | export const isPropertyNamesReviver: FilterReviver<null> = makeReviverFilter( |
| 1445 | "effect/schema/isPropertyNames", |
| 1446 | Schema.Null, |
| 1447 | ({ annotations, schemas }) => Schema.isPropertyNames(schemas[0], annotations) |
| 1448 | ) |
| 1449 | |
| 1450 | /** |
| 1451 | * Reviver for persisted `isUnique` checks. |
| 1452 | * |
| 1453 | * **When to use** |
| 1454 | * |
| 1455 | * Use when reconstructing documents that may contain checks created by {@link Schema.isUnique}. |
| 1456 | * |
| 1457 | * @see {@link Schema.isUnique} for creating the corresponding check |
| 1458 | * |
| 1459 | * @category validation |
| 1460 | * @since 4.0.0 |
| 1461 | */ |
| 1462 | export const isUniqueReviver: FilterReviver<null> = makeReviverFilter( |
| 1463 | "effect/schema/isUnique", |
| 1464 | Schema.Null, |
| 1465 | ({ annotations }) => Schema.isUnique(annotations) |
| 1466 | ) |
| 1467 | |
| 1468 | /** |
| 1469 | * Reviver for persisted `isUniqueKey` checks. |
| 1470 | * |
| 1471 | * **When to use** |
| 1472 | * |
| 1473 | * Use when reconstructing documents that may contain checks created by {@link Schema.isUniqueKey}. |
| 1474 | * |
| 1475 | * @see {@link Schema.isUniqueKey} for creating the corresponding check |
| 1476 | * @category validation |
| 1477 | * @since 4.0.0 |
| 1478 | */ |
| 1479 | export const isUniqueKeyReviver: FilterReviver<null> = makeReviverFilter( |
| 1480 | "effect/schema/isUniqueKey", |
| 1481 | Schema.Null, |
| 1482 | ({ annotations }) => Schema.isUniqueKey(annotations) |
| 1483 | ) |
| 1484 | |
| 1485 | /** |
| 1486 | * Reviver for persisted `Option` declarations. |
| 1487 | * |
| 1488 | * **When to use** |
| 1489 | * |
| 1490 | * Use when reconstructing documents that may contain schemas created by {@link Schema.Option}. |
| 1491 | * |
| 1492 | * @see {@link Schema.Option} for creating the corresponding schema |
| 1493 | * |
| 1494 | * @category schemas |
| 1495 | * @since 4.0.0 |
| 1496 | */ |
| 1497 | export const OptionReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1498 | "effect/schema/Option", |
| 1499 | Schema.Null, |
| 1500 | ({ annotations, typeParameters }) => { |
| 1501 | const schema = Schema.Option(typeParameters[0]) |
| 1502 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1503 | } |
| 1504 | ) |
| 1505 | |
| 1506 | /** |
| 1507 | * Reviver for persisted {@link Schema.Result} declarations. |
| 1508 | * |
| 1509 | * **When to use** |
| 1510 | * |
| 1511 | * Use when reconstructing documents that may contain schemas created by {@link Schema.Result}. |
| 1512 | * |
| 1513 | * @see {@link Schema.Result} for creating the corresponding schema |
| 1514 | * |
| 1515 | * @category schemas |
| 1516 | * @since 4.0.0 |
| 1517 | */ |
| 1518 | export const ResultReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1519 | "effect/schema/Result", |
| 1520 | Schema.Null, |
| 1521 | ({ annotations, typeParameters }) => { |
| 1522 | const schema = Schema.Result(typeParameters[0], typeParameters[1]) |
| 1523 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1524 | } |
| 1525 | ) |
| 1526 | |
| 1527 | /** |
| 1528 | * Reviver for persisted {@link Schema.Redacted} declarations. |
| 1529 | * |
| 1530 | * **When to use** |
| 1531 | * |
| 1532 | * Use when reconstructing documents that may contain schemas created by {@link Schema.Redacted}. |
| 1533 | * |
| 1534 | * @see {@link Schema.Redacted} for creating the corresponding schema |
| 1535 | * |
| 1536 | * @category schemas |
| 1537 | * @since 4.0.0 |
| 1538 | */ |
| 1539 | export const RedactedReviver: DeclarationReviver<RedactedRepresentationPayload> = makeReviverDeclaration( |
| 1540 | "effect/schema/Redacted", |
| 1541 | RedactedRepresentationPayload, |
| 1542 | ({ annotations, payload, typeParameters }) => { |
| 1543 | const schema = Schema.Redacted(typeParameters[0], payload ?? undefined) |
| 1544 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1545 | } |
| 1546 | ) |
| 1547 | |
| 1548 | /** |
| 1549 | * Reviver for persisted `CauseReason` declarations. |
| 1550 | * |
| 1551 | * **When to use** |
| 1552 | * |
| 1553 | * Use when reconstructing documents that may contain schemas created by {@link Schema.CauseReason}. |
| 1554 | * |
| 1555 | * @see {@link Schema.CauseReason} for creating the corresponding schema |
| 1556 | * |
| 1557 | * @category schemas |
| 1558 | * @since 4.0.0 |
| 1559 | */ |
| 1560 | export const CauseReasonReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1561 | "effect/schema/CauseReason", |
| 1562 | Schema.Null, |
| 1563 | ({ annotations, typeParameters }) => { |
| 1564 | const schema = Schema.CauseReason(typeParameters[0], typeParameters[1]) |
| 1565 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1566 | } |
| 1567 | ) |
| 1568 | |
| 1569 | /** |
| 1570 | * Reviver for persisted `Cause` declarations. |
| 1571 | * |
| 1572 | * **When to use** |
| 1573 | * |
| 1574 | * Use when reconstructing documents that may contain schemas created by {@link Schema.Cause}. |
| 1575 | * |
| 1576 | * @see {@link Schema.Cause} for creating the corresponding schema |
| 1577 | * |
| 1578 | * @category schemas |
| 1579 | * @since 4.0.0 |
| 1580 | */ |
| 1581 | export const CauseReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1582 | "effect/schema/Cause", |
| 1583 | Schema.Null, |
| 1584 | ({ annotations, typeParameters }) => { |
| 1585 | const schema = Schema.Cause(typeParameters[0], typeParameters[1]) |
| 1586 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1587 | } |
| 1588 | ) |
| 1589 | |
| 1590 | /** |
| 1591 | * Reviver for persisted {@link Schema.ErrorInstance} declarations. |
| 1592 | * |
| 1593 | * **When to use** |
| 1594 | * |
| 1595 | * Use when reconstructing documents that may contain schemas created by {@link Schema.ErrorInstance}. |
| 1596 | * |
| 1597 | * @see {@link Schema.ErrorInstance} for creating the corresponding schema |
| 1598 | * |
| 1599 | * @category schemas |
| 1600 | * @since 4.0.0 |
| 1601 | */ |
| 1602 | export const ErrorInstanceReviver: DeclarationReviver<ErrorRepresentationPayload> = makeReviverDeclaration( |
| 1603 | "effect/schema/Error", |
| 1604 | ErrorRepresentationPayload, |
| 1605 | ({ annotations, payload }) => { |
| 1606 | const schema = Schema.ErrorInstance(payload ?? undefined) |
| 1607 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1608 | } |
| 1609 | ) |
| 1610 | |
| 1611 | /** |
| 1612 | * Reviver for persisted `Exit` declarations. |
| 1613 | * |
| 1614 | * **When to use** |
| 1615 | * |
| 1616 | * Use when reconstructing documents that may contain schemas created by {@link Schema.Exit}. |
| 1617 | * |
| 1618 | * @see {@link Schema.Exit} for creating the corresponding schema |
| 1619 | * |
| 1620 | * @category schemas |
| 1621 | * @since 4.0.0 |
| 1622 | */ |
| 1623 | export const ExitReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1624 | "effect/schema/Exit", |
| 1625 | Schema.Null, |
| 1626 | ({ annotations, typeParameters }) => { |
| 1627 | const schema = Schema.Exit(typeParameters[0], typeParameters[1], typeParameters[2]) |
| 1628 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1629 | } |
| 1630 | ) |
| 1631 | |
| 1632 | /** |
| 1633 | * Reviver for persisted {@link Schema.ReadonlyMap} declarations. |
| 1634 | * |
| 1635 | * **When to use** |
| 1636 | * |
| 1637 | * Use when reconstructing documents that may contain schemas created by {@link Schema.ReadonlyMap}. |
| 1638 | * |
| 1639 | * @see {@link Schema.ReadonlyMap} for creating the corresponding schema |
| 1640 | * |
| 1641 | * @category schemas |
| 1642 | * @since 4.0.0 |
| 1643 | */ |
| 1644 | export const ReadonlyMapReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1645 | "effect/schema/ReadonlyMap", |
| 1646 | Schema.Null, |
| 1647 | ({ annotations, typeParameters }) => { |
| 1648 | const schema = Schema.ReadonlyMap(typeParameters[0], typeParameters[1]) |
| 1649 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1650 | } |
| 1651 | ) |
| 1652 | |
| 1653 | /** |
| 1654 | * Reviver for persisted {@link Schema.Graph} declarations. |
| 1655 | * |
| 1656 | * @category schemas |
| 1657 | * @since 4.0.0 |
| 1658 | */ |
| 1659 | export const GraphReviver: DeclarationReviver<"directed" | "undirected"> = makeReviverDeclaration( |
| 1660 | "effect/schema/Graph", |
| 1661 | Schema.Literals(["directed", "undirected"]), |
| 1662 | ({ annotations, payload, typeParameters }) => { |
| 1663 | const schema = Schema.Graph(payload, typeParameters[0], typeParameters[1]) |
| 1664 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1665 | } |
| 1666 | ) |
| 1667 | |
| 1668 | /** |
| 1669 | * Reviver for persisted `HashMap` declarations. |
| 1670 | * |
| 1671 | * **When to use** |
| 1672 | * |
| 1673 | * Use when reconstructing documents that may contain schemas created by {@link Schema.HashMap}. |
| 1674 | * |
| 1675 | * @see {@link Schema.HashMap} for creating the corresponding schema |
| 1676 | * |
| 1677 | * @category schemas |
| 1678 | * @since 4.0.0 |
| 1679 | */ |
| 1680 | export const HashMapReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1681 | "effect/schema/HashMap", |
| 1682 | Schema.Null, |
| 1683 | ({ annotations, typeParameters }) => { |
| 1684 | const schema = Schema.HashMap(typeParameters[0], typeParameters[1]) |
| 1685 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1686 | } |
| 1687 | ) |
| 1688 | |
| 1689 | /** |
| 1690 | * Reviver for persisted {@link Schema.ReadonlySet} declarations. |
| 1691 | * |
| 1692 | * **When to use** |
| 1693 | * |
| 1694 | * Use when reconstructing documents that may contain schemas created by {@link Schema.ReadonlySet}. |
| 1695 | * |
| 1696 | * @see {@link Schema.ReadonlySet} for creating the corresponding schema |
| 1697 | * |
| 1698 | * @category schemas |
| 1699 | * @since 4.0.0 |
| 1700 | */ |
| 1701 | export const ReadonlySetReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1702 | "effect/schema/ReadonlySet", |
| 1703 | Schema.Null, |
| 1704 | ({ annotations, typeParameters }) => { |
| 1705 | const schema = Schema.ReadonlySet(typeParameters[0]) |
| 1706 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1707 | } |
| 1708 | ) |
| 1709 | |
| 1710 | /** |
| 1711 | * Reviver for persisted `HashSet` declarations. |
| 1712 | * |
| 1713 | * **When to use** |
| 1714 | * |
| 1715 | * Use when reconstructing documents that may contain schemas created by {@link Schema.HashSet}. |
| 1716 | * |
| 1717 | * @see {@link Schema.HashSet} for creating the corresponding schema |
| 1718 | * |
| 1719 | * @category schemas |
| 1720 | * @since 4.0.0 |
| 1721 | */ |
| 1722 | export const HashSetReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1723 | "effect/schema/HashSet", |
| 1724 | Schema.Null, |
| 1725 | ({ annotations, typeParameters }) => { |
| 1726 | const schema = Schema.HashSet(typeParameters[0]) |
| 1727 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1728 | } |
| 1729 | ) |
| 1730 | |
| 1731 | /** |
| 1732 | * Reviver for persisted {@link Schema.Chunk} declarations. |
| 1733 | * |
| 1734 | * **When to use** |
| 1735 | * |
| 1736 | * Use when reconstructing documents that may contain schemas created by {@link Schema.Chunk}. |
| 1737 | * |
| 1738 | * @see {@link Schema.Chunk} for creating the corresponding schema |
| 1739 | * |
| 1740 | * @category schemas |
| 1741 | * @since 4.0.0 |
| 1742 | */ |
| 1743 | export const ChunkReviver: DeclarationReviver<null> = makeReviverDeclaration( |
| 1744 | "effect/schema/Chunk", |
| 1745 | Schema.Null, |
| 1746 | ({ annotations, typeParameters }) => { |
| 1747 | const schema = Schema.Chunk(typeParameters[0]) |
| 1748 | return annotations === undefined ? schema : schema.annotate(annotations) |
| 1749 | } |
| 1750 | ) |
| 1751 | |
| 1752 | /** |
| 1753 | * Reviver for persisted `RegExp` declarations. |
| 1754 | * |
| 1755 | * **When to use** |
| 1756 | * |
| 1757 | * Use when reconstructing documents that may contain the {@link Schema.RegExp} schema. |
| 1758 | * |
| 1759 | * @see {@link Schema.RegExp} for the corresponding schema |
| 1760 | * |
| 1761 | * @category schemas |
| 1762 | * @since 4.0.0 |
| 1763 | */ |
| 1764 | export const RegExpReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1765 | "effect/schema/RegExp", |
| 1766 | Schema.RegExp |
| 1767 | ) |
| 1768 | |
| 1769 | /** |
| 1770 | * Reviver for persisted `URL` declarations. |
| 1771 | * |
| 1772 | * **When to use** |
| 1773 | * |
| 1774 | * Use when reconstructing documents that may contain the {@link Schema.URL} schema. |
| 1775 | * |
| 1776 | * @see {@link Schema.URL} for the corresponding schema |
| 1777 | * |
| 1778 | * @category schemas |
| 1779 | * @since 4.0.0 |
| 1780 | */ |
| 1781 | export const URLReviver: DeclarationReviver<null> = makeFixedDeclarationReviver("effect/schema/URL", Schema.URL) |
| 1782 | |
| 1783 | /** |
| 1784 | * Reviver for persisted `Date` declarations. |
| 1785 | * |
| 1786 | * **When to use** |
| 1787 | * |
| 1788 | * Use when reconstructing documents that may contain the {@link Schema.Date} schema. |
| 1789 | * |
| 1790 | * @see {@link Schema.Date} for the corresponding schema |
| 1791 | * |
| 1792 | * @category schemas |
| 1793 | * @since 4.0.0 |
| 1794 | */ |
| 1795 | export const DateReviver: DeclarationReviver<null> = makeFixedDeclarationReviver("effect/schema/Date", Schema.Date) |
| 1796 | |
| 1797 | /** |
| 1798 | * Reviver for persisted {@link Schema.Duration} declarations. |
| 1799 | * |
| 1800 | * **When to use** |
| 1801 | * |
| 1802 | * Use when reconstructing documents that may contain the {@link Schema.Duration} schema. |
| 1803 | * |
| 1804 | * @see {@link Schema.Duration} for the corresponding schema |
| 1805 | * |
| 1806 | * @category schemas |
| 1807 | * @since 4.0.0 |
| 1808 | */ |
| 1809 | export const DurationReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1810 | "effect/schema/Duration", |
| 1811 | Schema.Duration |
| 1812 | ) |
| 1813 | |
| 1814 | /** |
| 1815 | * Reviver for persisted {@link Schema.ByteSize} declarations. |
| 1816 | * |
| 1817 | * @category schemas |
| 1818 | * @since 4.0.0 |
| 1819 | */ |
| 1820 | export const ByteSizeReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1821 | "effect/schema/ByteSize", |
| 1822 | Schema.ByteSize |
| 1823 | ) |
| 1824 | |
| 1825 | /** |
| 1826 | * Reviver for persisted {@link Schema.BigDecimal} declarations. |
| 1827 | * |
| 1828 | * **When to use** |
| 1829 | * |
| 1830 | * Use when reconstructing documents that may contain the {@link Schema.BigDecimal} schema. |
| 1831 | * |
| 1832 | * @see {@link Schema.BigDecimal} for the corresponding schema |
| 1833 | * |
| 1834 | * @category schemas |
| 1835 | * @since 4.0.0 |
| 1836 | */ |
| 1837 | export const BigDecimalReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1838 | "effect/schema/BigDecimal", |
| 1839 | Schema.BigDecimal |
| 1840 | ) |
| 1841 | |
| 1842 | /** |
| 1843 | * Reviver for persisted `File` declarations. |
| 1844 | * |
| 1845 | * **When to use** |
| 1846 | * |
| 1847 | * Use when reconstructing documents that may contain the {@link Schema.File} schema. |
| 1848 | * |
| 1849 | * @see {@link Schema.File} for the corresponding schema |
| 1850 | * |
| 1851 | * @category schemas |
| 1852 | * @since 4.0.0 |
| 1853 | */ |
| 1854 | export const FileReviver: DeclarationReviver<null> = makeFixedDeclarationReviver("effect/schema/File", Schema.File) |
| 1855 | |
| 1856 | /** |
| 1857 | * Reviver for persisted `FormData` declarations. |
| 1858 | * |
| 1859 | * **When to use** |
| 1860 | * |
| 1861 | * Use when reconstructing documents that may contain the {@link Schema.FormData} schema. |
| 1862 | * |
| 1863 | * @see {@link Schema.FormData} for the corresponding schema |
| 1864 | * |
| 1865 | * @category schemas |
| 1866 | * @since 4.0.0 |
| 1867 | */ |
| 1868 | export const FormDataReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1869 | "effect/schema/FormData", |
| 1870 | Schema.FormData |
| 1871 | ) |
| 1872 | |
| 1873 | /** |
| 1874 | * Reviver for persisted `URLSearchParams` declarations. |
| 1875 | * |
| 1876 | * **When to use** |
| 1877 | * |
| 1878 | * Use when reconstructing documents that may contain the {@link Schema.URLSearchParams} schema. |
| 1879 | * |
| 1880 | * @see {@link Schema.URLSearchParams} for the corresponding schema |
| 1881 | * |
| 1882 | * @category schemas |
| 1883 | * @since 4.0.0 |
| 1884 | */ |
| 1885 | export const URLSearchParamsReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1886 | "effect/schema/URLSearchParams", |
| 1887 | Schema.URLSearchParams |
| 1888 | ) |
| 1889 | |
| 1890 | /** |
| 1891 | * Reviver for persisted `Uint8Array` declarations. |
| 1892 | * |
| 1893 | * **When to use** |
| 1894 | * |
| 1895 | * Use when reconstructing documents that may contain the {@link Schema.Uint8Array} schema. |
| 1896 | * |
| 1897 | * @see {@link Schema.Uint8Array} for the corresponding schema |
| 1898 | * |
| 1899 | * @category schemas |
| 1900 | * @since 4.0.0 |
| 1901 | */ |
| 1902 | export const Uint8ArrayReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1903 | "effect/schema/Uint8Array", |
| 1904 | Schema.Uint8Array |
| 1905 | ) |
| 1906 | |
| 1907 | /** |
| 1908 | * Reviver for persisted {@link Schema.DateTimeUtc} declarations. |
| 1909 | * |
| 1910 | * **When to use** |
| 1911 | * |
| 1912 | * Use when reconstructing documents that may contain the {@link Schema.DateTimeUtc} schema. |
| 1913 | * |
| 1914 | * @see {@link Schema.DateTimeUtc} for the corresponding schema |
| 1915 | * |
| 1916 | * @category schemas |
| 1917 | * @since 4.0.0 |
| 1918 | */ |
| 1919 | export const DateTimeUtcReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1920 | "effect/schema/DateTimeUtc", |
| 1921 | Schema.DateTimeUtc |
| 1922 | ) |
| 1923 | |
| 1924 | /** |
| 1925 | * Reviver for persisted {@link Schema.TimeZoneOffset} declarations. |
| 1926 | * |
| 1927 | * **When to use** |
| 1928 | * |
| 1929 | * Use when reconstructing documents that may contain the {@link Schema.TimeZoneOffset} schema. |
| 1930 | * |
| 1931 | * @see {@link Schema.TimeZoneOffset} for the corresponding schema |
| 1932 | * |
| 1933 | * @category schemas |
| 1934 | * @since 4.0.0 |
| 1935 | */ |
| 1936 | export const TimeZoneOffsetReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1937 | "effect/schema/TimeZoneOffset", |
| 1938 | Schema.TimeZoneOffset |
| 1939 | ) |
| 1940 | |
| 1941 | /** |
| 1942 | * Reviver for persisted {@link Schema.TimeZoneNamed} declarations. |
| 1943 | * |
| 1944 | * **When to use** |
| 1945 | * |
| 1946 | * Use when reconstructing documents that may contain the {@link Schema.TimeZoneNamed} schema. |
| 1947 | * |
| 1948 | * @see {@link Schema.TimeZoneNamed} for the corresponding schema |
| 1949 | * |
| 1950 | * @category schemas |
| 1951 | * @since 4.0.0 |
| 1952 | */ |
| 1953 | export const TimeZoneNamedReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1954 | "effect/schema/TimeZoneNamed", |
| 1955 | Schema.TimeZoneNamed |
| 1956 | ) |
| 1957 | |
| 1958 | /** |
| 1959 | * Reviver for persisted {@link Schema.TimeZone} declarations. |
| 1960 | * |
| 1961 | * **When to use** |
| 1962 | * |
| 1963 | * Use when reconstructing documents that may contain the {@link Schema.TimeZone} schema. |
| 1964 | * |
| 1965 | * @see {@link Schema.TimeZone} for the corresponding schema |
| 1966 | * |
| 1967 | * @category schemas |
| 1968 | * @since 4.0.0 |
| 1969 | */ |
| 1970 | export const TimeZoneReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1971 | "effect/schema/TimeZone", |
| 1972 | Schema.TimeZone |
| 1973 | ) |
| 1974 | |
| 1975 | /** |
| 1976 | * Reviver for persisted {@link Schema.DateTimeZoned} declarations. |
| 1977 | * |
| 1978 | * **When to use** |
| 1979 | * |
| 1980 | * Use when reconstructing documents that may contain the {@link Schema.DateTimeZoned} schema. |
| 1981 | * |
| 1982 | * @see {@link Schema.DateTimeZoned} for the corresponding schema |
| 1983 | * |
| 1984 | * @category schemas |
| 1985 | * @since 4.0.0 |
| 1986 | */ |
| 1987 | export const DateTimeZonedReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 1988 | "effect/schema/DateTimeZoned", |
| 1989 | Schema.DateTimeZoned |
| 1990 | ) |
| 1991 | |
| 1992 | /** |
| 1993 | * Reviver for persisted `isGreaterThanDate` checks. |
| 1994 | * |
| 1995 | * **When to use** |
| 1996 | * |
| 1997 | * Use when reconstructing documents that may contain checks created by {@link Schema.isGreaterThanDate}. |
| 1998 | * |
| 1999 | * @see {@link Schema.isGreaterThanDate} for creating the corresponding check |
| 2000 | * |
| 2001 | * @category validation |
| 2002 | * @since 4.0.0 |
| 2003 | */ |
| 2004 | export const isGreaterThanDateReviver: FilterReviver<{ |
| 2005 | readonly exclusiveMinimum: globalThis.Date |
| 2006 | }> = makeReviverFilter( |
| 2007 | "effect/schema/isGreaterThanDate", |
| 2008 | Schema.Struct({ exclusiveMinimum: Schema.Date }), |
| 2009 | ({ annotations, payload }) => Schema.isGreaterThanDate(payload.exclusiveMinimum, annotations) |
| 2010 | ) |
| 2011 | |
| 2012 | /** |
| 2013 | * Reviver for persisted `isGreaterThanOrEqualToDate` checks. |
| 2014 | * |
| 2015 | * **When to use** |
| 2016 | * |
| 2017 | * Use when reconstructing documents that may contain checks created by {@link Schema.isGreaterThanOrEqualToDate}. |
| 2018 | * |
| 2019 | * @see {@link Schema.isGreaterThanOrEqualToDate} for creating the corresponding check |
| 2020 | * |
| 2021 | * @category validation |
| 2022 | * @since 4.0.0 |
| 2023 | */ |
| 2024 | export const isGreaterThanOrEqualToDateReviver: FilterReviver<{ |
| 2025 | readonly minimum: globalThis.Date |
| 2026 | }> = makeReviverFilter( |
| 2027 | "effect/schema/isGreaterThanOrEqualToDate", |
| 2028 | Schema.Struct({ minimum: Schema.Date }), |
| 2029 | ({ annotations, payload }) => Schema.isGreaterThanOrEqualToDate(payload.minimum, annotations) |
| 2030 | ) |
| 2031 | |
| 2032 | /** |
| 2033 | * Reviver for persisted `isLessThanDate` checks. |
| 2034 | * |
| 2035 | * **When to use** |
| 2036 | * |
| 2037 | * Use when reconstructing documents that may contain checks created by {@link Schema.isLessThanDate}. |
| 2038 | * |
| 2039 | * @see {@link Schema.isLessThanDate} for creating the corresponding check |
| 2040 | * |
| 2041 | * @category validation |
| 2042 | * @since 4.0.0 |
| 2043 | */ |
| 2044 | export const isLessThanDateReviver: FilterReviver<{ |
| 2045 | readonly exclusiveMaximum: globalThis.Date |
| 2046 | }> = makeReviverFilter( |
| 2047 | "effect/schema/isLessThanDate", |
| 2048 | Schema.Struct({ exclusiveMaximum: Schema.Date }), |
| 2049 | ({ annotations, payload }) => Schema.isLessThanDate(payload.exclusiveMaximum, annotations) |
| 2050 | ) |
| 2051 | |
| 2052 | /** |
| 2053 | * Reviver for persisted `isLessThanOrEqualToDate` checks. |
| 2054 | * |
| 2055 | * **When to use** |
| 2056 | * |
| 2057 | * Use when reconstructing documents that may contain checks created by {@link Schema.isLessThanOrEqualToDate}. |
| 2058 | * |
| 2059 | * @see {@link Schema.isLessThanOrEqualToDate} for creating the corresponding check |
| 2060 | * |
| 2061 | * @category validation |
| 2062 | * @since 4.0.0 |
| 2063 | */ |
| 2064 | export const isLessThanOrEqualToDateReviver: FilterReviver<{ |
| 2065 | readonly maximum: globalThis.Date |
| 2066 | }> = makeReviverFilter( |
| 2067 | "effect/schema/isLessThanOrEqualToDate", |
| 2068 | Schema.Struct({ maximum: Schema.Date }), |
| 2069 | ({ annotations, payload }) => Schema.isLessThanOrEqualToDate(payload.maximum, annotations) |
| 2070 | ) |
| 2071 | |
| 2072 | /** |
| 2073 | * Reviver for persisted `isBetweenDate` checks. |
| 2074 | * |
| 2075 | * **When to use** |
| 2076 | * |
| 2077 | * Use when reconstructing documents that may contain checks created by {@link Schema.isBetweenDate}. |
| 2078 | * |
| 2079 | * @see {@link Schema.isBetweenDate} for creating the corresponding check |
| 2080 | * |
| 2081 | * @category validation |
| 2082 | * @since 4.0.0 |
| 2083 | */ |
| 2084 | export const isBetweenDateReviver: FilterReviver<{ |
| 2085 | readonly minimum: globalThis.Date |
| 2086 | readonly maximum: globalThis.Date |
| 2087 | readonly exclusiveMinimum?: true | undefined |
| 2088 | readonly exclusiveMaximum?: true | undefined |
| 2089 | }> = makeReviverFilter( |
| 2090 | "effect/schema/isBetweenDate", |
| 2091 | Schema.Struct({ |
| 2092 | minimum: Schema.Date, |
| 2093 | maximum: Schema.Date, |
| 2094 | exclusiveMinimum: Schema.optional(Schema.Literal(true)), |
| 2095 | exclusiveMaximum: Schema.optional(Schema.Literal(true)) |
| 2096 | }), |
| 2097 | ({ annotations, payload }) => Schema.isBetweenDate(payload, annotations) |
| 2098 | ) |
| 2099 | |
| 2100 | /** |
| 2101 | * Reviver for persisted `isGreaterThanBigInt` checks. |
| 2102 | * |
| 2103 | * **When to use** |
| 2104 | * |
| 2105 | * Use when reconstructing documents that may contain checks created by {@link Schema.isGreaterThanBigInt}. |
| 2106 | * |
| 2107 | * @see {@link Schema.isGreaterThanBigInt} for creating the corresponding check |
| 2108 | * |
| 2109 | * @category validation |
| 2110 | * @since 4.0.0 |
| 2111 | */ |
| 2112 | export const isGreaterThanBigIntReviver: FilterReviver<{ |
| 2113 | readonly exclusiveMinimum: bigint |
| 2114 | }> = makeReviverFilter( |
| 2115 | "effect/schema/isGreaterThanBigInt", |
| 2116 | Schema.Struct({ exclusiveMinimum: Schema.BigInt }), |
| 2117 | ({ annotations, payload }) => Schema.isGreaterThanBigInt(payload.exclusiveMinimum, annotations) |
| 2118 | ) |
| 2119 | |
| 2120 | /** |
| 2121 | * Reviver for persisted `isGreaterThanOrEqualToBigInt` checks. |
| 2122 | * |
| 2123 | * **When to use** |
| 2124 | * |
| 2125 | * Use when reconstructing documents that may contain checks created by {@link Schema.isGreaterThanOrEqualToBigInt}. |
| 2126 | * |
| 2127 | * @see {@link Schema.isGreaterThanOrEqualToBigInt} for creating the corresponding check |
| 2128 | * |
| 2129 | * @category validation |
| 2130 | * @since 4.0.0 |
| 2131 | */ |
| 2132 | export const isGreaterThanOrEqualToBigIntReviver: FilterReviver<{ |
| 2133 | readonly minimum: bigint |
| 2134 | }> = makeReviverFilter( |
| 2135 | "effect/schema/isGreaterThanOrEqualToBigInt", |
| 2136 | Schema.Struct({ minimum: Schema.BigInt }), |
| 2137 | ({ annotations, payload }) => Schema.isGreaterThanOrEqualToBigInt(payload.minimum, annotations) |
| 2138 | ) |
| 2139 | |
| 2140 | /** |
| 2141 | * Reviver for persisted `isLessThanBigInt` checks. |
| 2142 | * |
| 2143 | * **When to use** |
| 2144 | * |
| 2145 | * Use when reconstructing documents that may contain checks created by {@link Schema.isLessThanBigInt}. |
| 2146 | * |
| 2147 | * @see {@link Schema.isLessThanBigInt} for creating the corresponding check |
| 2148 | * |
| 2149 | * @category validation |
| 2150 | * @since 4.0.0 |
| 2151 | */ |
| 2152 | export const isLessThanBigIntReviver: FilterReviver<{ |
| 2153 | readonly exclusiveMaximum: bigint |
| 2154 | }> = makeReviverFilter( |
| 2155 | "effect/schema/isLessThanBigInt", |
| 2156 | Schema.Struct({ exclusiveMaximum: Schema.BigInt }), |
| 2157 | ({ annotations, payload }) => Schema.isLessThanBigInt(payload.exclusiveMaximum, annotations) |
| 2158 | ) |
| 2159 | |
| 2160 | /** |
| 2161 | * Reviver for persisted `isLessThanOrEqualToBigInt` checks. |
| 2162 | * |
| 2163 | * **When to use** |
| 2164 | * |
| 2165 | * Use when reconstructing documents that may contain checks created by {@link Schema.isLessThanOrEqualToBigInt}. |
| 2166 | * |
| 2167 | * @see {@link Schema.isLessThanOrEqualToBigInt} for creating the corresponding check |
| 2168 | * |
| 2169 | * @category validation |
| 2170 | * @since 4.0.0 |
| 2171 | */ |
| 2172 | export const isLessThanOrEqualToBigIntReviver: FilterReviver<{ |
| 2173 | readonly maximum: bigint |
| 2174 | }> = makeReviverFilter( |
| 2175 | "effect/schema/isLessThanOrEqualToBigInt", |
| 2176 | Schema.Struct({ maximum: Schema.BigInt }), |
| 2177 | ({ annotations, payload }) => Schema.isLessThanOrEqualToBigInt(payload.maximum, annotations) |
| 2178 | ) |
| 2179 | |
| 2180 | /** |
| 2181 | * Reviver for persisted `isBetweenBigInt` checks. |
| 2182 | * |
| 2183 | * **When to use** |
| 2184 | * |
| 2185 | * Use when reconstructing documents that may contain checks created by {@link Schema.isBetweenBigInt}. |
| 2186 | * |
| 2187 | * @see {@link Schema.isBetweenBigInt} for creating the corresponding check |
| 2188 | * |
| 2189 | * @category validation |
| 2190 | * @since 4.0.0 |
| 2191 | */ |
| 2192 | export const isBetweenBigIntReviver: FilterReviver<{ |
| 2193 | readonly minimum: bigint |
| 2194 | readonly maximum: bigint |
| 2195 | readonly exclusiveMinimum?: true | undefined |
| 2196 | readonly exclusiveMaximum?: true | undefined |
| 2197 | }> = makeReviverFilter( |
| 2198 | "effect/schema/isBetweenBigInt", |
| 2199 | Schema.Struct({ |
| 2200 | minimum: Schema.BigInt, |
| 2201 | maximum: Schema.BigInt, |
| 2202 | exclusiveMinimum: Schema.optional(Schema.Literal(true)), |
| 2203 | exclusiveMaximum: Schema.optional(Schema.Literal(true)) |
| 2204 | }), |
| 2205 | ({ annotations, payload }) => Schema.isBetweenBigInt(payload, annotations) |
| 2206 | ) |
| 2207 | |
| 2208 | /** |
| 2209 | * Reviver for persisted `Json` declarations. |
| 2210 | * |
| 2211 | * **When to use** |
| 2212 | * |
| 2213 | * Use when reconstructing documents that may contain the {@link Schema.Json} schema. |
| 2214 | * |
| 2215 | * @see {@link Schema.Json} for the corresponding immutable JSON schema |
| 2216 | * |
| 2217 | * @category schemas |
| 2218 | * @since 4.0.0 |
| 2219 | */ |
| 2220 | export const JsonReviver: DeclarationReviver<null> = makeFixedDeclarationReviver("effect/schema/Json", Schema.Json) |
| 2221 | |
| 2222 | /** |
| 2223 | * Reviver for persisted `MutableJson` declarations. |
| 2224 | * |
| 2225 | * **When to use** |
| 2226 | * |
| 2227 | * Use when reconstructing documents that may contain the {@link Schema.MutableJson} schema. |
| 2228 | * |
| 2229 | * @see {@link Schema.MutableJson} for the corresponding mutable JSON schema |
| 2230 | * |
| 2231 | * @category schemas |
| 2232 | * @since 4.0.0 |
| 2233 | */ |
| 2234 | export const MutableJsonReviver: DeclarationReviver<null> = makeFixedDeclarationReviver( |
| 2235 | "effect/schema/MutableJson", |
| 2236 | Schema.MutableJson |
| 2237 | ) |
| 2238 | |
| 2239 | const jsonSchemaRevivers: ReadonlyArray<AnyReviver> = [ |
| 2240 | JsonReviver, |
| 2241 | isPatternReviver, |
| 2242 | isFiniteReviver, |
| 2243 | isGreaterThanReviver, |
| 2244 | isGreaterThanOrEqualToReviver, |
| 2245 | isLessThanReviver, |
| 2246 | isLessThanOrEqualToReviver, |
| 2247 | isMultipleOfReviver, |
| 2248 | isIntReviver, |
| 2249 | isMinLengthReviver, |
| 2250 | isMaxLengthReviver, |
| 2251 | isMinCodePointsReviver, |
| 2252 | isMaxCodePointsReviver, |
| 2253 | isMinPropertiesReviver, |
| 2254 | isMaxPropertiesReviver, |
| 2255 | isPropertyNamesReviver, |
| 2256 | isUniqueReviver |
| 2257 | ] |
| 2258 | |
| 2259 | /** |
| 2260 | * Options for importing JSON Schema Draft 2020-12 documents. |
| 2261 | * |
| 2262 | * **When to use** |
| 2263 | * |
| 2264 | * Use when you need to configure pattern handling or transform each JSON Schema node before translation. |
| 2265 | * |
| 2266 | * **Details** |
| 2267 | * |
| 2268 | * `patterns` controls pattern constraints reached during best-effort translation, including `pattern`, the keys of |
| 2269 | * `patternProperties`, and patterns nested in `propertyNames`: |
| 2270 | * |
| 2271 | * - `"error"` rejects the document and is the default. |
| 2272 | * - `"ignore"` skips the constraint. |
| 2273 | * - `"apply"` compiles and enforces the constraint with the runtime's native regular expression engine in Unicode |
| 2274 | * mode. Patterns that cannot be compiled with the `u` flag are rejected with their source path. |
| 2275 | * |
| 2276 | * **Gotchas** |
| 2277 | * |
| 2278 | * Use `patterns: "apply"` only for trusted documents because regular expression evaluation may block for an unbounded |
| 2279 | * amount of time. `patterns: "ignore"` can admit values the source rejects, but can also reject previously valid |
| 2280 | * values inside `oneOf` when removing constraints makes multiple branches match. |
| 2281 | * Input documents are assumed to be valid Draft 2020-12 schemas and are not validated against the meta-schema. |
| 2282 | * Ignoring `patternProperties` also skips its value constraints and `additionalProperties`, because matching keys cannot |
| 2283 | * be determined without evaluating the patterns. |
| 2284 | * `onEnter` must return a JSON Schema object. Its result is used directly, and exceptions raised by the callback pass |
| 2285 | * through unchanged. |
| 2286 | * |
| 2287 | * @category models |
| 2288 | * @since 4.0.0 |
| 2289 | */ |
| 2290 | export interface FromJsonSchemaOptions { |
| 2291 | readonly onEnter?: ((schema: JsonSchema.JsonSchema) => JsonSchema.JsonSchema) | undefined |
| 2292 | /** |
| 2293 | * Controls how reached JSON Schema regular expression patterns are imported. |
| 2294 | * |
| 2295 | * @default "error" |
| 2296 | */ |
| 2297 | readonly patterns?: "error" | "ignore" | "apply" | undefined |
| 2298 | } |
| 2299 | |
| 2300 | /** |
| 2301 | * Runtime and TypeScript source generated for one schema. |
| 2302 | * |
| 2303 | * @category models |
| 2304 | * @since 4.0.0 |
| 2305 | */ |
| 2306 | export interface Code { |
| 2307 | readonly runtime: string |
| 2308 | readonly Type: string |
| 2309 | } |
| 2310 | |
| 2311 | /** |
| 2312 | * Creates generated runtime and TypeScript source strings for a schema. |
| 2313 | * |
| 2314 | * @category constructors |
| 2315 | * @since 4.0.0 |
| 2316 | */ |
| 2317 | export const makeCode: (runtime: string, Type: string) => Code = InternalToCodeDocument.makeCode |
| 2318 | |
| 2319 | /** |
| 2320 | * Auxiliary source artifact emitted while generating schema code. |
| 2321 | * |
| 2322 | * @category models |
| 2323 | * @since 4.0.0 |
| 2324 | */ |
| 2325 | export type Artifact = |
| 2326 | | { |
| 2327 | readonly _tag: "Symbol" |
| 2328 | readonly identifier: string |
| 2329 | readonly code: Code |
| 2330 | } |
| 2331 | | { |
| 2332 | readonly _tag: "Enum" |
| 2333 | readonly identifier: string |
| 2334 | readonly code: Code |
| 2335 | } |
| 2336 | | { |
| 2337 | readonly _tag: "Import" |
| 2338 | readonly importDeclaration: string |
| 2339 | } |
| 2340 | |
| 2341 | /** |
| 2342 | * Generated schema code together with named references and auxiliary artifacts. |
| 2343 | * |
| 2344 | * @category models |
| 2345 | * @since 4.0.0 |
| 2346 | */ |
| 2347 | export interface CodeDocument { |
| 2348 | readonly codes: ReadonlyArray<Code> |
| 2349 | readonly references: { |
| 2350 | readonly nonRecursives: ReadonlyArray<{ |
| 2351 | readonly $ref: string |
| 2352 | readonly code: Code |
| 2353 | }> |
| 2354 | readonly recursives: Readonly<Record<string, Code>> |
| 2355 | } |
| 2356 | readonly artifacts: ReadonlyArray<Artifact> |
| 2357 | } |
| 2358 | |
| 2359 | /** |
| 2360 | * Information supplied to a reference policy for one representation candidate. |
| 2361 | * |
| 2362 | * @category models |
| 2363 | * @since 4.0.0 |
| 2364 | */ |
| 2365 | export interface ReferencePolicyInput { |
| 2366 | /** The encoded-side AST owner for the candidate. Contextual copies can share the same owner. */ |
| 2367 | readonly ast: SchemaAST.AST |
| 2368 | /** The number of times this candidate was encountered. Structurally equal ASTs remain distinct candidates. */ |
| 2369 | readonly occurrences: number |
| 2370 | /** The resolved encoded-side identifier, including an inherited `Encoded` suffix when applicable. */ |
| 2371 | readonly identifier: string | undefined |
| 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * Function that chooses whether a representation candidate is emitted as a named reference. |
| 2376 | * |
| 2377 | * **When to use** |
| 2378 | * |
| 2379 | * Use when you need reference allocation based on schema identity, occurrence counts, identifiers, or another |
| 2380 | * application-specific rule. |
| 2381 | * |
| 2382 | * **Details** |
| 2383 | * |
| 2384 | * Return a reference name to extract the candidate, or `undefined` to keep it inline. The policy is called once per |
| 2385 | * candidate after all occurrences have been counted. The `identifier` is the resolved identifier for the encoded AST, |
| 2386 | * including an `Encoded` suffix when an identifier is inherited from the source side of an encoding. If different |
| 2387 | * candidates request the same name, later names receive numeric suffixes in encounter order. |
| 2388 | * |
| 2389 | * **Gotchas** |
| 2390 | * |
| 2391 | * Recursive candidates always require a reference. When the policy returns `undefined` for one, the generator assigns |
| 2392 | * a synthetic name. Treat the input AST as immutable and keep the policy deterministic. |
| 2393 | * |
| 2394 | * @see {@link ToRepresentationOptions} for configuring representation generation |
| 2395 | * |
| 2396 | * @category models |
| 2397 | * @since 4.0.0 |
| 2398 | */ |
| 2399 | export type ReferencePolicy = (input: ReferencePolicyInput) => string | undefined |
| 2400 | |
| 2401 | /** |
| 2402 | * Options for generating schema representations. |
| 2403 | * |
| 2404 | * @category configuration |
| 2405 | * @since 4.0.0 |
| 2406 | */ |
| 2407 | export interface ToRepresentationOptions { |
| 2408 | /** |
| 2409 | * Chooses which representation candidates are extracted as named references. |
| 2410 | * |
| 2411 | * **Details** |
| 2412 | * |
| 2413 | * The default policy returns the resolved `identifier`, so anonymous non-recursive candidates remain inline even when |
| 2414 | * they occur more than once. |
| 2415 | * |
| 2416 | * **Gotchas** |
| 2417 | * |
| 2418 | * Recursive candidates always require a reference and receive a synthetic name when the policy returns `undefined`. |
| 2419 | * |
| 2420 | * @default ({ identifier }) => identifier |
| 2421 | */ |
| 2422 | readonly referencePolicy?: ReferencePolicy | undefined |
| 2423 | } |
| 2424 | |
| 2425 | /** |
| 2426 | * Lowers the encoded side of an AST to a live representation document. |
| 2427 | * |
| 2428 | * **When to use** |
| 2429 | * |
| 2430 | * Use when you have one `SchemaAST.AST` and need a live `Document` for inspection, persistence, or compilation. |
| 2431 | * |
| 2432 | * **Details** |
| 2433 | * |
| 2434 | * Apply `SchemaAST.toType` to the AST first to lower its type side instead. The optional reference policy controls which |
| 2435 | * candidates are moved into the document's shared reference table. TypeScript-only |
| 2436 | * distinctions such as those added by `Schema.brand` are absent from the AST |
| 2437 | * and cannot be reconstructed from the resulting document. |
| 2438 | * |
| 2439 | * @see {@link toRepresentations} for multiple roots sharing one reference table |
| 2440 | * |
| 2441 | * @category constructors |
| 2442 | * @since 4.0.0 |
| 2443 | */ |
| 2444 | export function toRepresentation(ast: SchemaAST.AST, options?: ToRepresentationOptions): Document { |
| 2445 | return InternalToRepresentation.toRepresentation(ast, options) |
| 2446 | } |
| 2447 | |
| 2448 | /** |
| 2449 | * Lowers one or more AST encoded sides in a shared reference environment. |
| 2450 | * |
| 2451 | * **When to use** |
| 2452 | * |
| 2453 | * Use when several AST roots must share identifiers, occurrence counts, recursion, and allocated reference names. |
| 2454 | * |
| 2455 | * **Details** |
| 2456 | * |
| 2457 | * Apply `SchemaAST.toType` to an AST first to lower its type side instead. The reference policy observes candidates from |
| 2458 | * every root before any representation is emitted. |
| 2459 | * |
| 2460 | * @see {@link toRepresentation} for a single AST root |
| 2461 | * |
| 2462 | * @category constructors |
| 2463 | * @since 4.0.0 |
| 2464 | */ |
| 2465 | export function toRepresentations( |
| 2466 | asts: readonly [SchemaAST.AST, ...Array<SchemaAST.AST>], |
| 2467 | options?: ToRepresentationOptions |
| 2468 | ): MultiDocument { |
| 2469 | return InternalToRepresentation.toRepresentations(asts, options) |
| 2470 | } |
| 2471 | |
| 2472 | /** |
| 2473 | * Wraps a single representation document as a multi-document with one root. |
| 2474 | * |
| 2475 | * **When to use** |
| 2476 | * |
| 2477 | * Use when an API such as `toCodeDocument` requires a `MultiDocument`. |
| 2478 | * |
| 2479 | * @category transforming |
| 2480 | * @since 4.0.0 |
| 2481 | */ |
| 2482 | export function toMultiDocument(document: Document): MultiDocument { |
| 2483 | return { |
| 2484 | representations: [document.representation], |
| 2485 | references: document.references |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | /** |
| 2490 | * Compiles a live representation document to JSON Schema Draft 2020-12. |
| 2491 | * |
| 2492 | * **When to use** |
| 2493 | * |
| 2494 | * Use when you need JSON Schema output from a representation whose checks carry compiler annotations. |
| 2495 | * |
| 2496 | * **Details** |
| 2497 | * |
| 2498 | * The generated document is intended for preliminary validation. JSON Schema |
| 2499 | * and Effect checks do not always have identical semantics, so the Effect |
| 2500 | * decoder remains the final authority. Passing JSON Schema validation does not |
| 2501 | * guarantee that Effect decoding will succeed. |
| 2502 | * |
| 2503 | * A `oneOf` union exports as `anyOf` when any branch contains a known |
| 2504 | * approximation; otherwise it retains `oneOf`. Approximation propagates through |
| 2505 | * checks, nested schemas, check dependencies, and recursive references. Filters |
| 2506 | * without a `toJsonSchema` callback and opaque declarations are approximate. |
| 2507 | * Custom callbacks declare their result using {@link ToJsonSchema.CheckOutput}. |
| 2508 | * |
| 2509 | * **Gotchas** |
| 2510 | * |
| 2511 | * - Reference allocation is already fixed in the input `Document`. The inherited `referencePolicy` option has no effect |
| 2512 | * here; pass it to {@link toRepresentation} when creating the document. |
| 2513 | * - String length, RegExp flags, and decoded-object property checks can differ from Effect validation. |
| 2514 | * - Opaque declarations are represented by an unconstrained JSON Schema. |
| 2515 | * - Check callback results are used directly, and exceptions raised by a callback pass through unchanged. Callbacks |
| 2516 | * must treat their input schemas as immutable. Each returned fragment must be a valid JSON Schema object graph and must |
| 2517 | * not be mutated after the callback returns. The callback author is responsible for the emitted semantics. |
| 2518 | * - Local definition references returned by callbacks are resolved together with compiler-generated references. |
| 2519 | * Invalid JSON Pointer URI fragments throw an `Error`. |
| 2520 | * - The default `onExcessProperty: "ignore"` matches the decoder default. Use `onExcessProperty: "error"` in both |
| 2521 | * places when a closed object contract is required. |
| 2522 | * - Record keys require an exact translation to a pattern before they can select `patternProperties` values. |
| 2523 | * Otherwise, `"ignore"` omits that index-signature constraint. In `"error"` mode, the compiler emits the generated |
| 2524 | * key schemas under `propertyNames` and allows unmatched properties to satisfy any candidate index value schema. |
| 2525 | * Exact selectors from other index signatures are retained, even when their value schemas are approximate. |
| 2526 | * |
| 2527 | * @see {@link toJsonSchemaMultiDocument} for multiple roots sharing definitions |
| 2528 | * |
| 2529 | * @category transforming |
| 2530 | * @since 4.0.0 |
| 2531 | */ |
| 2532 | export function toJsonSchemaDocument( |
| 2533 | document: Document, |
| 2534 | options?: Schema.ToJsonSchemaOptions |
| 2535 | ): JsonSchema.Document<"draft-2020-12"> { |
| 2536 | return InternalToJsonSchemaDocument.toJsonSchemaDocument(document, options) |
| 2537 | } |
| 2538 | |
| 2539 | /** |
| 2540 | * Compiles multiple live representations to a shared JSON Schema Draft 2020-12 document. |
| 2541 | * |
| 2542 | * **When to use** |
| 2543 | * |
| 2544 | * Use when several representation roots must share the same JSON Schema definitions. |
| 2545 | * |
| 2546 | * **Details** |
| 2547 | * |
| 2548 | * Uses the same approximation tracking and `oneOf` and record-key fallbacks as |
| 2549 | * {@link toJsonSchemaDocument}, including across shared and recursive definitions. |
| 2550 | * |
| 2551 | * **Gotchas** |
| 2552 | * |
| 2553 | * - Reference allocation is already fixed in the input `MultiDocument`. The inherited `referencePolicy` option has no |
| 2554 | * effect here; pass it to {@link toRepresentations} when creating the document. |
| 2555 | * - Every definition is compiled, including definitions that are not reachable from a root. Check callbacks must treat |
| 2556 | * their input schemas as immutable. Each returned fragment must be a valid JSON Schema object graph and must not be |
| 2557 | * mutated after the callback returns. Local definition references returned by callbacks are resolved together with |
| 2558 | * compiler-generated references. Invalid JSON Pointer URI fragments throw an `Error`. |
| 2559 | * - String length, RegExp flags, decoded-object property checks, and custom check callbacks can differ from |
| 2560 | * Effect validation, as described by {@link toJsonSchemaDocument}. |
| 2561 | * |
| 2562 | * @see {@link toJsonSchemaDocument} for a single root |
| 2563 | * |
| 2564 | * @category transforming |
| 2565 | * @since 4.0.0 |
| 2566 | */ |
| 2567 | export function toJsonSchemaMultiDocument( |
| 2568 | document: MultiDocument, |
| 2569 | options?: Schema.ToJsonSchemaOptions |
| 2570 | ): JsonSchema.MultiDocument<"draft-2020-12"> { |
| 2571 | return InternalToJsonSchemaDocument.toJsonSchemaMultiDocument(document, options) |
| 2572 | } |
| 2573 | |
| 2574 | /** |
| 2575 | * Generates TypeScript source for live schema representations and their definitions. |
| 2576 | * |
| 2577 | * **When to use** |
| 2578 | * |
| 2579 | * Use when custom declarations and checks provide `toCode` callbacks and must be emitted without a central handler registry. |
| 2580 | * |
| 2581 | * **Gotchas** |
| 2582 | * |
| 2583 | * Opaque declarations and leaf checks require `toCode` callbacks. Callback results are used directly, and exceptions raised by a callback pass through unchanged. |
| 2584 | * |
| 2585 | * @category transforming |
| 2586 | * @since 4.0.0 |
| 2587 | */ |
| 2588 | export function toCodeDocument(document: MultiDocument): CodeDocument { |
| 2589 | return InternalToCodeDocument.toCodeDocument(document) |
| 2590 | } |
| 2591 | |
| 2592 | const RepresentationSchema = Schema.suspend( |
| 2593 | (): Schema.Codec<Representation, unknown> => RepresentationUnion |
| 2594 | ) |
| 2595 | const RepresentationsSchema = Schema.Array(RepresentationSchema) |
| 2596 | |
| 2597 | const RepresentationAnnotationSchema = Schema.Struct({ |
| 2598 | id: Schema.NonEmptyString, |
| 2599 | payload: Schema.Json |
| 2600 | }) |
| 2601 | |
| 2602 | const CheckRepresentationAnnotationSchema = Schema.Struct({ |
| 2603 | ...RepresentationAnnotationSchema.fields, |
| 2604 | schemas: Schema.optional(RepresentationsSchema) |
| 2605 | }) |
| 2606 | |
| 2607 | function pruneAnnotations( |
| 2608 | annotations: Readonly<Record<string, unknown>> |
| 2609 | ): Option.Option<Readonly<Record<string, Schema.Json>>> { |
| 2610 | const out: Record<string, Schema.Json> = {} |
| 2611 | for (const [key, value] of Object.entries(annotations)) { |
| 2612 | if (SchemaAST.isJson(value)) { |
| 2613 | InternalRecord.assignProperty(out, key, value) |
| 2614 | } |
| 2615 | } |
| 2616 | return Object.keys(out).length === 0 ? Option.none() : Option.some(out) |
| 2617 | } |
| 2618 | |
| 2619 | const AnnotationsSchema = Schema.optional( |
| 2620 | Schema.Record(Schema.String, Schema.Unknown) |
| 2621 | ).pipe( |
| 2622 | Schema.encodeTo(Schema.optionalKey(Schema.JsonObject), { |
| 2623 | decode: InternalGetter.passthroughSubtype(), |
| 2624 | encode: InternalGetter.transformOptional((annotations) => |
| 2625 | Option.isNone(annotations) || annotations.value === undefined |
| 2626 | ? Option.none() |
| 2627 | : pruneAnnotations(annotations.value) |
| 2628 | ) |
| 2629 | }) |
| 2630 | ) |
| 2631 | |
| 2632 | const CheckSchema = Schema.suspend((): Schema.Codec<Check, unknown> => CheckUnion) |
| 2633 | const ChecksSchema = Schema.Array(CheckSchema) |
| 2634 | const KeywordFields = { |
| 2635 | annotations: AnnotationsSchema, |
| 2636 | checks: ChecksSchema |
| 2637 | } |
| 2638 | const FilterSchema = Schema.Struct({ |
| 2639 | _tag: Schema.tag("Filter"), |
| 2640 | representation: CheckRepresentationAnnotationSchema, |
| 2641 | annotations: AnnotationsSchema, |
| 2642 | aborted: Schema.Boolean |
| 2643 | }) |
| 2644 | const FilterGroupSchema = Schema.Struct({ |
| 2645 | _tag: Schema.tag("FilterGroup"), |
| 2646 | representation: Schema.optional(CheckRepresentationAnnotationSchema), |
| 2647 | annotations: AnnotationsSchema, |
| 2648 | checks: Schema.NonEmptyArray(CheckSchema) |
| 2649 | }) |
| 2650 | const CheckUnion = Schema.Union([FilterSchema, FilterGroupSchema]) |
| 2651 | |
| 2652 | function makeKeywordSchema<Tag extends Exclude<Representation["_tag"], "Reference">>(tag: Tag) { |
| 2653 | return Schema.Struct({ |
| 2654 | _tag: Schema.tag(tag), |
| 2655 | ...KeywordFields |
| 2656 | }) |
| 2657 | } |
| 2658 | |
| 2659 | const DeclarationSchema = Schema.Struct({ |
| 2660 | _tag: Schema.tag("Declaration"), |
| 2661 | representation: RepresentationAnnotationSchema, |
| 2662 | annotations: AnnotationsSchema, |
| 2663 | typeParameters: RepresentationsSchema, |
| 2664 | checks: ChecksSchema |
| 2665 | }) |
| 2666 | const SuspendSchema = Schema.Struct({ |
| 2667 | _tag: Schema.tag("Suspend"), |
| 2668 | annotations: AnnotationsSchema, |
| 2669 | checks: Schema.Tuple([]), |
| 2670 | thunk: RepresentationSchema |
| 2671 | }) |
| 2672 | function makeValueSchema<Type extends string, Value>(type: Type, value: Schema.Codec<Value>) { |
| 2673 | return value.pipe( |
| 2674 | Schema.encodeTo(Schema.Struct({ type: Schema.tag(type), value }), { |
| 2675 | decode: InternalGetter.transform((encoded: { readonly type: Type; readonly value: Value }) => encoded.value), |
| 2676 | encode: InternalGetter.transform((value: Value) => ({ type, value })) |
| 2677 | }) |
| 2678 | ) |
| 2679 | } |
| 2680 | const StringValueCodec = makeValueSchema("string", Schema.String) |
| 2681 | const NumberValueCodec = makeValueSchema("number", Schema.Number) |
| 2682 | const LiteralSchema = Schema.Struct({ |
| 2683 | _tag: Schema.tag("Literal"), |
| 2684 | ...KeywordFields, |
| 2685 | literal: Schema.Union([ |
| 2686 | StringValueCodec, |
| 2687 | makeValueSchema("number", Schema.Finite), |
| 2688 | makeValueSchema("bigint", Schema.BigInt), |
| 2689 | makeValueSchema("boolean", Schema.Boolean) |
| 2690 | ]) |
| 2691 | }) |
| 2692 | const UniqueSymbolSchema = Schema.Struct({ |
| 2693 | _tag: Schema.tag("UniqueSymbol"), |
| 2694 | ...KeywordFields, |
| 2695 | symbol: Schema.Symbol |
| 2696 | }) |
| 2697 | const EnumSchema = Schema.Struct({ |
| 2698 | _tag: Schema.tag("Enum"), |
| 2699 | ...KeywordFields, |
| 2700 | enums: Schema.Array(Schema.Tuple([ |
| 2701 | Schema.String, |
| 2702 | Schema.Union([StringValueCodec, NumberValueCodec]) |
| 2703 | ])) |
| 2704 | }) |
| 2705 | const TemplateLiteralSchema = Schema.Struct({ |
| 2706 | _tag: Schema.tag("TemplateLiteral"), |
| 2707 | ...KeywordFields, |
| 2708 | parts: RepresentationsSchema |
| 2709 | }) |
| 2710 | const ElementSchema = Schema.Struct({ |
| 2711 | isOptional: Schema.Boolean, |
| 2712 | type: RepresentationSchema, |
| 2713 | annotations: AnnotationsSchema |
| 2714 | }) |
| 2715 | const ArraysSchema = Schema.Struct({ |
| 2716 | _tag: Schema.tag("Arrays"), |
| 2717 | ...KeywordFields, |
| 2718 | elements: Schema.Array(ElementSchema), |
| 2719 | rest: RepresentationsSchema |
| 2720 | }) |
| 2721 | const PropertySignatureSchema = Schema.Struct({ |
| 2722 | name: Schema.Union([ |
| 2723 | StringValueCodec, |
| 2724 | NumberValueCodec, |
| 2725 | makeValueSchema("symbol", Schema.Symbol) |
| 2726 | ]), |
| 2727 | type: RepresentationSchema, |
| 2728 | isOptional: Schema.Boolean, |
| 2729 | isMutable: Schema.Boolean, |
| 2730 | annotations: AnnotationsSchema |
| 2731 | }) |
| 2732 | const IndexSignatureSchema = Schema.Struct({ |
| 2733 | parameter: RepresentationSchema, |
| 2734 | type: RepresentationSchema |
| 2735 | }) |
| 2736 | const ObjectsSchema = Schema.Struct({ |
| 2737 | _tag: Schema.tag("Objects"), |
| 2738 | ...KeywordFields, |
| 2739 | propertySignatures: Schema.Array(PropertySignatureSchema), |
| 2740 | indexSignatures: Schema.Array(IndexSignatureSchema) |
| 2741 | }) |
| 2742 | const UnionSchema = Schema.Struct({ |
| 2743 | _tag: Schema.tag("Union"), |
| 2744 | ...KeywordFields, |
| 2745 | types: RepresentationsSchema, |
| 2746 | options: Schema.optionalKey(Schema.Struct({ |
| 2747 | mode: Schema.optionalKey(Schema.Literals(["anyOf", "oneOf"])) |
| 2748 | })) |
| 2749 | }) |
| 2750 | const ReferenceSchema = Schema.Struct({ |
| 2751 | _tag: Schema.tag("Reference"), |
| 2752 | $ref: Schema.NonEmptyString |
| 2753 | }) |
| 2754 | |
| 2755 | const RepresentationUnion = Schema.Union([ |
| 2756 | DeclarationSchema, |
| 2757 | ReferenceSchema, |
| 2758 | SuspendSchema, |
| 2759 | makeKeywordSchema("Null"), |
| 2760 | makeKeywordSchema("Undefined"), |
| 2761 | makeKeywordSchema("Void"), |
| 2762 | makeKeywordSchema("Never"), |
| 2763 | makeKeywordSchema("Unknown"), |
| 2764 | makeKeywordSchema("Any"), |
| 2765 | makeKeywordSchema("String"), |
| 2766 | makeKeywordSchema("Number"), |
| 2767 | makeKeywordSchema("Boolean"), |
| 2768 | makeKeywordSchema("BigInt"), |
| 2769 | makeKeywordSchema("Symbol"), |
| 2770 | makeKeywordSchema("ObjectKeyword"), |
| 2771 | LiteralSchema, |
| 2772 | UniqueSymbolSchema, |
| 2773 | EnumSchema, |
| 2774 | TemplateLiteralSchema, |
| 2775 | ArraysSchema, |
| 2776 | ObjectsSchema, |
| 2777 | UnionSchema |
| 2778 | ]) |
| 2779 | |
| 2780 | const ReferencesSchema = Schema.Record(Schema.String, RepresentationSchema) |
| 2781 | |
| 2782 | const DocumentFromJson: Schema.Codec<Document, Schema.Json> = Schema.toCodecJson( |
| 2783 | Schema.Struct({ |
| 2784 | representation: RepresentationSchema, |
| 2785 | references: ReferencesSchema |
| 2786 | }) |
| 2787 | ) |
| 2788 | |
| 2789 | const MultiDocumentFromJson: Schema.Codec<MultiDocument, Schema.Json> = Schema.toCodecJson( |
| 2790 | Schema.Struct({ |
| 2791 | representations: Schema.NonEmptyArray(RepresentationSchema), |
| 2792 | references: ReferencesSchema |
| 2793 | }) |
| 2794 | ) |
| 2795 | |
| 2796 | const encodeDocument = Schema.encodeSync(DocumentFromJson) |
| 2797 | const encodeMultiDocument = Schema.encodeSync(MultiDocumentFromJson) |
| 2798 | const decodeDocument = Schema.decodeSync(DocumentFromJson) |
| 2799 | const decodeMultiDocument = Schema.decodeSync(MultiDocumentFromJson) |
| 2800 | |
| 2801 | /** |
| 2802 | * Projects a live single-root representation document and encodes it as JSON. |
| 2803 | * |
| 2804 | * **When to use** |
| 2805 | * |
| 2806 | * Use when you need a stable JSON value for storage or transport after calling `toRepresentation`. |
| 2807 | * |
| 2808 | * **Gotchas** |
| 2809 | * |
| 2810 | * Generic annotations that are not JSON are omitted. Invalid persistence identities and unsupported structural values throw an `Error` containing their representation path. |
| 2811 | * |
| 2812 | * @see {@link toRepresentation} for constructing the live document |
| 2813 | * @see {@link toJsonMultiDocument} for documents with multiple roots |
| 2814 | * |
| 2815 | * @category encoding |
| 2816 | * @since 4.0.0 |
| 2817 | */ |
| 2818 | export function toJson(document: Document): Schema.Json { |
| 2819 | return encodeDocument(document) |
| 2820 | } |
| 2821 | |
| 2822 | /** |
| 2823 | * Projects a live multi-root representation document and encodes it as JSON. |
| 2824 | * |
| 2825 | * **When to use** |
| 2826 | * |
| 2827 | * Use when you need one JSON value for multiple live roots that share a reference environment. |
| 2828 | * |
| 2829 | * **Gotchas** |
| 2830 | * |
| 2831 | * The root order and shared reference keys are preserved, while non-JSON generic annotations are omitted. |
| 2832 | * |
| 2833 | * @see {@link toRepresentations} for constructing the live multi-document |
| 2834 | * @see {@link toJson} for a single-root document |
| 2835 | * |
| 2836 | * @category encoding |
| 2837 | * @since 4.0.0 |
| 2838 | */ |
| 2839 | export function toJsonMultiDocument(document: MultiDocument): Schema.Json { |
| 2840 | return encodeMultiDocument(document) |
| 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * Decodes a persisted single-root representation document from JSON. |
| 2845 | * |
| 2846 | * **When to use** |
| 2847 | * |
| 2848 | * Use when reading a representation document from storage or transport before inspecting it or passing it to `fromRepresentation`. |
| 2849 | * |
| 2850 | * **Gotchas** |
| 2851 | * |
| 2852 | * Invalid documents throw a schema decoding error. Decoding does not reconstruct runtime callbacks. |
| 2853 | * |
| 2854 | * @see {@link toJson} for encoding a document |
| 2855 | * @see {@link fromRepresentation} for reconstructing a runtime schema |
| 2856 | * @see {@link fromJsonMultiDocument} for multiple roots sharing references |
| 2857 | * |
| 2858 | * @category decoding |
| 2859 | * @since 4.0.0 |
| 2860 | */ |
| 2861 | export function fromJson(input: Schema.Json): Document { |
| 2862 | return decodeDocument(input) |
| 2863 | } |
| 2864 | |
| 2865 | /** |
| 2866 | * Decodes a persisted multi-root representation document from JSON. |
| 2867 | * |
| 2868 | * **When to use** |
| 2869 | * |
| 2870 | * Use when reading multiple representation roots that share references before inspecting them or passing them to `fromRepresentations`. |
| 2871 | * |
| 2872 | * **Gotchas** |
| 2873 | * |
| 2874 | * Invalid documents throw a schema decoding error. Decoding does not reconstruct runtime callbacks. |
| 2875 | * |
| 2876 | * @see {@link toJsonMultiDocument} for encoding a multi-document |
| 2877 | * @see {@link fromRepresentations} for reconstructing runtime schemas |
| 2878 | * @see {@link fromJson} for a single root |
| 2879 | * |
| 2880 | * @category decoding |
| 2881 | * @since 4.0.0 |
| 2882 | */ |
| 2883 | export function fromJsonMultiDocument(input: Schema.Json): MultiDocument { |
| 2884 | return decodeMultiDocument(input) |
| 2885 | } |
| 2886 | |
| 2887 | /** |
| 2888 | * Reconstructs a runtime schema from a representation document. |
| 2889 | * |
| 2890 | * **When to use** |
| 2891 | * |
| 2892 | * Use when you have decoded or constructed a document whose declaration and check annotations may require revivers. |
| 2893 | * |
| 2894 | * **Gotchas** |
| 2895 | * |
| 2896 | * Revivers are resolved locally by `id`; none are installed implicitly. Reviver results are used directly, and exceptions raised by a reviver pass through unchanged. |
| 2897 | * |
| 2898 | * **Example** (Restoring a persisted schema) |
| 2899 | * |
| 2900 | * ```ts import.meta.vitest |
| 2901 | * import { Schema, SchemaRepresentation } from "effect" |
| 2902 | * |
| 2903 | * const document = SchemaRepresentation.toRepresentation(Schema.Struct({ name: Schema.String }).ast) |
| 2904 | * const persisted = SchemaRepresentation.toJson(document) |
| 2905 | * const restored = SchemaRepresentation.fromJson(persisted) |
| 2906 | * const schema = SchemaRepresentation.fromRepresentation(restored, { revivers: [] }) |
| 2907 | * const Person = Schema.make<Schema.Codec<{ readonly name: string }>>(schema.ast) |
| 2908 | * |
| 2909 | * Schema.decodeUnknownSync(Person)({ name: "Ada" }) // => { name: "Ada" } |
| 2910 | * ``` |
| 2911 | * |
| 2912 | * @see {@link fromJson} for decoding a persisted document |
| 2913 | * @see {@link fromRepresentations} for multiple roots sharing references |
| 2914 | * |
| 2915 | * @category transforming |
| 2916 | * @since 4.0.0 |
| 2917 | */ |
| 2918 | export function fromRepresentation( |
| 2919 | document: Document, |
| 2920 | options: { readonly revivers: ReadonlyArray<AnyReviver> } |
| 2921 | ): Schema.Top { |
| 2922 | return InternalFromRepresentation.fromRepresentation(document, options.revivers) |
| 2923 | } |
| 2924 | |
| 2925 | /** |
| 2926 | * Reconstructs multiple runtime schemas from a representation multi-document. |
| 2927 | * |
| 2928 | * **When to use** |
| 2929 | * |
| 2930 | * Use when multiple roots must be rebuilt in one shared reference environment. |
| 2931 | * |
| 2932 | * **Gotchas** |
| 2933 | * |
| 2934 | * Only references reachable from a root are revived. Revivers are resolved locally by `id`; none are installed implicitly. |
| 2935 | * |
| 2936 | * @see {@link fromJsonMultiDocument} for decoding a persisted multi-document |
| 2937 | * @see {@link fromRepresentation} for a single root |
| 2938 | * |
| 2939 | * @category transforming |
| 2940 | * @since 4.0.0 |
| 2941 | */ |
| 2942 | export function fromRepresentations( |
| 2943 | document: MultiDocument, |
| 2944 | options: { readonly revivers: ReadonlyArray<AnyReviver> } |
| 2945 | ): readonly [Schema.Top, ...Array<Schema.Top>] { |
| 2946 | return InternalFromRepresentation.fromRepresentations(document, options.revivers) |
| 2947 | } |
| 2948 | |
| 2949 | /** |
| 2950 | * Imports a JSON Schema Draft 2020-12 document as a runtime schema. |
| 2951 | * |
| 2952 | * **When to use** |
| 2953 | * |
| 2954 | * Use when you need to validate or transform values described by an external JSON Schema document. |
| 2955 | * |
| 2956 | * **Details** |
| 2957 | * |
| 2958 | * Translates a Draft 2020-12 subset using Effect schemas and built-in checks. Validation follows those checks and the |
| 2959 | * decoder's parse options, so import and re-export do not guarantee identical accepted values or a lossless round trip. |
| 2960 | * Import errors explain the unsupported constraint or reference and include its source path. |
| 2961 | * The input is assumed to be a valid Draft 2020-12 document; this function does not validate it against the meta-schema. |
| 2962 | * Instance validation semantics assume JSON-compatible JavaScript values produced by `JSON.parse`. |
| 2963 | * |
| 2964 | * **Gotchas** |
| 2965 | * |
| 2966 | * - `{ not: {} }` imports as `Never`. Other uses of `not` are unsupported. |
| 2967 | * - When `additionalProperties` is `true`, `{}`, or omitted, additional values use `Schema.Json` and are retained. |
| 2968 | * Closed objects normally strip excess properties by default; pass `onExcessProperty: "error"` to reject them. |
| 2969 | * Closed empty objects instead use `Schema.Record(Schema.String, Schema.Never)` and reject string-keyed entries |
| 2970 | * regardless of that option. |
| 2971 | * Object keyword scopes still constrain declared properties in intersections. Combinations requiring index |
| 2972 | * signatures that exclude explicit properties or patterned keys are rejected with an explanation of the limitation. |
| 2973 | * - Property count and name checks run on the decoded object, after excess properties have been stripped. |
| 2974 | * - String `minLength` and `maxLength` checks count Unicode code points, except that `minLength: 1` uses the equivalent |
| 2975 | * UTF-16 non-empty check. `integer` uses `Schema.isInt`, which requires safe integers. |
| 2976 | * - Applied patterns use `Schema.isPattern` with the `u` flag. Patterns that cannot be compiled in Unicode mode are |
| 2977 | * rejected as unsupported translations with their source path. |
| 2978 | * - `$dynamicRef`, `contains`, `dependentRequired`, `dependentSchemas`, active `if` / `then` / `else`, |
| 2979 | * `unevaluatedItems`, and `unevaluatedProperties` are rejected with an error identifying the unsupported keyword. Inactive |
| 2980 | * conditional keywords and `minContains` / `maxContains` without `contains` have no validation effect and are ignored. |
| 2981 | * - Objects and arrays used as `const` values or `enum` members are rejected. Only strings, numbers, booleans, and null |
| 2982 | * are supported. |
| 2983 | * - Intersections of overlapping unions are limited to disjoint root-type partitions and finite primitive `anyOf` |
| 2984 | * literal sets. Other union intersections, including cases that would duplicate a nested choice, are rejected. |
| 2985 | * - Unknown extension keywords are ignored and their semantics are not enforced. |
| 2986 | * - Only direct local references to top-level definitions in the form `#/$defs/<escaped-token>` are supported. Root |
| 2987 | * references, external references, and pointers below a definition are rejected with the supported reference format. |
| 2988 | * Missing definitions are reported by name. References reached inside nested schema |
| 2989 | * resources introduced by `$id` are rejected instead of resolved against the top-level definitions. |
| 2990 | * - Built-in declarations and checks are reconstructed with importer-owned revivers. |
| 2991 | * - Pattern constraints reached during translation cause an error by default. Use `patterns: "apply"` only for trusted |
| 2992 | * documents, or `patterns: "ignore"` to skip them. Ignoring patterns can both admit invalid values and reject valid |
| 2993 | * values when `oneOf` branches start overlapping. |
| 2994 | * - `onEnter` results replace the corresponding input nodes before translation. |
| 2995 | * - Callback results are used directly, and exceptions raised by a callback pass through unchanged. |
| 2996 | * |
| 2997 | * @see {@link fromJsonSchemaMultiDocument} for multiple roots sharing definitions |
| 2998 | * @see {@link toRepresentation} for converting the result to a representation document |
| 2999 | * |
| 3000 | * @category constructors |
| 3001 | * @since 4.0.0 |
| 3002 | */ |
| 3003 | export function fromJsonSchemaDocument( |
| 3004 | document: JsonSchema.Document<"draft-2020-12">, |
| 3005 | options?: FromJsonSchemaOptions |
| 3006 | ): Schema.Top { |
| 3007 | return InternalFromJsonSchemaDocument.fromJsonSchemaDocument(document, options, jsonSchemaRevivers) |
| 3008 | } |
| 3009 | |
| 3010 | /** |
| 3011 | * Imports multiple JSON Schema Draft 2020-12 roots as runtime schemas with shared definitions. |
| 3012 | * |
| 3013 | * **When to use** |
| 3014 | * |
| 3015 | * Use when multiple imported roots share reachable definitions, aliases, or recursion. |
| 3016 | * |
| 3017 | * **Details** |
| 3018 | * |
| 3019 | * Uses the same best-effort translation, built-in checks, and excess-property behavior as {@link fromJsonSchemaDocument}. |
| 3020 | * The input is assumed to be a valid Draft 2020-12 document; this function does not validate it against the meta-schema. |
| 3021 | * |
| 3022 | * **Gotchas** |
| 3023 | * |
| 3024 | * - Only definitions reachable from a root are translated. |
| 3025 | * - Unsupported standard validation and applicator keywords are rejected with an error identifying the keyword. Unknown |
| 3026 | * extension keywords are ignored and their semantics are not enforced. |
| 3027 | * - Objects and arrays used as `const` values or `enum` members are rejected. Only strings, numbers, booleans, and null |
| 3028 | * are supported. |
| 3029 | * - Intersections of overlapping unions are limited to disjoint root-type partitions and finite primitive `anyOf` |
| 3030 | * literal sets. Other union intersections, including cases that would duplicate a nested choice, are rejected. |
| 3031 | * - Only direct local references to top-level definitions in the form `#/$defs/<escaped-token>` are supported. Root |
| 3032 | * references, external references, and pointers below a definition are rejected with the supported reference format. |
| 3033 | * Missing definitions are reported by name. References reached inside nested schema |
| 3034 | * resources introduced by `$id` are rejected instead of resolved against the top-level definitions. |
| 3035 | * - Pattern constraints reached during translation cause an error by default. Use `patterns: "apply"` only for trusted |
| 3036 | * documents, or `patterns: "ignore"` to skip them. Ignoring patterns can both admit invalid values and reject valid |
| 3037 | * values when `oneOf` branches start overlapping. |
| 3038 | * - Callback results are used directly, and exceptions raised by a callback pass through unchanged. |
| 3039 | * |
| 3040 | * @see {@link fromJsonSchemaDocument} for a single root |
| 3041 | * @see {@link toRepresentations} for converting the returned schema ASTs to a representation document |
| 3042 | * |
| 3043 | * @category constructors |
| 3044 | * @since 4.0.0 |
| 3045 | */ |
| 3046 | export function fromJsonSchemaMultiDocument( |
| 3047 | document: JsonSchema.MultiDocument<"draft-2020-12">, |
| 3048 | options?: FromJsonSchemaOptions |
| 3049 | ): readonly [Schema.Top, ...Array<Schema.Top>] { |
| 3050 | return InternalFromJsonSchemaDocument.fromJsonSchemaMultiDocument(document, options, jsonSchemaRevivers) |
| 3051 | } |