Skip to content

feat: simpler validation #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed

feat: simpler validation #70

wants to merge 8 commits into from

Conversation

cyrilletuzi
Copy link
Owner

@cyrilletuzi cyrilletuzi commented Jan 17, 2019

  • Added predefined constants for basic JSON schemas to simplify validation:
    • SCHEMA_BOOLEAN (shortcut for: { type: 'boolean' })
    • SCHEMA_INTEGER (shortcut for: { type: 'integer' })
    • SCHEMA_NUMBER (shortcut for: { type: 'number' })
    • SCHEMA_STRING (shortcut for: { type: 'string' })
    • SCHEMA_ARRAY_OF_BOOLEANS (shortcut for: { items: { type: 'boolean' } })
    • SCHEMA_ARRAY_OF_INTEGER (shortcut for: { items: { type: 'integer' } })
    • SCHEMA_ARRAY_OF_NUMBER (shortcut for: { items: { type: 'number' } })
    • SCHEMA_ARRAY_OF_STRINGS (shortcut for: { items: { type: 'string' } })

This is especially useful for advanced types (arrays, objects, nested types),
to avoid TypeScript limitations.

  • For the same types as above, the result type is now automatically infered,
    meaning you don't need to cast anymore!

Before v7.5:

this.localStorage.getItem('test', { schema: { type: 'string' } })
.subscribe((result) => {
  result; // type: any :(
});

this.localStorage.getItem<string>('test', { schema: { type: 'string' } })
.subscribe((result) => {
  result; // type: string :)
});

From v7.5:

import { SCHEMA_STRING } from '@ngx-pwa/local-storage';

this.localStorage.getItem('test', { schema: SCHEMA_STRING })
.subscribe((result) => {
  result; // type: string :D
});
  • Added better interfaces for some JSON schemas:
    • JSONSchemaArrayOf<T>
    • JSONSchemaConstOf<T>
    • JSONSchemaEnumOf<T>

Why?

See documentation included in the PR.

@cyrilletuzi cyrilletuzi added this to the v7.5 milestone Jan 18, 2019
@cyrilletuzi cyrilletuzi changed the title feat: predefined constants for basic JSON schemas feat: simpler validation Jan 20, 2019
@cyrilletuzi
Copy link
Owner Author

Will wait a little for #72 to anticipate as much as possible and minimize possible breaking changes in v8.

@cyrilletuzi
Copy link
Owner Author

Closing in favor of #74

@cyrilletuzi cyrilletuzi deleted the validationconstants branch February 4, 2019 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant