feat: add falthyAsNull to nullable

This commit is contained in:
Florian Bouillon 2025-04-02 13:52:18 +02:00
parent 2f48637e28
commit 7e176f1fb7
Signed by: Florian Bouillon
GPG Key ID: 7676FF78F3BC40EC

View File

@ -1,9 +1,17 @@
import { parceable } from 'Schema'
import SchemaItem from '../SchemaItem' import SchemaItem from '../SchemaItem'
import { SchemaInfer, ValidationResult } from '../types' import { SchemaInfer, ValidationResult } from '../types'
export default class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> { export default class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
public constructor(public readonly child: Type) { super([child]) } public constructor(public readonly child: Type) { super([child]) }
@parceable()
public falthyAsNull() {
this.addPreProcess((it) => !it ? undefined : it)
return this
}
public unwrap(): Type { public unwrap(): Type {
return this.child return this.child
} }