| 284 | // what a module reads of a validator it imports, which a build reading one |
| 285 | // module at a time bakes there, as the validator's own module can't see it |
| 286 | export const bakeInto = <t>( |
| 287 | validator: t, |
| 288 | { "~standard": standard, ...attributes }: Omit<ValidatorAttributes, "meta"> |
| 289 | ): t => { |
| 290 | // a Type arktype created, as where a site is left to it, has each member |
| 291 | if (validator instanceof Validator && Object.isExtensible(validator)) { |
| 292 | const prior: BakedJsonSchema | undefined = Reflect.getOwnPropertyDescriptor( |
| 293 | validator, |
| 294 | "toJsonSchema" |
| 295 | )?.value |
| 296 | const baked = attributes.toJsonSchema as BakedJsonSchema | undefined |
| 297 | // another module's bake may answer a target this one doesn't, and |
| 298 | // writes what this one does as it would |
| 299 | if (prior && baked) { |
| 300 | attributes.toJsonSchema = freeze((opts?: JsonSchemaOptions) => { |
| 301 | try { |
| 302 | return baked(opts) |
| 303 | } catch (e) { |
| 304 | if (e instanceof Error && e.message === bakedJsonSchemaOptionsMessage) |
| 305 | return prior(opts) |
| 306 | throw e |
| 307 | } |
| 308 | }) |
| 309 | } |
| 310 | // another module's bake of any other member is what this one's would |
| 311 | // be, and code may already hold it |
| 312 | for (const member in attributes) { |
| 313 | if ( |
| 314 | member !== "toJsonSchema" && |
| 315 | Reflect.getOwnPropertyDescriptor(validator, member) |
| 316 | ) |
| 317 | delete attributes[member as keyof typeof attributes] |
| 318 | } |
| 319 | own(validator, attributes) |
| 320 | if ( |
| 321 | standard && |
| 322 | !Reflect.getOwnPropertyDescriptor(validator["~standard"], "jsonSchema") |
| 323 | ?.value |
| 324 | ) |
| 325 | own(validator["~standard"], standard) |
| 326 | } |
| 327 | return validator |
| 328 | } |
| 329 | |
| 330 | type StandardFailure = StandardSchemaV1.FailureResult & { |
| 331 | _s: (json?: true) => unknown |