@feathersjs/typebox 5.0.0-pre.32 → 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 CHANGED
@@ -3,6 +3,19 @@
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
+
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)
14
+
15
+ ### Features
16
+
17
+ - **schema:** Add StringEnum to TypeBox module ([#2827](https://github.com/feathersjs/feathers/issues/2827)) ([65d3665](https://github.com/feathersjs/feathers/commit/65d36656f50a48f633fa3fcabaea10521d04bf1c))
18
+
6
19
  # [5.0.0-pre.32](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.31...v5.0.0-pre.32) (2022-10-26)
7
20
 
8
21
  **Note:** Version bump only for package @feathersjs/typebox
@@ -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;
@@ -26,43 +26,87 @@ export declare const getValidator: <T = any, R = T>(schema: TObject, validator:
26
26
  * @returns A map of validator functions
27
27
  */
28
28
  export declare const getDataValidator: (def: TObject | TDataSchemaMap, validator: Ajv) => DataValidatorMap;
29
+ /**
30
+ * A TypeBox utility that converts an array of provided strings into a string enum.
31
+ * @param allowedValues array of strings for the enum
32
+ * @returns TypeBox.Type
33
+ */
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
+ */
29
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>;
30
- export declare const queryProperty: <T extends TSchema>(def: T) => TOptional<import("@sinclair/typebox").TUnion<[T, TObject<{
31
- $gt: TOptional<T>;
32
- $gte: TOptional<T>;
33
- $lt: TOptional<T>;
34
- $lte: TOptional<T>;
35
- $ne: TOptional<T>;
36
- $in: TOptional<import("@sinclair/typebox").TArray<T>>;
37
- $nin: TOptional<import("@sinclair/typebox").TArray<T>>;
38
- }>]>>;
39
- 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<{
40
- $gt: TOptional<T["properties"][K]>;
41
- $gte: TOptional<T["properties"][K]>;
42
- $lt: TOptional<T["properties"][K]>;
43
- $lte: TOptional<T["properties"][K]>;
44
- $ne: TOptional<T["properties"][K]>;
45
- $in: TOptional<import("@sinclair/typebox").TArray<T["properties"][K]>>;
46
- $nin: TOptional<import("@sinclair/typebox").TArray<T["properties"][K]>>;
47
- }>]>>; } : never>;
48
42
  /**
49
- * 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
+ * @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`
50
75
  * and `$sort` and `$select` for the allowed properties.
51
76
  *
52
77
  * @param type The properties to create the query syntax for
78
+ * @param options Options for the TypeBox object schema
53
79
  * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
54
80
  */
55
- export declare const querySyntax: <T extends TObject<import("@sinclair/typebox").TProperties> | TIntersect<TObject<import("@sinclair/typebox").TProperties>[]>>(type: T) => TIntersect<[TObject<{
56
- $limit: TOptional<import("@sinclair/typebox").TNumber>;
57
- $skip: TOptional<import("@sinclair/typebox").TNumber>;
58
- $sort: TOptional<TObject<T["properties"] extends infer T_1 ? { [K in keyof T_1]: TOptional<TInteger>; } : never>>;
59
- $select: TOptional<import("@sinclair/typebox").TUnsafe<(keyof T["properties"])[]>>;
60
- }>, TObject<T["properties"] extends infer T_2 ? { [K_1 in keyof T_2]: TOptional<import("@sinclair/typebox").TUnion<[T["properties"][K_1], TObject<{
61
- $gt: TOptional<T["properties"][K_1]>;
62
- $gte: TOptional<T["properties"][K_1]>;
63
- $lt: TOptional<T["properties"][K_1]>;
64
- $lte: TOptional<T["properties"][K_1]>;
65
- $ne: TOptional<T["properties"][K_1]>;
66
- $in: TOptional<import("@sinclair/typebox").TArray<T["properties"][K_1]>>;
67
- $nin: TOptional<import("@sinclair/typebox").TArray<T["properties"][K_1]>>;
68
- }>]>>; } : never>]>;
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
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.querySyntax = exports.queryProperties = exports.queryProperty = exports.sortDefinition = exports.getDataValidator = exports.getValidator = void 0;
17
+ exports.querySyntax = exports.queryProperties = exports.queryProperty = exports.sortDefinition = exports.StringEnum = exports.getDataValidator = exports.getValidator = void 0;
18
18
  const typebox_1 = require("@sinclair/typebox");
19
19
  const schema_1 = require("@feathersjs/schema");
20
20
  __exportStar(require("@sinclair/typebox"), exports);
@@ -40,68 +40,101 @@ exports.getValidator = getValidator;
40
40
  */
41
41
  const getDataValidator = (def, validator) => schema_1.jsonSchema.getDataValidator(def, validator);
42
42
  exports.getDataValidator = getDataValidator;
43
+ /**
44
+ * A TypeBox utility that converts an array of provided strings into a string enum.
45
+ * @param allowedValues array of strings for the enum
46
+ * @returns TypeBox.Type
47
+ */
48
+ function StringEnum(allowedValues) {
49
+ return typebox_1.Type.Unsafe({ type: 'string', enum: allowedValues });
50
+ }
51
+ exports.StringEnum = StringEnum;
43
52
  const arrayOfKeys = (type) => {
44
53
  const keys = Object.keys(type.properties);
45
- return typebox_1.Type.Unsafe({ type: 'array', items: { type: 'string', enum: keys } });
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
+ });
46
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
+ */
47
69
  function sortDefinition(schema) {
48
70
  const properties = Object.keys(schema.properties).reduce((res, key) => {
49
71
  const result = res;
50
72
  result[key] = typebox_1.Type.Optional(typebox_1.Type.Integer({ minimum: -1, maximum: 1 }));
51
73
  return result;
52
74
  }, {});
53
- return {
54
- type: 'object',
55
- additionalProperties: false,
56
- properties
57
- };
75
+ return typebox_1.Type.Object(properties, { additionalProperties: false });
58
76
  }
59
77
  exports.sortDefinition = sortDefinition;
60
- const queryProperty = (def) => {
61
- return typebox_1.Type.Optional(typebox_1.Type.Union([
62
- def,
63
- typebox_1.Type.Object({
64
- $gt: typebox_1.Type.Optional(def),
65
- $gte: typebox_1.Type.Optional(def),
66
- $lt: typebox_1.Type.Optional(def),
67
- $lte: typebox_1.Type.Optional(def),
68
- $ne: typebox_1.Type.Optional(def),
69
- $in: typebox_1.Type.Optional(typebox_1.Type.Array(def)),
70
- $nin: typebox_1.Type.Optional(typebox_1.Type.Array(def))
71
- })
72
- ]));
73
- };
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
+ ]));
74
97
  exports.queryProperty = queryProperty;
75
- const queryProperties = (type) => {
76
- const properties = Object.keys(type.properties).reduce((res, key) => {
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) => {
77
106
  const result = res;
78
- result[key] = (0, exports.queryProperty)(type.properties[key]);
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);
79
112
  return result;
80
113
  }, {});
81
- return {
82
- type: 'object',
83
- additionalProperties: false,
84
- properties
85
- };
114
+ return typebox_1.Type.Optional(typebox_1.Type.Object(properties, { additionalProperties: false }));
86
115
  };
87
116
  exports.queryProperties = queryProperties;
88
117
  /**
89
- * 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`
90
119
  * and `$sort` and `$select` for the allowed properties.
91
120
  *
92
121
  * @param type The properties to create the query syntax for
122
+ * @param options Options for the TypeBox object schema
93
123
  * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
94
124
  */
95
- const querySyntax = (type) => {
125
+ const querySyntax = (type, options = { additionalProperties: false }) => {
126
+ const propertySchema = (0, exports.queryProperties)(type);
96
127
  return typebox_1.Type.Intersect([
97
- typebox_1.Type.Object({
98
- $limit: typebox_1.Type.Optional(typebox_1.Type.Number({ minimum: 0 })),
99
- $skip: typebox_1.Type.Optional(typebox_1.Type.Number({ minimum: 0 })),
100
- $sort: typebox_1.Type.Optional(sortDefinition(type)),
101
- $select: typebox_1.Type.Optional(arrayOfKeys(type))
102
- }, { additionalProperties: false }),
103
- (0, exports.queryProperties)(type)
104
- ]);
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);
105
138
  };
106
139
  exports.querySyntax = querySyntax;
107
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,+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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AAE,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;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.32",
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.32",
58
- "@sinclair/typebox": "^0.24.44"
57
+ "@feathersjs/schema": "^5.0.0-pre.34",
58
+ "@sinclair/typebox": "^0.25.10"
59
59
  },
60
60
  "devDependencies": {
61
- "@types/mocha": "^10.0.0",
62
- "@types/node": "^18.8.2",
63
- "mocha": "^10.0.0",
61
+ "@types/mocha": "^10.0.1",
62
+ "@types/node": "^18.11.10",
63
+ "mocha": "^10.1.0",
64
64
  "shx": "^0.3.4",
65
- "typescript": "^4.8.4"
65
+ "typescript": "^4.9.3"
66
66
  },
67
- "gitHead": "b405e205fc6d9fbd42b42c7a3f30ea4bf33d121d"
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,33 @@ 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
+ /**
37
+ * A TypeBox utility that converts an array of provided strings into a string enum.
38
+ * @param allowedValues array of strings for the enum
39
+ * @returns TypeBox.Type
40
+ */
41
+ export function StringEnum<T extends string[]>(allowedValues: [...T]) {
42
+ return Type.Unsafe<T[number]>({ type: 'string', enum: allowedValues })
43
+ }
44
+
36
45
  const arrayOfKeys = <T extends TObject>(type: T) => {
37
46
  const keys = Object.keys(type.properties)
38
- return Type.Unsafe<(keyof T['properties'])[]>({ type: 'array', items: { type: 'string', enum: keys } })
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
+ })
39
55
  }
40
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
+ */
41
63
  export function sortDefinition<T extends TObject>(schema: T) {
42
64
  const properties = Object.keys(schema.properties).reduce((res, key) => {
43
65
  const result = res as any
@@ -47,66 +69,95 @@ export function sortDefinition<T extends TObject>(schema: T) {
47
69
  return result
48
70
  }, {} as { [K in keyof T['properties']]: TOptional<TInteger> })
49
71
 
50
- return {
51
- type: 'object',
52
- additionalProperties: false,
53
- properties
54
- } as TObject<typeof properties>
72
+ return Type.Object(properties, { additionalProperties: false })
55
73
  }
56
74
 
57
- export const queryProperty = <T extends TSchema>(def: T) => {
58
- 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
+ * @returns The Feathers query syntax schema
81
+ */
82
+ export const queryProperty = <T extends TSchema>(def: T) =>
83
+ Type.Optional(
59
84
  Type.Union([
60
85
  def,
61
- Type.Object({
62
- $gt: Type.Optional(def),
63
- $gte: Type.Optional(def),
64
- $lt: Type.Optional(def),
65
- $lte: Type.Optional(def),
66
- $ne: Type.Optional(def),
67
- $in: Type.Optional(Type.Array(def)),
68
- $nin: Type.Optional(Type.Array(def))
69
- })
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
+ )
70
98
  ])
71
99
  )
72
- }
73
100
 
74
101
  type QueryProperty<T extends TSchema> = ReturnType<typeof queryProperty<T>>
75
102
 
76
- export const queryProperties = <T extends TObject>(type: T) => {
77
- const properties = Object.keys(type.properties).reduce((res, key) => {
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) => {
78
111
  const result = res as any
112
+ const value = definition.properties[key]
79
113
 
80
- result[key] = queryProperty(type.properties[key])
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)
81
123
 
82
124
  return result
83
125
  }, {} as { [K in keyof T['properties']]: QueryProperty<T['properties'][K]> })
84
126
 
85
- return {
86
- type: 'object',
87
- additionalProperties: false,
88
- properties
89
- } as TObject<typeof properties>
127
+ return Type.Optional(Type.Object(properties, { additionalProperties: false }))
90
128
  }
91
129
 
92
130
  /**
93
- * 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`
94
132
  * and `$sort` and `$select` for the allowed properties.
95
133
  *
96
134
  * @param type The properties to create the query syntax for
135
+ * @param options Options for the TypeBox object schema
97
136
  * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
98
137
  */
99
- export const querySyntax = <T extends TObject | TIntersect>(type: T) => {
100
- return Type.Intersect([
101
- Type.Object(
102
- {
103
- $limit: Type.Optional(Type.Number({ minimum: 0 })),
104
- $skip: Type.Optional(Type.Number({ minimum: 0 })),
105
- $sort: Type.Optional(sortDefinition(type)),
106
- $select: Type.Optional(arrayOfKeys(type))
107
- },
108
- { additionalProperties: false }
109
- ),
110
- queryProperties(type)
111
- ])
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
+ )
112
163
  }