Skip to content

Commit 6336f5d

Browse files
authored
Merge pull request #472 from cyrilletuzi/readonly
feat: allow readonly arrays in JSON schema
2 parents 6bc0239 + 50b6656 commit 6336f5d

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262

6363
test-minimum:
6464

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

@@ -79,7 +79,7 @@ jobs:
7979
- name: Build the lib (with the current version, as it is what is published on npm)
8080
run: npm run build
8181
- name: Downgrade dependencies to minimal required version
82-
run: npm install [email protected] [email protected] [email protected] [email protected] @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
82+
run: npm install [email protected] [email protected] [email protected] [email protected] @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
8383
env:
8484
CI: true
8585
- name: Run unit tests

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
## 11.0.0-0 (2020-10-19)
5+
## 11.0.0-2 (2020-10-22)
66

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

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

53+
3. JSON schema `as const`
54+
55+
Given how JSON schema works, it is better to set them `as const`:
56+
57+
```ts
58+
this.storage.get('name', { type: 'string' } as const);
59+
```
60+
61+
But before v11, it was not possible when the JSON schema was using properties of array type
62+
(`enum`, `items`, `required`). This is now fixed, and is a first step toward
63+
auto-inferring the type from the JSON schema in all scenarios
64+
((#463)[https://github.com/cyrilletuzi/angular-async-local-storage/issues/463]).
65+
5366
## 10.1.0 (2020-09-03)
5467

5568
No code change, just rebuilt with Angular 10.1.

projects/ngx-pwa/local-storage/src/lib/storages/get-overloads.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,33 @@ describe('get() API', () => {
477477

478478
});
479479

480+
it('schema as const', (done) => {
481+
482+
interface Test {
483+
test: string;
484+
}
485+
486+
storageService.get<Test>('test', {
487+
type: 'object',
488+
properties: {
489+
test: {
490+
type: 'string',
491+
enum: ['hello', 'world'],
492+
},
493+
list: {
494+
type: 'array',
495+
items: [{ type: 'string' }, { type: 'number' }],
496+
},
497+
},
498+
required: ['test'],
499+
} as const).subscribe((_: Test | undefined) => {
500+
501+
expect().nothing();
502+
503+
done();
504+
505+
});
506+
507+
});
508+
480509
});

projects/ngx-pwa/local-storage/src/lib/validation/json-schema.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface JSONSchemaNumber {
3737
/**
3838
* Checks if a value is strictly equal to one of the value of enum.
3939
*/
40-
enum?: number[];
40+
enum?: readonly number[];
4141

4242
/**
4343
* Check if a number is a multiple of x.
@@ -85,7 +85,7 @@ export interface JSONSchemaInteger {
8585
/**
8686
* Checks if a value is strictly equal to one of the value of enum.
8787
*/
88-
enum?: number[];
88+
enum?: readonly number[];
8989

9090
/**
9191
* Check if a number is a multiple of x.
@@ -133,7 +133,7 @@ export interface JSONSchemaString {
133133
/**
134134
* Checks if a value is strictly equal to one of the value of enum.
135135
*/
136-
enum?: string[];
136+
enum?: readonly string[];
137137

138138
/**
139139
* Maxium length for a string.
@@ -168,7 +168,7 @@ export interface JSONSchemaArray {
168168
/**
169169
* Schema for the values of an array, or array of schemas for a tuple.
170170
*/
171-
items: JSONSchema | JSONSchema[];
171+
items: JSONSchema | readonly JSONSchema[];
172172

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

253253
}
254254

projects/ngx-pwa/local-storage/src/lib/validation/json-validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ export class JSONValidator {
196196
/* Validate all the values in array */
197197
for (const value of data) {
198198

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

0 commit comments

Comments
 (0)