Skip to content

Commit

Permalink
feat(validator): add literal primitive transform
Browse files Browse the repository at this point in the history
  • Loading branch information
NWYLZW committed Feb 29, 2024
1 parent 94049d1 commit d7b89d4
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/validator/src/types/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import type { t as tn } from '@typp/core'
import { FALSY } from '../base'
import type { SwitchBaseType } from '../base.inner'
import { preprocess } from '../utils.inner'
import { bigintTransform } from './primitive.bigint'
import { booleanTransform } from './primitive.boolean'
import { numberTransform } from './primitive.number'
import { stringTransform } from './primitive.string'
import { symbolTransform } from './primitive.symbol'

declare module '@typp/core' {
namespace t {
Expand Down Expand Up @@ -57,11 +62,26 @@ export function literalValidator(t: typeof tn) {
transform: input => FALSY.includes(input) ? undefined : input
})
t.useValidator((s): s is tn.Schema<
string | number | bigint | symbol | boolean,
string | number | bigint | symbol | boolean
> => typeof s.shape !== 'object', {
bigint | boolean | number | string | symbol,
bigint | boolean | number | string | symbol
> => [
'bigint',
'boolean',
'number',
'string',
'symbol'
].includes(typeof s.shape), {
validate(input) {
return input === this.shape
},
transform(input, options) {
return (<Record<string, Function>>{
bigint: bigintTransform,
boolean: booleanTransform,
number: numberTransform,
string: stringTransform,
symbol: symbolTransform
})[typeof this.shape as string]?.call(this as any, input, options)
}
})
}

0 comments on commit d7b89d4

Please sign in to comment.