Skip to content

Validate Map object in v6 #91

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

You must be logged in to vote

Thanks for your interest for this lib.

The JSON schema standard can only describe JSON, ie. literal objects. So for instances of specific classes, like Map, you'll need to do your own validation, for example:

this.localStorage.getItem(key).subscribe((result) => {
  if (result instanceof Map) {}
});

Beware that in some special cases (private mode for example), the lib will fallback to localStorage, where data will be serialized. But a JavaScript Map is not serializable to JSON. To be exact, seriazalization works (JSON.stringify()), but when unserializing (JSON.parse()), you won't get a real Map.

For now, a safer way to store a Map would be this:

const index = 'map';
const someMap = new Map<s…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by cyrilletuzi
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
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
Converted from issue

This discussion was converted from issue #91 on December 08, 2020 21:25.