Skip to content

Cleaning on a per schema level? #809

Answered by sinclairzx81
m1212e asked this question in Q&A
Discussion options

You must be logged in to vote

@m1212e Hi

I'm not sure there's much I can do to make TypeBox optionally Clean internally (not without depending on library specific schema keywords, which I'd like to avoid), but you can implement this functionality externally.

import { Type, TSchema } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

export function Clean(schema: TSchema, value: unknown): unknown {
  return schema.cleanAdditionalFields === true ? Value.Clean(schema, value) : value
}

const A = Type.Object({
  x: Type.Number(),
  y: Type.Number(),
  z: Type.Number()
}, {
  cleanAdditionalFields: true
})

const B = Type.Object({
  x: Type.Number(),
  y: Type.Number(),
  z: Type.Number()
})

console.l…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@m1212e
Comment options

Answer selected by m1212e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants