(
self: Schema<A, I, R>,
options?: {
readonly exact?: true
readonly default?: () => A
readonly nullable?: true
readonly as?: "Option"
readonly onNoneEncoding?: () => option_.Option<never>
}
)
| 2405 | } |
| 2406 | |
| 2407 | const optionalPropertySignatureAST = <A, I, R>( |
| 2408 | self: Schema<A, I, R>, |
| 2409 | options?: { |
| 2410 | readonly exact?: true |
| 2411 | readonly default?: () => A |
| 2412 | readonly nullable?: true |
| 2413 | readonly as?: "Option" |
| 2414 | readonly onNoneEncoding?: () => option_.Option<never> |
| 2415 | } |
| 2416 | ): PropertySignature.AST => { |
| 2417 | const isExact = options?.exact |
| 2418 | const defaultValue = options?.default |
| 2419 | const isNullable = options?.nullable |
| 2420 | const asOption = options?.as == "Option" |
| 2421 | const asOptionEncode = options?.onNoneEncoding ? option_.orElse(options.onNoneEncoding) : identity |
| 2422 | |
| 2423 | if (isExact) { |
| 2424 | if (defaultValue) { |
| 2425 | if (isNullable) { |
| 2426 | return withConstructorDefault( |
| 2427 | optionalToRequired( |
| 2428 | NullOr(self), |
| 2429 | typeSchema(self), |
| 2430 | { |
| 2431 | decode: option_.match({ onNone: defaultValue, onSome: (a) => a === null ? defaultValue() : a }), |
| 2432 | encode: option_.some |
| 2433 | } |
| 2434 | ), |
| 2435 | defaultValue |
| 2436 | ).ast |
| 2437 | } else { |
| 2438 | return withConstructorDefault( |
| 2439 | optionalToRequired( |
| 2440 | self, |
| 2441 | typeSchema(self), |
| 2442 | { decode: option_.match({ onNone: defaultValue, onSome: identity }), encode: option_.some } |
| 2443 | ), |
| 2444 | defaultValue |
| 2445 | ).ast |
| 2446 | } |
| 2447 | } else if (asOption) { |
| 2448 | const to = OptionFromSelf_(typeSchema(self)) |
| 2449 | if (isNullable) { |
| 2450 | return optionalToRequired( |
| 2451 | NullOr(self), |
| 2452 | to, |
| 2453 | { |
| 2454 | decode: option_.filter(Predicate.isNotNull<A | null>), |
| 2455 | encode: asOptionEncode |
| 2456 | } |
| 2457 | ).ast |
| 2458 | } else { |
| 2459 | return optionalToRequired( |
| 2460 | self, |
| 2461 | to, |
| 2462 | { decode: identity, encode: identity } |
| 2463 | ).ast |
| 2464 | } |
no test coverage detected