Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

how to handle optional properties correctly?#572

Answeredbycyrilletuzi
eliprand asked this question inQ&A
Discussion options

I am storing an object I receive from another API/Library. One of the properties is optional. When passed toStorageMap.set() it stores it with anundefined value. I currently do not enforce the schema on.set(). But when reading it back, i keeps complaining about invalid schema.

Here is my schema:

exportconstDISPLAY_USER_INFO_JSON_SCHEMA:JSONSchema={type:'object',properties:{firstName:{type:'string'},lastName:{type:'string'},name:{type:'string'},partner:{type:'string'},passwordExpiration:{type:'string'},username:{type:'string'},},required:['name','partner','username'],// i originally did not have that, but that did not help};

and here is my data from IndexDB:

firstName:"Eric"lastName:"Liprandi"name:"Eric Liprandi"partner:"snapfish"passwordExpiration:undefinedusername:"ericl"

I tried returningnull from the API/library, but same result.
The only fix I have right now is to pass'' (the empty string). It's not ideal, but I can live with i.

How can I handle this situation directly with the library?

You must be logged in to vote

Hi,

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

constuser={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:

constuser={firstName:"Eric",lastName:"Liprandi",name:"Eric Liprandi",partner:"snapfish",passwordExpiration:undefined,username:"ericl"};if(user.passwordExpiration===undefined){deleteuser.passwordExpiration;}

Given that in TypeScriptpasswordExpiration?: string is equivalent topasswordExpiration: string | undefined, it would make sense to allow explicitu…

Replies: 1 comment 1 reply

Comment options

Hi,

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

constuser={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:

constuser={firstName:"Eric",lastName:"Liprandi",name:"Eric Liprandi",partner:"snapfish",passwordExpiration:undefined,username:"ericl"};if(user.passwordExpiration===undefined){deleteuser.passwordExpiration;}

Given that in TypeScriptpasswordExpiration?: string is equivalent topasswordExpiration: string | undefined, it would make sense to allow explicitundefined too.

But while testing this solution, it appeared that the current lib behavior is preventing another issue: in some scenarios, the lib is forced to fallback tolocalStorage, which needs data serialization withJSON.stringify(), which will removeundefineds (as it doesn't exist in JSON).

So it means you would not get the exact same value when reading back (asundefined properties would have been stripped by serialization), which would be an issue in some scenarios (for example in JasminetoEqual() tests, which would fail).

Also, more generally,undefined /null can cause a lot of issues in client-side storage APIs. That's why for example when doingthis.storage.set('key', undefined) the lib will delete the key instead of trying to storeundefined.

So I'm not sure yet how and even if we should fix/add this feature. It needs further thinking.

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

Merci pour la réponse et pour pour le package ;)

Answer selected bycyrilletuzi
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@eliprand@cyrilletuzi

[8]ページ先頭

©2009-2025 Movatter.jp