@feathersjs/typebox 5.0.0-pre.33 → 5.0.0-pre.35

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 CHANGED
@@ -3,6 +3,20 @@
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.35](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.34...v5.0.0-pre.35) (2023-01-12)
7
+
8
+ ### Features
9
+
10
+ - **generators:** Move core code generators to shared generators package ([#2982](https://github.com/feathersjs/feathers/issues/2982)) ([0328d22](https://github.com/feathersjs/feathers/commit/0328d2292153870bc43958f73d2c6f288a8cec17))
11
+ - **schema:** Allow to add additional operators to the query syntax ([#2941](https://github.com/feathersjs/feathers/issues/2941)) ([f324940](https://github.com/feathersjs/feathers/commit/f324940d5795b41e8c6fc113defb0beb7ab03a0a))
12
+
13
+ # [5.0.0-pre.34](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.33...v5.0.0-pre.34) (2022-12-14)
14
+
15
+ ### Bug Fixes
16
+
17
+ - **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))
18
+ - **typebox:** Improve query syntax defaults ([#2888](https://github.com/feathersjs/feathers/issues/2888)) ([59f3cdc](https://github.com/feathersjs/feathers/commit/59f3cdca6376e34fe39a7b91db837d0325aeb5db))
19
+
6
20
  # [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
21
 
8
22
  ### Features
@@ -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 declare type DefaultAppConfiguration = Static<typeof defaultAppConfiguration>;
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 declare type TDataSchemaMap = {
5
+ export type TDataSchemaMap = {
6
6
  create: TObject;
7
7
  update?: TObject;
8
8
  patch?: TObject;
@@ -32,43 +32,90 @@ 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
- * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`
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
+ * @param extension Additional properties to add to the property query
48
+ * @returns The Feathers query syntax schema
49
+ */
50
+ export declare const queryProperty: <T extends TSchema, X extends {
51
+ [key: string]: TSchema;
52
+ }>(def: T, extension?: X) => TOptional<import("@sinclair/typebox").TUnion<[T, import("@sinclair/typebox").TPartial<TObject<{
53
+ $gt: T;
54
+ $gte: T;
55
+ $lt: T;
56
+ $lte: T;
57
+ $ne: T;
58
+ $in: import("@sinclair/typebox").TArray<T>;
59
+ $nin: import("@sinclair/typebox").TArray<T>;
60
+ } & X>>]>>;
61
+ /**
62
+ * Creates a Feathers query syntax schema for the properties defined in `definition`.
63
+ *
64
+ * @param definition The properties to create the Feathers query syntax schema for
65
+ * @param extensions Additional properties to add to a property query
66
+ * @returns The Feathers query syntax schema
67
+ */
68
+ export declare const queryProperties: <T extends TObject<import("@sinclair/typebox").TProperties>, X extends T["properties"] extends infer T_1 ? { [K in keyof T_1]?: {
69
+ [key: string]: TSchema;
70
+ }; } : never>(definition: T, extensions?: X) => 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<{
71
+ $gt: T["properties"][K_1];
72
+ $gte: T["properties"][K_1];
73
+ $lt: T["properties"][K_1];
74
+ $lte: T["properties"][K_1];
75
+ $ne: T["properties"][K_1];
76
+ $in: import("@sinclair/typebox").TArray<T["properties"][K_1]>;
77
+ $nin: import("@sinclair/typebox").TArray<T["properties"][K_1]>;
78
+ } & X[K_1]>>]>>; } : never>>;
79
+ /**
80
+ * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`, `$or`
56
81
  * and `$sort` and `$select` for the allowed properties.
57
82
  *
58
83
  * @param type The properties to create the query syntax for
84
+ * @param extensions Additional properties to add to the query syntax
85
+ * @param options Options for the TypeBox object schema
59
86
  * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
60
87
  */
61
- export declare const querySyntax: <T extends TObject<import("@sinclair/typebox").TProperties> | TIntersect<TObject<import("@sinclair/typebox").TProperties>[]>>(type: T) => TIntersect<[TObject<{
62
- $limit: TOptional<import("@sinclair/typebox").TNumber>;
63
- $skip: TOptional<import("@sinclair/typebox").TNumber>;
64
- $sort: TOptional<TObject<T["properties"] extends infer T_1 ? { [K in keyof T_1]: TOptional<TInteger>; } : never>>;
65
- $select: TOptional<import("@sinclair/typebox").TUnsafe<(keyof T["properties"])[]>>;
66
- }>, TObject<T["properties"] extends infer T_2 ? { [K_1 in keyof T_2]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K_1], TObject<{
67
- $gt: TOptional<T["properties"][K_1]>;
68
- $gte: TOptional<T["properties"][K_1]>;
69
- $lt: TOptional<T["properties"][K_1]>;
70
- $lte: TOptional<T["properties"][K_1]>;
71
- $ne: TOptional<T["properties"][K_1]>;
72
- $in: TOptional<import("@sinclair/typebox").TArray<T["properties"][K_1]>>;
73
- $nin: TOptional<import("@sinclair/typebox").TArray<T["properties"][K_1]>>;
74
- }>]>>; } : never>]>;
88
+ export declare const querySyntax: <T extends TObject<import("@sinclair/typebox").TProperties> | TIntersect<TObject<import("@sinclair/typebox").TProperties>[]>, X extends T["properties"] extends infer T_1 ? { [K in keyof T_1]?: {
89
+ [key: string]: TSchema;
90
+ }; } : never>(type: T, extensions?: X, options?: ObjectOptions) => TIntersect<[import("@sinclair/typebox").TPartial<TObject<{
91
+ $limit: import("@sinclair/typebox").TNumber;
92
+ $skip: import("@sinclair/typebox").TNumber;
93
+ $sort: TObject<T["properties"] extends infer T_2 ? { [K_1 in keyof T_2]: TOptional<TInteger>; } : never>;
94
+ $select: import("@sinclair/typebox").TUnsafe<(keyof T["properties"])[]>;
95
+ $or: import("@sinclair/typebox").TArray<TOptional<TObject<T["properties"] extends infer T_3 ? { [K_2 in keyof T_3]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K_2], import("@sinclair/typebox").TPartial<TObject<{
96
+ $gt: T["properties"][K_2];
97
+ $gte: T["properties"][K_2];
98
+ $lt: T["properties"][K_2];
99
+ $lte: T["properties"][K_2];
100
+ $ne: T["properties"][K_2];
101
+ $in: import("@sinclair/typebox").TArray<T["properties"][K_2]>;
102
+ $nin: import("@sinclair/typebox").TArray<T["properties"][K_2]>;
103
+ } & X[K_2]>>]>>; } : never>>>;
104
+ $and: import("@sinclair/typebox").TArray<TOptional<TObject<T["properties"] extends infer T_3 ? { [K_2 in keyof T_3]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K_2], import("@sinclair/typebox").TPartial<TObject<{
105
+ $gt: T["properties"][K_2];
106
+ $gte: T["properties"][K_2];
107
+ $lt: T["properties"][K_2];
108
+ $lte: T["properties"][K_2];
109
+ $ne: T["properties"][K_2];
110
+ $in: import("@sinclair/typebox").TArray<T["properties"][K_2]>;
111
+ $nin: import("@sinclair/typebox").TArray<T["properties"][K_2]>;
112
+ } & X[K_2]>>]>>; } : never>>>;
113
+ }>>, TOptional<TObject<T["properties"] extends infer T_4 ? { [K_2 in keyof T_4]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K_2], import("@sinclair/typebox").TPartial<TObject<{
114
+ $gt: T["properties"][K_2];
115
+ $gte: T["properties"][K_2];
116
+ $lt: T["properties"][K_2];
117
+ $lte: T["properties"][K_2];
118
+ $ne: T["properties"][K_2];
119
+ $in: import("@sinclair/typebox").TArray<T["properties"][K_2]>;
120
+ $nin: import("@sinclair/typebox").TArray<T["properties"][K_2]>;
121
+ } & X[K_2]>>]>>; } : 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,96 @@ 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
- const queryProperty = (def) => {
70
- return typebox_1.Type.Optional(typebox_1.Type.Union([
71
- def,
72
- typebox_1.Type.Object({
73
- $gt: typebox_1.Type.Optional(def),
74
- $gte: typebox_1.Type.Optional(def),
75
- $lt: typebox_1.Type.Optional(def),
76
- $lte: typebox_1.Type.Optional(def),
77
- $ne: typebox_1.Type.Optional(def),
78
- $in: typebox_1.Type.Optional(typebox_1.Type.Array(def)),
79
- $nin: typebox_1.Type.Optional(typebox_1.Type.Array(def))
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
+ * @param extension Additional properties to add to the property query
84
+ * @returns The Feathers query syntax schema
85
+ */
86
+ const queryProperty = (def, extension = {}) => typebox_1.Type.Optional(typebox_1.Type.Union([
87
+ def,
88
+ typebox_1.Type.Partial(typebox_1.Type.Object({
89
+ $gt: def,
90
+ $gte: def,
91
+ $lt: def,
92
+ $lte: def,
93
+ $ne: def,
94
+ $in: typebox_1.Type.Array(def),
95
+ $nin: typebox_1.Type.Array(def),
96
+ ...extension
97
+ }), { additionalProperties: false })
98
+ ]));
83
99
  exports.queryProperty = queryProperty;
84
- const queryProperties = (type) => {
85
- const properties = Object.keys(type.properties).reduce((res, key) => {
100
+ /**
101
+ * Creates a Feathers query syntax schema for the properties defined in `definition`.
102
+ *
103
+ * @param definition The properties to create the Feathers query syntax schema for
104
+ * @param extensions Additional properties to add to a property query
105
+ * @returns The Feathers query syntax schema
106
+ */
107
+ const queryProperties = (definition, extensions = {}) => {
108
+ const properties = Object.keys(definition.properties).reduce((res, key) => {
86
109
  const result = res;
87
- result[key] = (0, exports.queryProperty)(type.properties[key]);
110
+ const value = definition.properties[key];
111
+ if (value.$ref) {
112
+ throw new Error(`Can not create query syntax schema for reference property '${key}'`);
113
+ }
114
+ result[key] = (0, exports.queryProperty)(value, extensions[key]);
88
115
  return result;
89
116
  }, {});
90
- return {
91
- type: 'object',
92
- additionalProperties: false,
93
- properties
94
- };
117
+ return typebox_1.Type.Optional(typebox_1.Type.Object(properties, { additionalProperties: false }));
95
118
  };
96
119
  exports.queryProperties = queryProperties;
97
120
  /**
98
- * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`
121
+ * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`, `$or`
99
122
  * and `$sort` and `$select` for the allowed properties.
100
123
  *
101
124
  * @param type The properties to create the query syntax for
125
+ * @param extensions Additional properties to add to the query syntax
126
+ * @param options Options for the TypeBox object schema
102
127
  * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
103
128
  */
104
- const querySyntax = (type) => {
129
+ const querySyntax = (type, extensions = {}, options = { additionalProperties: false }) => {
130
+ const propertySchema = (0, exports.queryProperties)(type, extensions);
105
131
  return typebox_1.Type.Intersect([
106
- typebox_1.Type.Object({
107
- $limit: typebox_1.Type.Optional(typebox_1.Type.Number({ minimum: 0 })),
108
- $skip: typebox_1.Type.Optional(typebox_1.Type.Number({ minimum: 0 })),
109
- $sort: typebox_1.Type.Optional(sortDefinition(type)),
110
- $select: typebox_1.Type.Optional(arrayOfKeys(type))
111
- }, { additionalProperties: false }),
112
- (0, exports.queryProperties)(type)
113
- ]);
132
+ typebox_1.Type.Partial(typebox_1.Type.Object({
133
+ $limit: typebox_1.Type.Number({ minimum: 0 }),
134
+ $skip: typebox_1.Type.Number({ minimum: 0 }),
135
+ $sort: sortDefinition(type),
136
+ $select: arrayOfKeys(type),
137
+ $or: typebox_1.Type.Array(propertySchema),
138
+ $and: typebox_1.Type.Array(propertySchema)
139
+ }, { additionalProperties: false })),
140
+ propertySchema
141
+ ], options);
114
142
  };
115
143
  exports.querySyntax = querySyntax;
116
144
  //# 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,+CAA2F;AAC3F,+CAAiF;AAEjF,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,gBAC0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AAA;IAExD,OAAO,cAAI,CAAC,SAAS,CACnB;QACE,cAAI,CAAC,OAAO,CACV,cAAI,CAAC,MAAM,CACT;YACE,MAAM,EAAE,cAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;YACnC,KAAK,EAAE,cAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;YAClC,KAAK,EAAE,cAAc,CAAC,IAAI,CAAC;YAC3B,OAAO,EAAE,WAAW,CAAC,IAAI,CAAC;YAC1B,GAAG,EAAE,cAAI,CAAC,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;AA7BY,QAAA,WAAW,eA6BvB"}
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.33",
4
+ "version": "5.0.0-pre.35",
5
5
  "homepage": "https://feathersjs.com",
6
6
  "main": "lib/",
7
7
  "types": "lib/",
@@ -42,7 +42,7 @@
42
42
  ],
43
43
  "scripts": {
44
44
  "prepublish": "npm run compile",
45
- "pack": "npm pack --pack-destination ../cli/test/build",
45
+ "pack": "npm pack --pack-destination ../generators/test/build",
46
46
  "compile": "shx rm -rf lib/ && tsc && npm run pack",
47
47
  "mocha": "mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts",
48
48
  "test": "npm run compile && npm run mocha"
@@ -54,15 +54,15 @@
54
54
  "access": "public"
55
55
  },
56
56
  "dependencies": {
57
- "@feathersjs/schema": "^5.0.0-pre.33",
58
- "@sinclair/typebox": "^0.25.2"
57
+ "@feathersjs/schema": "^5.0.0-pre.35",
58
+ "@sinclair/typebox": "^0.25.16"
59
59
  },
60
60
  "devDependencies": {
61
- "@types/mocha": "^10.0.0",
62
- "@types/node": "^18.11.9",
63
- "mocha": "^10.1.0",
61
+ "@types/mocha": "^10.0.1",
62
+ "@types/node": "^18.11.18",
63
+ "mocha": "^10.2.0",
64
64
  "shx": "^0.3.4",
65
- "typescript": "^4.8.4"
65
+ "typescript": "^4.9.4"
66
66
  },
67
- "gitHead": "89f516bcb1457e23a02c6212e9cd8bacc4d267d4"
67
+ "gitHead": "c641598d9a4de3ceda10f56cf2af288a4236b15e"
68
68
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Type, TObject, TInteger, TOptional, TSchema, TIntersect } from '@sinclair/typebox'
1
+ import { Type, TObject, TInteger, TOptional, TSchema, TIntersect, ObjectOptions } from '@sinclair/typebox'
2
2
  import { jsonSchema, Validator, DataValidatorMap, Ajv } from '@feathersjs/schema'
3
3
 
4
4
  export * from '@sinclair/typebox'
@@ -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,110 @@ 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
- export const queryProperty = <T extends TSchema>(def: T) => {
67
- return Type.Optional(
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
+ * @param extension Additional properties to add to the property query
81
+ * @returns The Feathers query syntax schema
82
+ */
83
+ export const queryProperty = <T extends TSchema, X extends { [key: string]: TSchema }>(
84
+ def: T,
85
+ extension: X = {} as X
86
+ ) =>
87
+ Type.Optional(
68
88
  Type.Union([
69
89
  def,
70
- Type.Object({
71
- $gt: Type.Optional(def),
72
- $gte: Type.Optional(def),
73
- $lt: Type.Optional(def),
74
- $lte: Type.Optional(def),
75
- $ne: Type.Optional(def),
76
- $in: Type.Optional(Type.Array(def)),
77
- $nin: Type.Optional(Type.Array(def))
78
- })
90
+ Type.Partial(
91
+ Type.Object({
92
+ $gt: def,
93
+ $gte: def,
94
+ $lt: def,
95
+ $lte: def,
96
+ $ne: def,
97
+ $in: Type.Array(def),
98
+ $nin: Type.Array(def),
99
+ ...extension
100
+ }),
101
+ { additionalProperties: false }
102
+ )
79
103
  ])
80
104
  )
81
- }
82
105
 
83
- type QueryProperty<T extends TSchema> = ReturnType<typeof queryProperty<T>>
106
+ type QueryProperty<T extends TSchema, X extends { [key: string]: TSchema }> = ReturnType<
107
+ typeof queryProperty<T, X>
108
+ >
84
109
 
85
- export const queryProperties = <T extends TObject>(type: T) => {
86
- const properties = Object.keys(type.properties).reduce((res, key) => {
110
+ /**
111
+ * Creates a Feathers query syntax schema for the properties defined in `definition`.
112
+ *
113
+ * @param definition The properties to create the Feathers query syntax schema for
114
+ * @param extensions Additional properties to add to a property query
115
+ * @returns The Feathers query syntax schema
116
+ */
117
+ export const queryProperties = <
118
+ T extends TObject,
119
+ X extends { [K in keyof T['properties']]?: { [key: string]: TSchema } }
120
+ >(
121
+ definition: T,
122
+ extensions: X = {} as X
123
+ ) => {
124
+ const properties = Object.keys(definition.properties).reduce((res, key) => {
87
125
  const result = res as any
126
+ const value = definition.properties[key]
88
127
 
89
- result[key] = queryProperty(type.properties[key])
128
+ if (value.$ref) {
129
+ throw new Error(`Can not create query syntax schema for reference property '${key}'`)
130
+ }
131
+
132
+ result[key] = queryProperty(value, extensions[key])
90
133
 
91
134
  return result
92
- }, {} as { [K in keyof T['properties']]: QueryProperty<T['properties'][K]> })
135
+ }, {} as { [K in keyof T['properties']]: QueryProperty<T['properties'][K], X[K]> })
93
136
 
94
- return {
95
- type: 'object',
96
- additionalProperties: false,
97
- properties
98
- } as TObject<typeof properties>
137
+ return Type.Optional(Type.Object(properties, { additionalProperties: false }))
99
138
  }
100
139
 
101
140
  /**
102
- * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`
141
+ * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`, `$or`
103
142
  * and `$sort` and `$select` for the allowed properties.
104
143
  *
105
144
  * @param type The properties to create the query syntax for
145
+ * @param extensions Additional properties to add to the query syntax
146
+ * @param options Options for the TypeBox object schema
106
147
  * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
107
148
  */
108
- export const querySyntax = <T extends TObject | TIntersect>(type: T) => {
109
- return Type.Intersect([
110
- Type.Object(
111
- {
112
- $limit: Type.Optional(Type.Number({ minimum: 0 })),
113
- $skip: Type.Optional(Type.Number({ minimum: 0 })),
114
- $sort: Type.Optional(sortDefinition(type)),
115
- $select: Type.Optional(arrayOfKeys(type))
116
- },
117
- { additionalProperties: false }
118
- ),
119
- queryProperties(type)
120
- ])
149
+ export const querySyntax = <
150
+ T extends TObject | TIntersect,
151
+ X extends { [K in keyof T['properties']]?: { [key: string]: TSchema } }
152
+ >(
153
+ type: T,
154
+ extensions: X = {} as X,
155
+ options: ObjectOptions = { additionalProperties: false }
156
+ ) => {
157
+ const propertySchema = queryProperties(type, extensions)
158
+
159
+ return Type.Intersect(
160
+ [
161
+ Type.Partial(
162
+ Type.Object(
163
+ {
164
+ $limit: Type.Number({ minimum: 0 }),
165
+ $skip: Type.Number({ minimum: 0 }),
166
+ $sort: sortDefinition(type),
167
+ $select: arrayOfKeys(type),
168
+ $or: Type.Array(propertySchema),
169
+ $and: Type.Array(propertySchema)
170
+ },
171
+ { additionalProperties: false }
172
+ )
173
+ ),
174
+ propertySchema
175
+ ],
176
+ options
177
+ )
121
178
  }