|
|
|
|
@ -8,9 +8,9 @@ import type { Schema } from './types';
|
|
|
|
|
* - `encode(undefined)` returns `undefined` directly.
|
|
|
|
|
* - `encode(value)` delegates to the inner schema.
|
|
|
|
|
*
|
|
|
|
|
* Sync APIs inherit the async contract of the wrapped schema. If the inner
|
|
|
|
|
* schema is async, `decodeSync` / `validateSync` throw `SiumAsyncSchemaError`
|
|
|
|
|
* even when the input is `undefined` and could be short-circuited.
|
|
|
|
|
* Sync APIs preserve the sentinel short-circuit even when the inner schema is
|
|
|
|
|
* async. `decodeSync(undefined)` returns `undefined`; defined values delegate
|
|
|
|
|
* to the inner schema and throw `SiumAsyncSchemaError` if that path is async.
|
|
|
|
|
*/
|
|
|
|
|
export function optional<I, O>(schema: Schema<I, O>): Schema<I | undefined, O | undefined> {
|
|
|
|
|
const innerIsAsync = schema['~sium'].async;
|
|
|
|
|
@ -32,6 +32,13 @@ export function optional<I, O>(schema: Schema<I, O>): Schema<I | undefined, O |
|
|
|
|
|
|
|
|
|
|
return innerIsAsync ? schema.decode(input, ctx) : schema.decodeSync(input, ctx);
|
|
|
|
|
},
|
|
|
|
|
decodeSyncImpl: (input, ctx) => {
|
|
|
|
|
if (input === undefined) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return schema.decodeSync(input, ctx);
|
|
|
|
|
},
|
|
|
|
|
encode: (value) => {
|
|
|
|
|
if (value === undefined) {
|
|
|
|
|
return undefined;
|
|
|
|
|
@ -49,9 +56,9 @@ export function optional<I, O>(schema: Schema<I, O>): Schema<I | undefined, O |
|
|
|
|
|
* - `encode(null)` returns `null` directly.
|
|
|
|
|
* - `encode(value)` delegates to the inner schema.
|
|
|
|
|
*
|
|
|
|
|
* Sync APIs inherit the async contract of the wrapped schema. If the inner
|
|
|
|
|
* schema is async, `decodeSync` / `validateSync` throw `SiumAsyncSchemaError`
|
|
|
|
|
* before any null short-circuit happens.
|
|
|
|
|
* Sync APIs preserve the sentinel short-circuit even when the inner schema is
|
|
|
|
|
* async. `decodeSync(null)` returns `null`; non-null values delegate to the
|
|
|
|
|
* inner schema and throw `SiumAsyncSchemaError` if that path is async.
|
|
|
|
|
*/
|
|
|
|
|
export function nullable<I, O>(schema: Schema<I, O>): Schema<I | null, O | null> {
|
|
|
|
|
const innerIsAsync = schema['~sium'].async;
|
|
|
|
|
@ -73,6 +80,13 @@ export function nullable<I, O>(schema: Schema<I, O>): Schema<I | null, O | null>
|
|
|
|
|
|
|
|
|
|
return innerIsAsync ? schema.decode(input, ctx) : schema.decodeSync(input, ctx);
|
|
|
|
|
},
|
|
|
|
|
decodeSyncImpl: (input, ctx) => {
|
|
|
|
|
if (input === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return schema.decodeSync(input, ctx);
|
|
|
|
|
},
|
|
|
|
|
encode: (value) => {
|
|
|
|
|
if (value === null) {
|
|
|
|
|
return null;
|
|
|
|
|
@ -92,12 +106,14 @@ export function nullable<I, O>(schema: Schema<I, O>): Schema<I | null, O | null>
|
|
|
|
|
* - `encode(value)` delegates directly to the inner schema and never
|
|
|
|
|
* re-injects the default.
|
|
|
|
|
*
|
|
|
|
|
* Sync APIs inherit the async contract of the wrapped schema. If the inner
|
|
|
|
|
* schema is async, `decodeSync` / `validateSync` throw `SiumAsyncSchemaError`
|
|
|
|
|
* even when the input is `undefined` and the default could be returned
|
|
|
|
|
* without consulting the inner decoder.
|
|
|
|
|
* Sync APIs preserve the default short-circuit even when the inner schema is
|
|
|
|
|
* async. `decodeSync(undefined)` returns `getDefault()`; defined values delegate
|
|
|
|
|
* to the inner schema and throw `SiumAsyncSchemaError` if that path is async.
|
|
|
|
|
*/
|
|
|
|
|
export function defaulted<I, O>(schema: Schema<I, O>, getDefault: () => O): Schema<I | undefined, O> {
|
|
|
|
|
export function defaulted<I, O>(
|
|
|
|
|
schema: Schema<I, O>,
|
|
|
|
|
getDefault: () => O
|
|
|
|
|
): Schema<I | undefined, O> {
|
|
|
|
|
const innerIsAsync = schema['~sium'].async;
|
|
|
|
|
|
|
|
|
|
return createSchema({
|
|
|
|
|
@ -118,6 +134,13 @@ export function defaulted<I, O>(schema: Schema<I, O>, getDefault: () => O): Sche
|
|
|
|
|
|
|
|
|
|
return innerIsAsync ? schema.decode(input, ctx) : schema.decodeSync(input, ctx);
|
|
|
|
|
},
|
|
|
|
|
decodeSyncImpl: (input, ctx) => {
|
|
|
|
|
if (input === undefined) {
|
|
|
|
|
return getDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return schema.decodeSync(input, ctx);
|
|
|
|
|
},
|
|
|
|
|
encode: (value) => schema.encode(value)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|