@feathersjs/typebox 5.0.0-pre.33 → 5.0.0-pre.34
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/CHANGELOG.md +7 -0
- package/lib/default-schemas.d.ts +1 -1
- package/lib/index.d.ts +73 -35
- package/lib/index.js +65 -41
- package/lib/index.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +88 -46
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.0.0-pre.34](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.33...v5.0.0-pre.34) (2022-12-14)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **schema:** Allow query schemas with no properties, error on unsupported types ([#2904](https://github.com/feathersjs/feathers/issues/2904)) ([b66c734](https://github.com/feathersjs/feathers/commit/b66c734357478f51b2d38fa7f3eee08640cea26e))
|
|
11
|
+
- **typebox:** Improve query syntax defaults ([#2888](https://github.com/feathersjs/feathers/issues/2888)) ([59f3cdc](https://github.com/feathersjs/feathers/commit/59f3cdca6376e34fe39a7b91db837d0325aeb5db))
|
|
12
|
+
|
|
6
13
|
# [5.0.0-pre.33](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.32...v5.0.0-pre.33) (2022-11-08)
|
|
7
14
|
|
|
8
15
|
### Features
|
package/lib/default-schemas.d.ts
CHANGED
|
@@ -85,4 +85,4 @@ export declare const defaultAppConfiguration: import("@sinclair/typebox").TObjec
|
|
|
85
85
|
connection: import("@sinclair/typebox").TString<string>;
|
|
86
86
|
}>>;
|
|
87
87
|
}>;
|
|
88
|
-
export
|
|
88
|
+
export type DefaultAppConfiguration = Static<typeof defaultAppConfiguration>;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { TObject, TInteger, TOptional, TSchema, TIntersect } from '@sinclair/typebox';
|
|
1
|
+
import { TObject, TInteger, TOptional, TSchema, TIntersect, ObjectOptions } from '@sinclair/typebox';
|
|
2
2
|
import { Validator, DataValidatorMap, Ajv } from '@feathersjs/schema';
|
|
3
3
|
export * from '@sinclair/typebox';
|
|
4
4
|
export * from './default-schemas';
|
|
5
|
-
export
|
|
5
|
+
export type TDataSchemaMap = {
|
|
6
6
|
create: TObject;
|
|
7
7
|
update?: TObject;
|
|
8
8
|
patch?: TObject;
|
|
@@ -32,43 +32,81 @@ export declare const getDataValidator: (def: TObject | TDataSchemaMap, validator
|
|
|
32
32
|
* @returns TypeBox.Type
|
|
33
33
|
*/
|
|
34
34
|
export declare function StringEnum<T extends string[]>(allowedValues: [...T]): import("@sinclair/typebox").TUnsafe<T[number]>;
|
|
35
|
+
/**
|
|
36
|
+
* Creates the `$sort` Feathers query syntax schema for an object schema
|
|
37
|
+
*
|
|
38
|
+
* @param schema The TypeBox object schema
|
|
39
|
+
* @returns The `$sort` syntax schema
|
|
40
|
+
*/
|
|
35
41
|
export declare function sortDefinition<T extends TObject>(schema: T): TObject<T["properties"] extends infer T_1 ? { [K in keyof T_1]: TOptional<TInteger>; } : never>;
|
|
36
|
-
export declare const queryProperty: <T extends TSchema>(def: T) => TOptional<import("@sinclair/typebox").TUnion<[T, TObject<{
|
|
37
|
-
$gt: TOptional<T>;
|
|
38
|
-
$gte: TOptional<T>;
|
|
39
|
-
$lt: TOptional<T>;
|
|
40
|
-
$lte: TOptional<T>;
|
|
41
|
-
$ne: TOptional<T>;
|
|
42
|
-
$in: TOptional<import("@sinclair/typebox").TArray<T>>;
|
|
43
|
-
$nin: TOptional<import("@sinclair/typebox").TArray<T>>;
|
|
44
|
-
}>]>>;
|
|
45
|
-
export declare const queryProperties: <T extends TObject<import("@sinclair/typebox").TProperties>>(type: T) => TObject<T["properties"] extends infer T_1 ? { [K in keyof T_1]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K], TObject<{
|
|
46
|
-
$gt: TOptional<T["properties"][K]>;
|
|
47
|
-
$gte: TOptional<T["properties"][K]>;
|
|
48
|
-
$lt: TOptional<T["properties"][K]>;
|
|
49
|
-
$lte: TOptional<T["properties"][K]>;
|
|
50
|
-
$ne: TOptional<T["properties"][K]>;
|
|
51
|
-
$in: TOptional<import("@sinclair/typebox").TArray<T["properties"][K]>>;
|
|
52
|
-
$nin: TOptional<import("@sinclair/typebox").TArray<T["properties"][K]>>;
|
|
53
|
-
}>]>>; } : never>;
|
|
54
42
|
/**
|
|
55
|
-
*
|
|
43
|
+
* Returns the standard Feathers query syntax for a property schema,
|
|
44
|
+
* including operators like `$gt`, `$lt` etc. for a single property
|
|
45
|
+
*
|
|
46
|
+
* @param def The property definition
|
|
47
|
+
* @returns The Feathers query syntax schema
|
|
48
|
+
*/
|
|
49
|
+
export declare const queryProperty: <T extends TSchema>(def: T) => TOptional<import("@sinclair/typebox").TUnion<[T, import("@sinclair/typebox").TPartial<TObject<{
|
|
50
|
+
$gt: T;
|
|
51
|
+
$gte: T;
|
|
52
|
+
$lt: T;
|
|
53
|
+
$lte: T;
|
|
54
|
+
$ne: T;
|
|
55
|
+
$in: import("@sinclair/typebox").TArray<T>;
|
|
56
|
+
$nin: import("@sinclair/typebox").TArray<T>;
|
|
57
|
+
}>>]>>;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a Feathers query syntax schema for the properties defined in `definition`.
|
|
60
|
+
*
|
|
61
|
+
* @param definition The properties to create the Feathers query syntax schema for
|
|
62
|
+
* @returns The Feathers query syntax schema
|
|
63
|
+
*/
|
|
64
|
+
export declare const queryProperties: <T extends TObject<import("@sinclair/typebox").TProperties>>(definition: T) => TOptional<TObject<T["properties"] extends infer T_1 ? { [K in keyof T_1]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K], import("@sinclair/typebox").TPartial<TObject<{
|
|
65
|
+
$gt: T["properties"][K];
|
|
66
|
+
$gte: T["properties"][K];
|
|
67
|
+
$lt: T["properties"][K];
|
|
68
|
+
$lte: T["properties"][K];
|
|
69
|
+
$ne: T["properties"][K];
|
|
70
|
+
$in: import("@sinclair/typebox").TArray<T["properties"][K]>;
|
|
71
|
+
$nin: import("@sinclair/typebox").TArray<T["properties"][K]>;
|
|
72
|
+
}>>]>>; } : never>>;
|
|
73
|
+
/**
|
|
74
|
+
* Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`, `$or`
|
|
56
75
|
* and `$sort` and `$select` for the allowed properties.
|
|
57
76
|
*
|
|
58
77
|
* @param type The properties to create the query syntax for
|
|
78
|
+
* @param options Options for the TypeBox object schema
|
|
59
79
|
* @returns A TypeBox object representing the complete Feathers query syntax for the given properties
|
|
60
80
|
*/
|
|
61
|
-
export declare const querySyntax: <T extends TObject<import("@sinclair/typebox").TProperties> | TIntersect<TObject<import("@sinclair/typebox").TProperties>[]>>(type: T) => TIntersect<[TObject<{
|
|
62
|
-
$limit:
|
|
63
|
-
$skip:
|
|
64
|
-
$sort:
|
|
65
|
-
$select:
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
81
|
+
export declare const querySyntax: <T extends TObject<import("@sinclair/typebox").TProperties> | TIntersect<TObject<import("@sinclair/typebox").TProperties>[]>>(type: T, options?: ObjectOptions) => TIntersect<[import("@sinclair/typebox").TPartial<TObject<{
|
|
82
|
+
$limit: import("@sinclair/typebox").TNumber;
|
|
83
|
+
$skip: import("@sinclair/typebox").TNumber;
|
|
84
|
+
$sort: TObject<T["properties"] extends infer T_1 ? { [K in keyof T_1]: TOptional<TInteger>; } : never>;
|
|
85
|
+
$select: import("@sinclair/typebox").TUnsafe<(keyof T["properties"])[]>;
|
|
86
|
+
$or: import("@sinclair/typebox").TArray<TOptional<TObject<T["properties"] extends infer T_2 ? { [K_1 in keyof T_2]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K_1], import("@sinclair/typebox").TPartial<TObject<{
|
|
87
|
+
$gt: T["properties"][K_1];
|
|
88
|
+
$gte: T["properties"][K_1];
|
|
89
|
+
$lt: T["properties"][K_1];
|
|
90
|
+
$lte: T["properties"][K_1];
|
|
91
|
+
$ne: T["properties"][K_1];
|
|
92
|
+
$in: import("@sinclair/typebox").TArray<T["properties"][K_1]>;
|
|
93
|
+
$nin: import("@sinclair/typebox").TArray<T["properties"][K_1]>;
|
|
94
|
+
}>>]>>; } : never>>>;
|
|
95
|
+
$and: import("@sinclair/typebox").TArray<TOptional<TObject<T["properties"] extends infer T_2 ? { [K_1 in keyof T_2]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K_1], import("@sinclair/typebox").TPartial<TObject<{
|
|
96
|
+
$gt: T["properties"][K_1];
|
|
97
|
+
$gte: T["properties"][K_1];
|
|
98
|
+
$lt: T["properties"][K_1];
|
|
99
|
+
$lte: T["properties"][K_1];
|
|
100
|
+
$ne: T["properties"][K_1];
|
|
101
|
+
$in: import("@sinclair/typebox").TArray<T["properties"][K_1]>;
|
|
102
|
+
$nin: import("@sinclair/typebox").TArray<T["properties"][K_1]>;
|
|
103
|
+
}>>]>>; } : never>>>;
|
|
104
|
+
}>>, TOptional<TObject<T["properties"] extends infer T_3 ? { [K_1 in keyof T_3]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K_1], import("@sinclair/typebox").TPartial<TObject<{
|
|
105
|
+
$gt: T["properties"][K_1];
|
|
106
|
+
$gte: T["properties"][K_1];
|
|
107
|
+
$lt: T["properties"][K_1];
|
|
108
|
+
$lte: T["properties"][K_1];
|
|
109
|
+
$ne: T["properties"][K_1];
|
|
110
|
+
$in: import("@sinclair/typebox").TArray<T["properties"][K_1]>;
|
|
111
|
+
$nin: import("@sinclair/typebox").TArray<T["properties"][K_1]>;
|
|
112
|
+
}>>]>>; } : never>>]>;
|
package/lib/index.js
CHANGED
|
@@ -40,10 +40,6 @@ exports.getValidator = getValidator;
|
|
|
40
40
|
*/
|
|
41
41
|
const getDataValidator = (def, validator) => schema_1.jsonSchema.getDataValidator(def, validator);
|
|
42
42
|
exports.getDataValidator = getDataValidator;
|
|
43
|
-
const arrayOfKeys = (type) => {
|
|
44
|
-
const keys = Object.keys(type.properties);
|
|
45
|
-
return typebox_1.Type.Unsafe({ type: 'array', items: { type: 'string', enum: keys } });
|
|
46
|
-
};
|
|
47
43
|
/**
|
|
48
44
|
* A TypeBox utility that converts an array of provided strings into a string enum.
|
|
49
45
|
* @param allowedValues array of strings for the enum
|
|
@@ -53,64 +49,92 @@ function StringEnum(allowedValues) {
|
|
|
53
49
|
return typebox_1.Type.Unsafe({ type: 'string', enum: allowedValues });
|
|
54
50
|
}
|
|
55
51
|
exports.StringEnum = StringEnum;
|
|
52
|
+
const arrayOfKeys = (type) => {
|
|
53
|
+
const keys = Object.keys(type.properties);
|
|
54
|
+
return typebox_1.Type.Unsafe({
|
|
55
|
+
type: 'array',
|
|
56
|
+
maxItems: keys.length,
|
|
57
|
+
items: {
|
|
58
|
+
type: 'string',
|
|
59
|
+
...(keys.length > 0 ? { enum: keys } : {})
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Creates the `$sort` Feathers query syntax schema for an object schema
|
|
65
|
+
*
|
|
66
|
+
* @param schema The TypeBox object schema
|
|
67
|
+
* @returns The `$sort` syntax schema
|
|
68
|
+
*/
|
|
56
69
|
function sortDefinition(schema) {
|
|
57
70
|
const properties = Object.keys(schema.properties).reduce((res, key) => {
|
|
58
71
|
const result = res;
|
|
59
72
|
result[key] = typebox_1.Type.Optional(typebox_1.Type.Integer({ minimum: -1, maximum: 1 }));
|
|
60
73
|
return result;
|
|
61
74
|
}, {});
|
|
62
|
-
return {
|
|
63
|
-
type: 'object',
|
|
64
|
-
additionalProperties: false,
|
|
65
|
-
properties
|
|
66
|
-
};
|
|
75
|
+
return typebox_1.Type.Object(properties, { additionalProperties: false });
|
|
67
76
|
}
|
|
68
77
|
exports.sortDefinition = sortDefinition;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Returns the standard Feathers query syntax for a property schema,
|
|
80
|
+
* including operators like `$gt`, `$lt` etc. for a single property
|
|
81
|
+
*
|
|
82
|
+
* @param def The property definition
|
|
83
|
+
* @returns The Feathers query syntax schema
|
|
84
|
+
*/
|
|
85
|
+
const queryProperty = (def) => typebox_1.Type.Optional(typebox_1.Type.Union([
|
|
86
|
+
def,
|
|
87
|
+
typebox_1.Type.Partial(typebox_1.Type.Object({
|
|
88
|
+
$gt: def,
|
|
89
|
+
$gte: def,
|
|
90
|
+
$lt: def,
|
|
91
|
+
$lte: def,
|
|
92
|
+
$ne: def,
|
|
93
|
+
$in: typebox_1.Type.Array(def),
|
|
94
|
+
$nin: typebox_1.Type.Array(def)
|
|
95
|
+
}), { additionalProperties: false })
|
|
96
|
+
]));
|
|
83
97
|
exports.queryProperty = queryProperty;
|
|
84
|
-
|
|
85
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Creates a Feathers query syntax schema for the properties defined in `definition`.
|
|
100
|
+
*
|
|
101
|
+
* @param definition The properties to create the Feathers query syntax schema for
|
|
102
|
+
* @returns The Feathers query syntax schema
|
|
103
|
+
*/
|
|
104
|
+
const queryProperties = (definition) => {
|
|
105
|
+
const properties = Object.keys(definition.properties).reduce((res, key) => {
|
|
86
106
|
const result = res;
|
|
87
|
-
|
|
107
|
+
const value = definition.properties[key];
|
|
108
|
+
if (value.$ref || !schema_1.SUPPORTED_TYPES.includes(value.type)) {
|
|
109
|
+
throw new Error(`Can not create query syntax schema for property '${key}'. Only types ${schema_1.SUPPORTED_TYPES.join(', ')} are allowed.`);
|
|
110
|
+
}
|
|
111
|
+
result[key] = (0, exports.queryProperty)(value);
|
|
88
112
|
return result;
|
|
89
113
|
}, {});
|
|
90
|
-
return {
|
|
91
|
-
type: 'object',
|
|
92
|
-
additionalProperties: false,
|
|
93
|
-
properties
|
|
94
|
-
};
|
|
114
|
+
return typebox_1.Type.Optional(typebox_1.Type.Object(properties, { additionalProperties: false }));
|
|
95
115
|
};
|
|
96
116
|
exports.queryProperties = queryProperties;
|
|
97
117
|
/**
|
|
98
|
-
* Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`
|
|
118
|
+
* Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`, `$or`
|
|
99
119
|
* and `$sort` and `$select` for the allowed properties.
|
|
100
120
|
*
|
|
101
121
|
* @param type The properties to create the query syntax for
|
|
122
|
+
* @param options Options for the TypeBox object schema
|
|
102
123
|
* @returns A TypeBox object representing the complete Feathers query syntax for the given properties
|
|
103
124
|
*/
|
|
104
|
-
const querySyntax = (type) => {
|
|
125
|
+
const querySyntax = (type, options = { additionalProperties: false }) => {
|
|
126
|
+
const propertySchema = (0, exports.queryProperties)(type);
|
|
105
127
|
return typebox_1.Type.Intersect([
|
|
106
|
-
typebox_1.Type.Object({
|
|
107
|
-
$limit: typebox_1.Type.
|
|
108
|
-
$skip: typebox_1.Type.
|
|
109
|
-
$sort:
|
|
110
|
-
$select:
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
128
|
+
typebox_1.Type.Partial(typebox_1.Type.Object({
|
|
129
|
+
$limit: typebox_1.Type.Number({ minimum: 0 }),
|
|
130
|
+
$skip: typebox_1.Type.Number({ minimum: 0 }),
|
|
131
|
+
$sort: sortDefinition(type),
|
|
132
|
+
$select: arrayOfKeys(type),
|
|
133
|
+
$or: typebox_1.Type.Array(propertySchema),
|
|
134
|
+
$and: typebox_1.Type.Array(propertySchema)
|
|
135
|
+
}, { additionalProperties: false })),
|
|
136
|
+
propertySchema
|
|
137
|
+
], options);
|
|
114
138
|
};
|
|
115
139
|
exports.querySyntax = querySyntax;
|
|
116
140
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+CAA0G;AAC1G,+CAAkG;AAElG,oDAAiC;AACjC,oDAAiC;AAQjC;;;;;;GAMG;AACI,MAAM,YAAY,GAAG,CAAiB,MAAe,EAAE,SAAc,EAAmB,EAAE,CAC/F,mBAAU,CAAC,YAAY,CAAC,MAAa,EAAE,SAAS,CAAC,CAAA;AADtC,QAAA,YAAY,gBAC0B;AAEnD;;;;;;;;;GASG;AACI,MAAM,gBAAgB,GAAG,CAAC,GAA6B,EAAE,SAAc,EAAoB,EAAE,CAClG,mBAAU,CAAC,gBAAgB,CAAC,GAAU,EAAE,SAAS,CAAC,CAAA;AADvC,QAAA,gBAAgB,oBACuB;AAEpD;;;;GAIG;AACH,SAAgB,UAAU,CAAqB,aAAqB;IAClE,OAAO,cAAI,CAAC,MAAM,CAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC,CAAA;AACxE,CAAC;AAFD,gCAEC;AAED,MAAM,WAAW,GAAG,CAAoB,IAAO,EAAE,EAAE;IACjD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IACzC,OAAO,cAAI,CAAC,MAAM,CAA4B;QAC5C,IAAI,EAAE,OAAO;QACb,QAAQ,EAAE,IAAI,CAAC,MAAM;QACrB,KAAK,EAAE;YACL,IAAI,EAAE,QAAQ;YACd,GAAG,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,C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C,KAAK,CAAC,cAAc,CAAC;YAC/B,IAAI,EAAE,cAAI,CAAC,KAAK,CAAC,cAAc,CAAC;SACjC,EACD,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAChC,CACF;QACD,cAAc;KACf,EACD,OAAO,CACR,CAAA;AACH,CAAC,CAAA;AAzBY,QAAA,WAAW,eAyBvB"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/typebox",
|
|
3
3
|
"description": "TypeBox integration for @feathersjs/schema",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.34",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -54,15 +54,15 @@
|
|
|
54
54
|
"access": "public"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@feathersjs/schema": "^5.0.0-pre.
|
|
58
|
-
"@sinclair/typebox": "^0.25.
|
|
57
|
+
"@feathersjs/schema": "^5.0.0-pre.34",
|
|
58
|
+
"@sinclair/typebox": "^0.25.10"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@types/mocha": "^10.0.
|
|
62
|
-
"@types/node": "^18.11.
|
|
61
|
+
"@types/mocha": "^10.0.1",
|
|
62
|
+
"@types/node": "^18.11.10",
|
|
63
63
|
"mocha": "^10.1.0",
|
|
64
64
|
"shx": "^0.3.4",
|
|
65
|
-
"typescript": "^4.
|
|
65
|
+
"typescript": "^4.9.3"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "42cca600d00f0b3b9d89fa79be30fcd46bc50132"
|
|
68
68
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Type, TObject, TInteger, TOptional, TSchema, TIntersect } from '@sinclair/typebox'
|
|
2
|
-
import { jsonSchema, Validator, DataValidatorMap, Ajv } from '@feathersjs/schema'
|
|
1
|
+
import { Type, TObject, TInteger, TOptional, TSchema, TIntersect, ObjectOptions } from '@sinclair/typebox'
|
|
2
|
+
import { jsonSchema, Validator, DataValidatorMap, Ajv, SUPPORTED_TYPES } from '@feathersjs/schema'
|
|
3
3
|
|
|
4
4
|
export * from '@sinclair/typebox'
|
|
5
5
|
export * from './default-schemas'
|
|
@@ -33,11 +33,6 @@ export const getValidator = <T = any, R = T>(schema: TObject, validator: Ajv): V
|
|
|
33
33
|
export const getDataValidator = (def: TObject | TDataSchemaMap, validator: Ajv): DataValidatorMap =>
|
|
34
34
|
jsonSchema.getDataValidator(def as any, validator)
|
|
35
35
|
|
|
36
|
-
const arrayOfKeys = <T extends TObject>(type: T) => {
|
|
37
|
-
const keys = Object.keys(type.properties)
|
|
38
|
-
return Type.Unsafe<(keyof T['properties'])[]>({ type: 'array', items: { type: 'string', enum: keys } })
|
|
39
|
-
}
|
|
40
|
-
|
|
41
36
|
/**
|
|
42
37
|
* A TypeBox utility that converts an array of provided strings into a string enum.
|
|
43
38
|
* @param allowedValues array of strings for the enum
|
|
@@ -47,6 +42,24 @@ export function StringEnum<T extends string[]>(allowedValues: [...T]) {
|
|
|
47
42
|
return Type.Unsafe<T[number]>({ type: 'string', enum: allowedValues })
|
|
48
43
|
}
|
|
49
44
|
|
|
45
|
+
const arrayOfKeys = <T extends TObject>(type: T) => {
|
|
46
|
+
const keys = Object.keys(type.properties)
|
|
47
|
+
return Type.Unsafe<(keyof T['properties'])[]>({
|
|
48
|
+
type: 'array',
|
|
49
|
+
maxItems: keys.length,
|
|
50
|
+
items: {
|
|
51
|
+
type: 'string',
|
|
52
|
+
...(keys.length > 0 ? { enum: keys } : {})
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Creates the `$sort` Feathers query syntax schema for an object schema
|
|
59
|
+
*
|
|
60
|
+
* @param schema The TypeBox object schema
|
|
61
|
+
* @returns The `$sort` syntax schema
|
|
62
|
+
*/
|
|
50
63
|
export function sortDefinition<T extends TObject>(schema: T) {
|
|
51
64
|
const properties = Object.keys(schema.properties).reduce((res, key) => {
|
|
52
65
|
const result = res as any
|
|
@@ -56,66 +69,95 @@ export function sortDefinition<T extends TObject>(schema: T) {
|
|
|
56
69
|
return result
|
|
57
70
|
}, {} as { [K in keyof T['properties']]: TOptional<TInteger> })
|
|
58
71
|
|
|
59
|
-
return {
|
|
60
|
-
type: 'object',
|
|
61
|
-
additionalProperties: false,
|
|
62
|
-
properties
|
|
63
|
-
} as TObject<typeof properties>
|
|
72
|
+
return Type.Object(properties, { additionalProperties: false })
|
|
64
73
|
}
|
|
65
74
|
|
|
66
|
-
|
|
67
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Returns the standard Feathers query syntax for a property schema,
|
|
77
|
+
* including operators like `$gt`, `$lt` etc. for a single property
|
|
78
|
+
*
|
|
79
|
+
* @param def The property definition
|
|
80
|
+
* @returns The Feathers query syntax schema
|
|
81
|
+
*/
|
|
82
|
+
export const queryProperty = <T extends TSchema>(def: T) =>
|
|
83
|
+
Type.Optional(
|
|
68
84
|
Type.Union([
|
|
69
85
|
def,
|
|
70
|
-
Type.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
86
|
+
Type.Partial(
|
|
87
|
+
Type.Object({
|
|
88
|
+
$gt: def,
|
|
89
|
+
$gte: def,
|
|
90
|
+
$lt: def,
|
|
91
|
+
$lte: def,
|
|
92
|
+
$ne: def,
|
|
93
|
+
$in: Type.Array(def),
|
|
94
|
+
$nin: Type.Array(def)
|
|
95
|
+
}),
|
|
96
|
+
{ additionalProperties: false }
|
|
97
|
+
)
|
|
79
98
|
])
|
|
80
99
|
)
|
|
81
|
-
}
|
|
82
100
|
|
|
83
101
|
type QueryProperty<T extends TSchema> = ReturnType<typeof queryProperty<T>>
|
|
84
102
|
|
|
85
|
-
|
|
86
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Creates a Feathers query syntax schema for the properties defined in `definition`.
|
|
105
|
+
*
|
|
106
|
+
* @param definition The properties to create the Feathers query syntax schema for
|
|
107
|
+
* @returns The Feathers query syntax schema
|
|
108
|
+
*/
|
|
109
|
+
export const queryProperties = <T extends TObject>(definition: T) => {
|
|
110
|
+
const properties = Object.keys(definition.properties).reduce((res, key) => {
|
|
87
111
|
const result = res as any
|
|
112
|
+
const value = definition.properties[key]
|
|
88
113
|
|
|
89
|
-
|
|
114
|
+
if (value.$ref || !SUPPORTED_TYPES.includes(value.type)) {
|
|
115
|
+
throw new Error(
|
|
116
|
+
`Can not create query syntax schema for property '${key}'. Only types ${SUPPORTED_TYPES.join(
|
|
117
|
+
', '
|
|
118
|
+
)} are allowed.`
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
result[key] = queryProperty(value)
|
|
90
123
|
|
|
91
124
|
return result
|
|
92
125
|
}, {} as { [K in keyof T['properties']]: QueryProperty<T['properties'][K]> })
|
|
93
126
|
|
|
94
|
-
return {
|
|
95
|
-
type: 'object',
|
|
96
|
-
additionalProperties: false,
|
|
97
|
-
properties
|
|
98
|
-
} as TObject<typeof properties>
|
|
127
|
+
return Type.Optional(Type.Object(properties, { additionalProperties: false }))
|
|
99
128
|
}
|
|
100
129
|
|
|
101
130
|
/**
|
|
102
|
-
* Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`
|
|
131
|
+
* Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`, `$or`
|
|
103
132
|
* and `$sort` and `$select` for the allowed properties.
|
|
104
133
|
*
|
|
105
134
|
* @param type The properties to create the query syntax for
|
|
135
|
+
* @param options Options for the TypeBox object schema
|
|
106
136
|
* @returns A TypeBox object representing the complete Feathers query syntax for the given properties
|
|
107
137
|
*/
|
|
108
|
-
export const querySyntax = <T extends TObject | TIntersect>(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
138
|
+
export const querySyntax = <T extends TObject | TIntersect>(
|
|
139
|
+
type: T,
|
|
140
|
+
options: ObjectOptions = { additionalProperties: false }
|
|
141
|
+
) => {
|
|
142
|
+
const propertySchema = queryProperties(type)
|
|
143
|
+
|
|
144
|
+
return Type.Intersect(
|
|
145
|
+
[
|
|
146
|
+
Type.Partial(
|
|
147
|
+
Type.Object(
|
|
148
|
+
{
|
|
149
|
+
$limit: Type.Number({ minimum: 0 }),
|
|
150
|
+
$skip: Type.Number({ minimum: 0 }),
|
|
151
|
+
$sort: sortDefinition(type),
|
|
152
|
+
$select: arrayOfKeys(type),
|
|
153
|
+
$or: Type.Array(propertySchema),
|
|
154
|
+
$and: Type.Array(propertySchema)
|
|
155
|
+
},
|
|
156
|
+
{ additionalProperties: false }
|
|
157
|
+
)
|
|
158
|
+
),
|
|
159
|
+
propertySchema
|
|
160
|
+
],
|
|
161
|
+
options
|
|
162
|
+
)
|
|
121
163
|
}
|