schema/src/items/boolean.ts
Florian Bouillon 817b7d6774
0.0.2
Signed-off-by: Avior <git@avior.me>
2025-03-25 13:54:46 +01:00

31 lines
699 B
TypeScript

import { parceable } from '../Schema'
import SchemaItem from '../SchemaItem'
export default class SchemaBoolean extends SchemaItem<boolean> {
/**
* @param [trueValue='true'] the truhty value (default to `'true'`)
* @param [falseValue='false'] the falthy value (default to `'false'`)
*/
@parceable()
public parseString(trueValue = 'true', falseValue = 'false'): this {
this.addPreProcess((it) => {
if (typeof it !== 'string') {
return it
}
if (it === trueValue) {
return true
}
if (it === falseValue) {
return false
}
return it
})
return this
}
public override isOfType(input: unknown): input is boolean {
return typeof input === 'boolean'
}
}