@verdant-web/common 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/schema/defaults.test.js +16 -3
- package/dist/cjs/schema/defaults.test.js.map +1 -1
- package/dist/cjs/schema/fieldHelpers.d.ts +35 -0
- package/dist/cjs/schema/fieldHelpers.js +38 -0
- package/dist/cjs/schema/fieldHelpers.js.map +1 -0
- package/dist/cjs/schema/fields.js +13 -2
- package/dist/cjs/schema/fields.js.map +1 -1
- package/dist/cjs/schema/index.d.ts +44 -0
- package/dist/cjs/schema/index.js +10 -0
- package/dist/cjs/schema/index.js.map +1 -1
- package/dist/cjs/schema/types/compounds.d.ts +1 -1
- package/dist/cjs/schema/types/fields.d.ts +9 -8
- package/dist/cjs/schema/types/shapes.d.ts +5 -2
- package/dist/cjs/schema/validation.js +7 -0
- package/dist/cjs/schema/validation.js.map +1 -1
- package/dist/esm/schema/defaults.test.js +16 -3
- package/dist/esm/schema/defaults.test.js.map +1 -1
- package/dist/esm/schema/fieldHelpers.d.ts +35 -0
- package/dist/esm/schema/fieldHelpers.js +35 -0
- package/dist/esm/schema/fieldHelpers.js.map +1 -0
- package/dist/esm/schema/fields.js +13 -2
- package/dist/esm/schema/fields.js.map +1 -1
- package/dist/esm/schema/index.d.ts +44 -0
- package/dist/esm/schema/index.js +7 -0
- package/dist/esm/schema/index.js.map +1 -1
- package/dist/esm/schema/types/compounds.d.ts +1 -1
- package/dist/esm/schema/types/fields.d.ts +9 -8
- package/dist/esm/schema/types/shapes.d.ts +5 -2
- package/dist/esm/schema/validation.js +7 -0
- package/dist/esm/schema/validation.js.map +1 -1
- package/dist/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/schema/defaults.test.ts +18 -4
- package/src/schema/fieldHelpers.ts +106 -0
- package/src/schema/fields.ts +23 -3
- package/src/schema/index.ts +7 -0
- package/src/schema/types/compounds.ts +2 -2
- package/src/schema/types/fields.ts +15 -13
- package/src/schema/types/shapes.ts +9 -5
- package/src/schema/validation.ts +13 -2
|
@@ -21,19 +21,21 @@ export type StorageBooleanFieldSchema = {
|
|
|
21
21
|
/** @deprecated - add an index to indexes with this field name. */
|
|
22
22
|
indexed?: boolean;
|
|
23
23
|
};
|
|
24
|
-
export type StorageArrayFieldSchema = {
|
|
24
|
+
export type StorageArrayFieldSchema<TItems extends NestedStorageFieldSchema> = {
|
|
25
25
|
type: 'array';
|
|
26
|
-
items:
|
|
26
|
+
items: TItems;
|
|
27
27
|
nullable?: boolean;
|
|
28
28
|
};
|
|
29
|
-
export type StorageObjectFieldSchema =
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
export type StorageObjectFieldSchema<Props extends NestedStorageFieldsSchema> =
|
|
30
|
+
{
|
|
31
|
+
type: 'object';
|
|
32
|
+
properties: Props;
|
|
33
|
+
nullable?: boolean;
|
|
34
|
+
default?: Record<string, any> | (() => Record<string, any>);
|
|
35
|
+
};
|
|
36
|
+
export type StorageAnyFieldSchema<TShape = any> = {
|
|
35
37
|
type: 'any';
|
|
36
|
-
default?:
|
|
38
|
+
default?: TShape;
|
|
37
39
|
};
|
|
38
40
|
export type StorageMapFieldSchema<V extends NestedStorageFieldSchema> = {
|
|
39
41
|
type: 'map';
|
|
@@ -54,8 +56,8 @@ export type StorageFieldSchema =
|
|
|
54
56
|
| StorageStringFieldSchema
|
|
55
57
|
| StorageNumberFieldSchema
|
|
56
58
|
| StorageBooleanFieldSchema
|
|
57
|
-
| StorageArrayFieldSchema
|
|
58
|
-
| StorageObjectFieldSchema
|
|
59
|
+
| StorageArrayFieldSchema<any>
|
|
60
|
+
| StorageObjectFieldSchema<any>
|
|
59
61
|
| StorageAnyFieldSchema
|
|
60
62
|
| StorageMapFieldSchema<any>
|
|
61
63
|
| StorageFileFieldSchema;
|
|
@@ -81,8 +83,8 @@ export type NestedStorageFieldSchema =
|
|
|
81
83
|
| NestedStorageStringFieldSchema
|
|
82
84
|
| NestedStorageNumberFieldSchema
|
|
83
85
|
| NestedStorageBooleanFieldSchema
|
|
84
|
-
| StorageArrayFieldSchema
|
|
85
|
-
| StorageObjectFieldSchema
|
|
86
|
+
| StorageArrayFieldSchema<any>
|
|
87
|
+
| StorageObjectFieldSchema<any>
|
|
86
88
|
| StorageAnyFieldSchema
|
|
87
89
|
| StorageMapFieldSchema<any>
|
|
88
90
|
| StorageFileFieldSchema;
|
|
@@ -25,10 +25,10 @@ export type BaseShapeFromProperty<T extends StorageFieldSchema> =
|
|
|
25
25
|
? number
|
|
26
26
|
: T['type'] extends 'boolean'
|
|
27
27
|
? boolean
|
|
28
|
-
: T extends StorageArrayFieldSchema
|
|
29
|
-
? ShapeFromProperty<
|
|
30
|
-
: T extends StorageObjectFieldSchema
|
|
31
|
-
? ShapeFromFields<
|
|
28
|
+
: T extends StorageArrayFieldSchema<infer U>
|
|
29
|
+
? ShapeFromProperty<U>[]
|
|
30
|
+
: T extends StorageObjectFieldSchema<infer U>
|
|
31
|
+
? ShapeFromFields<U>
|
|
32
32
|
: T extends StorageMapFieldSchema<any>
|
|
33
33
|
? Record<string, ShapeFromProperty<T['values']>>
|
|
34
34
|
: T['type'] extends 'any'
|
|
@@ -65,7 +65,7 @@ type StoragePropertyIsOptional<T extends StorageFieldSchema> =
|
|
|
65
65
|
? true
|
|
66
66
|
: false;
|
|
67
67
|
|
|
68
|
-
type ShapeFromFieldsWithDefaults<
|
|
68
|
+
export type ShapeFromFieldsWithDefaults<
|
|
69
69
|
T extends StorageFieldsSchema | NestedStorageFieldsSchema,
|
|
70
70
|
> = {
|
|
71
71
|
[K in keyof T as StoragePropertyIsOptional<T[K]> extends true
|
|
@@ -80,3 +80,7 @@ type ShapeFromFieldsWithDefaults<
|
|
|
80
80
|
export type StorageDocumentInit<
|
|
81
81
|
Collection extends StorageCollectionSchema<any, any, any>,
|
|
82
82
|
> = ShapeFromFieldsWithDefaults<Collection['fields']>;
|
|
83
|
+
|
|
84
|
+
export type FieldsFromShape<Shape extends Record<string, unknown>> = {
|
|
85
|
+
[K in keyof Shape]: StorageFieldSchema;
|
|
86
|
+
};
|
package/src/schema/validation.ts
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
+
import { OID_KEY } from '../oids.js';
|
|
1
2
|
import { isObject } from '../utils.js';
|
|
2
3
|
import { hasDefault, isNullable } from './fields.js';
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
NestedStorageFieldsSchema,
|
|
6
|
+
StorageFieldSchema,
|
|
7
|
+
StorageFieldsSchema,
|
|
8
|
+
} from './types.js';
|
|
4
9
|
|
|
5
10
|
export function validateEntity(
|
|
6
11
|
schema: StorageFieldsSchema,
|
|
7
12
|
entity: any,
|
|
8
13
|
): EntityValidationProblem | void {
|
|
9
14
|
for (const [key, value] of Object.entries(entity)) {
|
|
15
|
+
// legacy -- old objects sometimes accidentally include this key
|
|
16
|
+
if (key === OID_KEY) continue;
|
|
10
17
|
if (!schema[key]) {
|
|
11
18
|
return {
|
|
12
19
|
type: 'invalid-key',
|
|
@@ -73,7 +80,11 @@ export function validateEntityField({
|
|
|
73
80
|
} for field ${formatField(fieldPath)}, got ${value}`,
|
|
74
81
|
};
|
|
75
82
|
}
|
|
76
|
-
for (const [key, subField] of Object.entries(
|
|
83
|
+
for (const [key, subField] of Object.entries(
|
|
84
|
+
field.properties as NestedStorageFieldsSchema,
|
|
85
|
+
)) {
|
|
86
|
+
// legacy -- old objects sometimes accidentally include this key
|
|
87
|
+
if (key === OID_KEY) continue;
|
|
77
88
|
if (value[key]) {
|
|
78
89
|
validateEntityField({
|
|
79
90
|
field: subField,
|