Skip to content

how to handle optional properties correctly? #572

Answered by cyrilletuzi
eliprand asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,

Currently, the JSON validator in the lib expects optional object properties to be inexistant:

const user = {
  firstName: "Eric",
  lastName: "Liprandi",
  name: "Eric Liprandi",
  partner: "snapfish",
  username: "ericl"
};

If it's not possible to directly get that from your other API, you can do:

const user = {
  firstName: "Eric",
  lastName: "Liprandi",
  name: "Eric Liprandi",
  partner: "snapfish",
  passwordExpiration: undefined,
  username: "ericl"
};

if (user.passwordExpiration === undefined) {
  delete user.passwordExpiration;
}

Given that in TypeScript passwordExpiration?: string is equivalent to passwordExpiration: string | undefined, it would make sense to allow explicit u…

Replies: 1 comment 1 reply

Comment options

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

Answer selected by cyrilletuzi
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