Skip to content

Casting empty strings to null #935

Closed Answered by EvHaus
EvHaus asked this question in Q&A
Discussion options

You must be logged in to vote

I think I found my answer -- Type.Transform. I changed my schema to:

const schema = Type.Object({
  myField: Type.Transform(
    Type.Union([t.Null(), t.String()])
  )
  .Decode((value) => {
    if (value === '') return null;
    return value;
  })
  .Encode((value) => {
    if (value === null) return '';
    return value;
  })
);

And now it works.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by EvHaus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant