core: validator: support 'convert' as default action

fix #505, close #506
This commit is contained in:
undefined committed 2023-02-05 13:07:46 +08:00
1 parent 02c0d7aaa8
commit 1124ed9506
7 files changed
+11 -9

No files matched your search

+2 -2
View File
@@ -8,7 +8,7 @@ import saslprep from 'saslprep';
type InputType = string | number | Record<string, any> | any[];
export type Converter<T> = (value: any) => T;
export type Validator<Loose extends boolean = true> = (value: Loose extends true ? any : InputType) => boolean;
export type Type<T> = readonly [Converter<T>, Validator<false>?, boolean?];
export type Type<T> = readonly [Converter<T>, Validator<false>?, (boolean | 'convert')?];
export interface Types {
// String outputs
@@ -95,7 +95,7 @@ export const Types: Types = {
Float: [(v) => +v, (v) => Number.isFinite(+v)],
ObjectID: [(v) => new ObjectID(v), ObjectID.isValid],
Boolean: [(v) => v && !['false', 'off'].includes(v), null, true],
Boolean: [(v) => !!(v && !['false', 'off'].includes(v)), null, 'convert'],
Date: [
(v) => {
const d = v.split('-');