@feathersjs/typebox 5.0.39 → 6.0.0-pre.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/src/index.ts CHANGED
@@ -1,25 +1,26 @@
1
- import {
2
- Type,
1
+ import Type, {
3
2
  TObject,
4
3
  TInteger,
5
4
  TOptional,
6
5
  TSchema,
7
- ObjectOptions,
8
6
  TIntersect,
9
7
  TUnion,
8
+ type TObjectOptions,
10
9
  type TRecord
11
- } from '@sinclair/typebox'
12
- import { jsonSchema, Validator, DataValidatorMap, Ajv } from '@feathersjs/schema'
13
-
14
- export * from '@sinclair/typebox'
15
- export * from './default-schemas'
10
+ } from 'typebox'
11
+ import type { Validator, DataValidatorMap } from '@feathersjs/schema'
12
+ import { jsonSchema, Ajv } from '@feathersjs/schema'
13
+ export * from './default-schemas.js'
14
+ export { Type, Static } from 'typebox'
16
15
 
17
16
  export type TDataSchemaMap = {
18
17
  create: TObject
19
18
  update?: TObject
20
19
  patch?: TObject
21
20
  }
22
-
21
+ // ------------------------------------------------------------------
22
+ // GetValidator: No Change
23
+ // ------------------------------------------------------------------
23
24
  /**
24
25
  * Returns a compiled validation function for a TypeBox object and AJV validator instance.
25
26
  *
@@ -45,18 +46,28 @@ export const getValidator = <T = any, R = T>(
45
46
  export const getDataValidator = (def: TObject | TDataSchemaMap, validator: Ajv): DataValidatorMap =>
46
47
  jsonSchema.getDataValidator(def as any, validator)
47
48
 
49
+ // ------------------------------------------------------------------
50
+ // StringEnum: Tip Type.Enum([]) to produce { enum: [...] } schema
51
+ // ------------------------------------------------------------------
48
52
  /**
49
53
  * A TypeBox utility that converts an array of provided strings into a string enum.
50
54
  * @param allowedValues array of strings for the enum
51
55
  * @returns TypeBox.Type
52
56
  */
53
- export function StringEnum<T extends string[]>(allowedValues: [...T], options?: { default: T[number] }) {
54
- return Type.Unsafe<T[number]>({ type: 'string', enum: allowedValues, ...options })
57
+ export function StringEnum<Values extends string[]>(
58
+ allowedValues: [...Values],
59
+ options?: { default: Values[number] }
60
+ ) {
61
+ return Type.Unsafe<Values[number]>({ type: 'string', enum: allowedValues, ...options })
55
62
  }
56
63
 
57
- const arrayOfKeys = <T extends TObject>(type: T) => {
64
+ // ------------------------------------------------------------------
65
+ // ArrayOfKeys: Explicit, Unsafe
66
+ // ------------------------------------------------------------------
67
+ export type TArrayOfKeys<Type extends TObject> = Type.TUnsafe<(keyof Type['properties'])[]>
68
+ const arrayOfKeys = <Type extends TObject>(type: Type): TArrayOfKeys<Type> => {
58
69
  const keys = Object.keys(type.properties)
59
- return Type.Unsafe<(keyof T['properties'])[]>({
70
+ return Type.Unsafe<(keyof Type['properties'])[]>({
60
71
  type: 'array',
61
72
  maxItems: keys.length,
62
73
  items: {
@@ -66,27 +77,58 @@ const arrayOfKeys = <T extends TObject>(type: T) => {
66
77
  })
67
78
  }
68
79
 
80
+ // ------------------------------------------------------------------
81
+ // SortDefinition: Symmetric Mapping, Return Never
82
+ // ------------------------------------------------------------------
83
+ export type TSortDefinition<
84
+ Type extends TObject,
85
+ Properties extends Type.TProperties = { [K in keyof Type['properties']]: TOptional<TInteger> },
86
+ Return extends Type.TSchema = Type.TObject<Properties>
87
+ > = Return
88
+
69
89
  /**
70
90
  * Creates the `$sort` Feathers query syntax schema for an object schema
71
91
  *
72
- * @param schema The TypeBox object schema
92
+ * @param type The TypeBox object schema
73
93
  * @returns The `$sort` syntax schema
74
94
  */
75
- export function sortDefinition<T extends TObject>(schema: T) {
76
- const properties = Object.keys(schema.properties).reduce(
77
- (res, key) => {
78
- const result = res as any
79
-
80
- result[key] = Type.Optional(Type.Integer({ minimum: -1, maximum: 1 }))
81
-
82
- return result
83
- },
84
- {} as { [K in keyof T['properties']]: TOptional<TInteger> }
85
- )
86
-
87
- return Type.Object(properties, { additionalProperties: false })
95
+ export function sortDefinition<Type extends TObject>(type: Type): TSortDefinition<Type> {
96
+ const properties = Object.keys(type.properties).reduce((res, key) => {
97
+ const result = res as any
98
+ result[key] = Type.Optional(Type.Integer({ minimum: -1, maximum: 1 }))
99
+ return result
100
+ }, {} as Type.TProperties)
101
+ return Type.Object(properties, { additionalProperties: false }) as never
88
102
  }
89
103
 
104
+ // ------------------------------------------------------------------
105
+ // QueryProperty: Symmetric Mapping, Return Never
106
+ // ------------------------------------------------------------------
107
+ export type TQueryProperty<
108
+ Def extends TSchema,
109
+ Extension extends Type.TProperties,
110
+ Return extends Type.TSchema = Type.TUnion<
111
+ [
112
+ Def,
113
+ Type.TPartial<
114
+ Type.TIntersect<
115
+ [
116
+ Type.TObject<{
117
+ $gt: Def
118
+ $gte: Def
119
+ $lt: Def
120
+ $lte: Def
121
+ $ne: Def
122
+ $in: Type.TArray<Def>
123
+ $nin: Type.TArray<Def>
124
+ }>,
125
+ Type.TObject<Extension>
126
+ ]
127
+ >
128
+ >
129
+ ]
130
+ >
131
+ > = Return
90
132
  /**
91
133
  * Returns the standard Feathers query syntax for a property schema,
92
134
  * including operators like `$gt`, `$lt` etc. for a single property
@@ -95,37 +137,44 @@ export function sortDefinition<T extends TObject>(schema: T) {
95
137
  * @param extension Additional properties to add to the property query
96
138
  * @returns The Feathers query syntax schema
97
139
  */
98
- export const queryProperty = <T extends TSchema, X extends { [key: string]: TSchema }>(
99
- def: T,
100
- extension: X = {} as X
101
- ) =>
102
- Type.Optional(
103
- Type.Union([
104
- def,
105
- Type.Partial(
106
- Type.Intersect(
107
- [
108
- Type.Object({
109
- $gt: def,
110
- $gte: def,
111
- $lt: def,
112
- $lte: def,
113
- $ne: def,
114
- $in: def.type === 'array' ? def : Type.Array(def),
115
- $nin: def.type === 'array' ? def : Type.Array(def)
116
- }),
117
- Type.Object(extension)
118
- ],
119
- { additionalProperties: false }
120
- )
140
+ export const queryProperty = <Def extends TSchema, Extension extends { [key: string]: TSchema }>(
141
+ def: Def,
142
+ extension: Extension
143
+ ): TQueryProperty<Def, Extension> =>
144
+ Type.Union([
145
+ def,
146
+ Type.Partial(
147
+ Type.Intersect(
148
+ [
149
+ Type.Object({
150
+ $gt: def,
151
+ $gte: def,
152
+ $lt: def,
153
+ $lte: def,
154
+ $ne: def,
155
+ $in: Type.Array(def),
156
+ $nin: Type.Array(def)
157
+ }),
158
+ Type.Object((extension || {}) as Extension)
159
+ ],
160
+ { additionalProperties: false }
121
161
  )
122
- ])
123
- )
124
-
125
- type QueryProperty<T extends TSchema, X extends { [key: string]: TSchema }> = ReturnType<
126
- typeof queryProperty<T, X>
127
- >
162
+ )
163
+ ]) as any
128
164
 
165
+ // ------------------------------------------------------------------
166
+ // QueryProperties: Symmetric Mapping, Return Never
167
+ // ------------------------------------------------------------------
168
+ type TQueryProperties<
169
+ // Parameters
170
+ Type extends TObject,
171
+ Extensions extends { [K in keyof Type['properties']]?: { [key: string]: TSchema } },
172
+ // Variables + Return
173
+ Properties extends Type.TProperties = {
174
+ [K in keyof Type['properties']]: TQueryProperty<Type['properties'][K], NonNullable<Extensions[K]>>
175
+ },
176
+ Return extends Type.TSchema = Type.TOptional<Type.TObject<Properties>>
177
+ > = Return
129
178
  /**
130
179
  * Creates a Feathers query syntax schema for the properties defined in `definition`.
131
180
  *
@@ -134,68 +183,86 @@ type QueryProperty<T extends TSchema, X extends { [key: string]: TSchema }> = Re
134
183
  * @returns The Feathers query syntax schema
135
184
  */
136
185
  export const queryProperties = <
137
- T extends TObject,
138
- X extends { [K in keyof T['properties']]?: { [key: string]: TSchema } }
186
+ Type extends TObject,
187
+ Extensions extends { [K in keyof Type['properties']]?: { [key: string]: TSchema } }
139
188
  >(
140
- definition: T,
141
- extensions: X = {} as X
142
- ) => {
143
- const properties = Object.keys(definition.properties).reduce(
144
- (res, key) => {
145
- const result = res as any
146
- const value = definition.properties[key]
147
-
148
- result[key] = queryProperty(value, extensions[key])
189
+ definition: Type,
190
+ extensions: Extensions
191
+ ): TQueryProperties<Type, Extensions> => {
192
+ const properties = Object.keys(definition.properties).reduce((res, key) => {
193
+ const result = res as any
194
+ const value = definition.properties[key]
149
195
 
150
- return result
151
- },
152
- {} as { [K in keyof T['properties']]: QueryProperty<T['properties'][K], X[K]> }
153
- )
196
+ result[key] = queryProperty(value, extensions[key] || {})
154
197
 
155
- return Type.Optional(Type.Object(properties, { additionalProperties: false }))
198
+ return result
199
+ }, {})
200
+ return Type.Optional(Type.Object(properties, { additionalProperties: false })) as never
156
201
  }
202
+ // ------------------------------------------------------------------
203
+ // QuerySyntax: Symmetric Mapping, Return Never
204
+ // ------------------------------------------------------------------
205
+ export type TQuerySyntax<
206
+ // Parameters
207
+ Type extends TObject,
208
+ Extensions extends { [K in keyof Type['properties']]?: { [key: string]: TSchema } },
209
+ // Variables + Return
210
+ PropertySchema extends Type.TObject = TQueryProperties<Type, Extensions>,
211
+ Or extends TSchema = Type.TArray<PropertySchema>,
212
+ And extends TSchema = Type.TArray<Type.TUnion<[PropertySchema, Or]>>,
213
+ Return extends TSchema = Type.TPartial<
214
+ Type.TObject<
215
+ {
216
+ $limit: Type.TNumber
217
+ $skip: Type.TNumber
218
+ $sort: TSortDefinition<Type>
219
+ $select: TArrayOfKeys<Type>
220
+ $or: Or
221
+ $and: And
222
+ } & PropertySchema['properties']
223
+ > // todo: consider Simplify<T> to flatten intersection
224
+ >
225
+ > = Return
157
226
 
158
227
  /**
159
228
  * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`, `$or`
160
229
  * and `$sort` and `$select` for the allowed properties.
161
230
  *
162
231
  * @param type The properties to create the query syntax for
163
- * @param extensions Additional properties to add to the query syntax
232
+ * @param extensions Additional properties to add to the query syntax, use `{}` if none
164
233
  * @param options Options for the TypeBox object schema
165
234
  * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
166
235
  */
167
236
  export const querySyntax = <
168
- T extends TObject,
169
- X extends { [K in keyof T['properties']]?: { [key: string]: TSchema } }
237
+ Type extends TObject,
238
+ Extensions extends { [K in keyof Type['properties']]?: { [key: string]: TSchema } }
170
239
  >(
171
- type: T,
172
- extensions: X = {} as X,
173
- options: ObjectOptions = { additionalProperties: false }
174
- ) => {
240
+ type: Type,
241
+ extensions: Extensions,
242
+ options: TObjectOptions = { additionalProperties: false }
243
+ ): TQuerySyntax<Type, Extensions> => {
175
244
  const propertySchema = queryProperties(type, extensions)
176
245
  const $or = Type.Array(propertySchema)
177
246
  const $and = Type.Array(Type.Union([propertySchema, Type.Object({ $or })]))
178
-
179
- return Type.Intersect(
180
- [
181
- Type.Partial(
182
- Type.Object(
183
- {
184
- $limit: Type.Number({ minimum: 0 }),
185
- $skip: Type.Number({ minimum: 0 }),
186
- $sort: sortDefinition(type),
187
- $select: arrayOfKeys(type),
188
- $and,
189
- $or
190
- },
191
- { additionalProperties: false }
192
- )
193
- ),
194
- propertySchema
195
- ],
247
+ return Type.Partial(
248
+ Type.Object(
249
+ {
250
+ $limit: Type.Number({ minimum: 0 }),
251
+ $skip: Type.Number({ minimum: 0 }),
252
+ $sort: sortDefinition(type),
253
+ $select: arrayOfKeys(type),
254
+ $and,
255
+ $or,
256
+ ...propertySchema.properties
257
+ },
258
+ { additionalProperties: false }
259
+ ),
196
260
  options
197
- )
261
+ ) as never
198
262
  }
199
-
200
- export const ObjectIdSchema = () =>
263
+ // ------------------------------------------------------------------
264
+ // ObjectSchemaId: Explicit Return
265
+ // ------------------------------------------------------------------
266
+ export type TObjectIdSchema = Type.TUnion<[Type.TString, Type.TObject<Type.TProperties>]>
267
+ export const ObjectIdSchema = (): TObjectIdSchema =>
201
268
  Type.Union([Type.String({ objectid: true }), Type.Object({}, { additionalProperties: true })])