Skip to content
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

feat: allow readonly arrays in JSON schema #472

Merged
merged 3 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:

test-minimum:

name: Test with Angular@10.0.0 and minimum dependencies requirements
name: Test with Angular@11.0.0 and minimum dependencies requirements
runs-on: ubuntu-latest
timeout-minutes: 5

Expand All @@ -79,7 +79,7 @@ jobs:
- name: Build the lib (with the current version, as it is what is published on npm)
run: npm run build
- name: Downgrade dependencies to minimal required version
run: npm install typescript@4.0.2 tslib@2.0.0 rxjs@6.5.3 zone.js@0.10.3 @angular/common@10.0.0 @angular/compiler@10.0.0 @angular/core@10.0.0 @angular/platform-browser@10.0.0 @angular/platform-browser-dynamic@10.0.0 @angular/router@10.0.0 @angular/cli@10.0.0 @angular/compiler-cli@10.0.0 @angular-devkit/build-angular@0.1000.0
run: npm install typescript@4.0.2 tslib@2.0.0 rxjs@6.5.3 zone.js@0.10.3 @angular/common@next @angular/compiler@next @angular/core@next @angular/platform-browser@next @angular/platform-browser-dynamic@next @angular/router@next @angular/cli@next @angular/compiler-cli@next @angular-devkit/build-angular@next
env:
CI: true
- name: Run unit tests
Expand Down
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This lib is fully documented and so you'll find detailed [migration guides](./MIGRATION.md).

## 11.0.0-0 (2020-10-19)
## 11.0.0-2 (2020-10-22)

**This is a pre-release version. Do NOT use in production.**

Expand Down Expand Up @@ -50,6 +50,19 @@ Note that in this scenario, the cast is not needed at all, it will be automatica
this.storage.get('name', { type: 'string' });
```

3. JSON schema `as const`

Given how JSON schema works, it is better to set them `as const`:

```ts
this.storage.get('name', { type: 'string' } as const);
```

But before v11, it was not possible when the JSON schema was using properties of array type
(`enum`, `items`, `required`). This is now fixed, and is a first step toward
auto-inferring the type from the JSON schema in all scenarios
((#463)[https://github.com/cyrilletuzi/angular-async-local-storage/issues/463]).

## 10.1.0 (2020-09-03)

No code change, just rebuilt with Angular 10.1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,33 @@ describe('get() API', () => {

});

it('schema as const', (done) => {

interface Test {
test: string;
}

storageService.get<Test>('test', {
type: 'object',
properties: {
test: {
type: 'string',
enum: ['hello', 'world'],
},
list: {
type: 'array',
items: [{ type: 'string' }, { type: 'number' }],
},
},
required: ['test'],
} as const).subscribe((_: Test | undefined) => {

expect().nothing();

done();

});

});

});
10 changes: 5 additions & 5 deletions projects/ngx-pwa/local-storage/src/lib/validation/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface JSONSchemaNumber {
/**
* Checks if a value is strictly equal to one of the value of enum.
*/
enum?: number[];
enum?: readonly number[];

/**
* Check if a number is a multiple of x.
Expand Down Expand Up @@ -85,7 +85,7 @@ export interface JSONSchemaInteger {
/**
* Checks if a value is strictly equal to one of the value of enum.
*/
enum?: number[];
enum?: readonly number[];

/**
* Check if a number is a multiple of x.
Expand Down Expand Up @@ -133,7 +133,7 @@ export interface JSONSchemaString {
/**
* Checks if a value is strictly equal to one of the value of enum.
*/
enum?: string[];
enum?: readonly string[];

/**
* Maxium length for a string.
Expand Down Expand Up @@ -168,7 +168,7 @@ export interface JSONSchemaArray {
/**
* Schema for the values of an array, or array of schemas for a tuple.
*/
items: JSONSchema | JSONSchema[];
items: JSONSchema | readonly JSONSchema[];

/**
* Check if an array length is lower or equal to this value.
Expand Down Expand Up @@ -248,7 +248,7 @@ export interface JSONSchemaObject {
* Array of names of the required properties for an object.
* Properties set as required should be present in `properties` too.
*/
required?: string[];
required?: readonly string[];

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ export class JSONValidator {
/* Validate all the values in array */
for (const value of data) {

if (!this.validate(value, schema.items)) {
// TODO: remove when TypeScript 4.1 is available
// (currently the narrowed type from `Array.isArray()` is lost on readonly arrays)
if (!this.validate(value, schema.items as JSONSchema)) {
return false;
}

Expand Down