Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/generators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@types/mocha": "^10.0.10",
"@types/node": "^24.3.1",
"@types/prettier": "^2.7.3",
"@sinclair/typebox": "^0.34.0",
"axios": "^1.11.0",
"knex": "^3.1.0",
"mocha": "^11.7.2",
Expand Down
1 change: 1 addition & 0 deletions packages/generators/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export const generate = (ctx: AppGeneratorArguments) =>

if (schema === 'typebox') {
dependencies.push('@feathersjs/typebox')
dependencies.push('@sinclair/typebox')
}

return addVersions(dependencies, dependencyVersions)
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/src/app/templates/configuration.tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import type { Static } from '@feathersjs/typebox'

import { dataValidator } from './validators'

export const configurationSchema = Type.Intersect([
export const configurationSchema = Type.Composite([
defaultAppConfiguration,
Type.Object({
host: Type.String(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const ${camelName}ExternalResolver = resolve<${upperName}, HookContext<${
authStrategies,
`// The password should never be visible externally
password: async () => undefined`
)}
)}
})

// Schema for creating new entries
Expand Down Expand Up @@ -90,8 +90,8 @@ export const ${camelName}QueryProperties = Type.Pick(${camelName}Schema, [
: `'text'`
}
])
export const ${camelName}QuerySchema = Type.Intersect([
querySyntax(${camelName}QueryProperties),
export const ${camelName}QuerySchema = Type.Composite([
querySyntax(${camelName}QueryProperties, {}),
// Add additional query properties here
Type.Object({}, { additionalProperties: false })
], { additionalProperties: false })
Expand Down
9 changes: 6 additions & 3 deletions packages/typebox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@
"access": "public"
},
"dependencies": {
"@feathersjs/schema": "^5.0.34",
"@sinclair/typebox": "^0.25.0"
"@feathersjs/schema": "^5.0.34"
},
"peerDependencies": {
"@sinclair/typebox": ">=0.26.0"
},
"devDependencies": {
"@sinclair/typebox": "^0.34.0",
"@types/mocha": "^10.0.10",
"@types/node": "^24.3.1",
"mocha": "^11.7.2",
"shx": "^0.4.0",
"typescript": "^5.9.2"
},
"gitHead": "90caf635aec850550b9d37bea2762af959d9e8d5"
}
}
78 changes: 36 additions & 42 deletions packages/typebox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,28 @@ export function sortDefinition<T extends TObject>(schema: T) {
*/
export const queryProperty = <T extends TSchema, X extends { [key: string]: TSchema }>(
def: T,
extension: X = {} as X
extension: X
) =>
Type.Optional(
Type.Union([
def,
Type.Partial(
Type.Intersect(
[
Type.Object({
$gt: def,
$gte: def,
$lt: def,
$lte: def,
$ne: def,
$in: def.type === 'array' ? def : Type.Array(def),
$nin: def.type === 'array' ? def : Type.Array(def)
}),
Type.Object(extension)
],
{ additionalProperties: false }
)
Type.Union([
def,
Type.Partial(
Type.Intersect(
[
Type.Object({
$gt: def,
$gte: def,
$lt: def,
$lte: def,
$ne: def,
$in: Type.Array(def),
$nin: Type.Array(def)
}),
Type.Object((extension || {}) as X)
],
{ additionalProperties: false }
)
])
)
)
])

type QueryProperty<T extends TSchema, X extends { [key: string]: TSchema }> = ReturnType<
typeof queryProperty<T, X>
Expand All @@ -138,7 +136,7 @@ export const queryProperties = <
X extends { [K in keyof T['properties']]?: { [key: string]: TSchema } }
>(
definition: T,
extensions: X = {} as X
extensions: X
) => {
const properties = Object.keys(definition.properties).reduce(
(res, key) => {
Expand All @@ -160,7 +158,7 @@ export const queryProperties = <
* and `$sort` and `$select` for the allowed properties.
*
* @param type The properties to create the query syntax for
* @param extensions Additional properties to add to the query syntax
* @param extensions Additional properties to add to the query syntax, use `{}` if none
* @param options Options for the TypeBox object schema
* @returns A TypeBox object representing the complete Feathers query syntax for the given properties
*/
Expand All @@ -169,30 +167,26 @@ export const querySyntax = <
X extends { [K in keyof T['properties']]?: { [key: string]: TSchema } }
>(
type: T,
extensions: X = {} as X,
extensions: X,
options: ObjectOptions = { additionalProperties: false }
) => {
const propertySchema = queryProperties(type, extensions)
const $or = Type.Array(propertySchema)
const $and = Type.Array(Type.Union([propertySchema, Type.Object({ $or })]))

return Type.Intersect(
[
Type.Partial(
Type.Object(
{
$limit: Type.Number({ minimum: 0 }),
$skip: Type.Number({ minimum: 0 }),
$sort: sortDefinition(type),
$select: arrayOfKeys(type),
$and,
$or
},
{ additionalProperties: false }
)
),
propertySchema
],
return Type.Partial(
Type.Object(
{
$limit: Type.Number({ minimum: 0 }),
$skip: Type.Number({ minimum: 0 }),
$sort: sortDefinition(type),
$select: arrayOfKeys(type),
$and,
$or,
...propertySchema.properties
},
{ additionalProperties: false }
),
options
)
}
Expand Down
27 changes: 3 additions & 24 deletions packages/typebox/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('@feathersjs/schema/typebox', () => {
name: Type.String(),
age: Type.Number()
})
const querySchema = querySyntax(schema)
const querySchema = querySyntax(schema, {})

type Query = Static<typeof querySchema>

Expand All @@ -41,7 +41,7 @@ describe('@feathersjs/schema/typebox', () => {
})

it('querySyntax works with no properties', async () => {
const schema = querySyntax(Type.Object({}))
const schema = querySyntax(Type.Object({}), {})

new Ajv().compile(schema)
})
Expand Down Expand Up @@ -80,29 +80,8 @@ describe('@feathersjs/schema/typebox', () => {
})
})

it('$in and $nin works with array type', async () => {
const schema = Type.Object({
things: Type.Array(Type.Number())
})
const querySchema = querySyntax(schema)
const validator = new Ajv().compile(querySchema)

type Query = Static<typeof querySchema>

const query: Query = {
things: {
$in: [10, 20],
$nin: [30]
}
}

const validated = (await validator(query)) as any as Query

assert.ok(validated)
})

it('defaultAppConfiguration', async () => {
const configSchema = Type.Intersect([
const configSchema = Type.Composite([
defaultAppConfiguration,
Type.Object({
host: Type.String(),
Expand Down