E5F1 fix(typebox): Revert to TypeBox 0.25 by daffl · Pull Request #3183 · feathersjs/feathers · GitHub
[go: up one dir, main page]

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
10 changes: 0 additions & 10 deletions docs/api/schema/typebox.md
Original file line number Diff line number Diff line change
Expand Up @@ -1004,16 +1004,6 @@ const T = {
}
```

<BlockQuote type="warning" label="note">

TypeBox 0.26.0 introduced a breaking change in `Type.Intersect`. `@feathersjs/typebox` maintains the original behaviour which is now available in TypeBox as `Type.Composite`. It you want to use the new `Type.Intersect` use

```ts
import { Intersect } from '@feathersjs/typebox'
```

</BlockQuote>

##### Never

Creates a type that will never validate if the attribute is present. This is useful if you are allowing [additionalProperties](#additionalproperties) but need to prevent using specific keys.
Expand Down
2,523 changes: 986 additions & 1,537 deletions docs/package-lock.json

Large diffs are not rendered by default.

45,702 changes: 33,395 additions & 12,307 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"scripts": {
"install": "lerna bootstrap",
"publish": "lerna publish && git commit -am \"chore: Update changelog\" && git push origin",
"publish": "lerna publish --force-publish && git commit -am \"chore: Update changelog\" && git push origin",
"publish:major": "lerna publish major && git commit -am \"chore: Update changelog\" && git push origin",
"publish:minor": "lerna publish minor && git commit -am \"chore: Update changelog\" && git push origin",
"publish:patch": "lerna publish patch && git commit -am \"chore: Update changelog\" && git push origin",
Expand Down
4 changes: 1 addition & 3 deletions packages/adapter-tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export const adapterTests = (testNames: AdapterTestName[]) => {
after(() => {
testNames.forEach((name) => {
if (!allTests.includes(name)) {
console.error(
`WARNING: '${name}' test is not part of the test suite`
)
console.error(`WARNING: '${name}' test is not part of the test suite`)
}
})
if (skippedTests.length) {
Expand Down
2 changes: 1 addition & 1 deletion packages/typebox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"@feathersjs/schema": "^5.0.4",
"@sinclair/typebox": "^0.26.8"
"@sinclair/typebox": "^0.25.0"
},
"devDependencies": {
"@types/mocha": "^10.0.1",
Expand Down
74 changes: 8 additions & 66 deletions packages/typebox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,9 @@
import {
TObject,
TInteger,
TOptional,
TSchema,
TIntersect,
ObjectOptions,
ExtendedTypeBuilder,
SchemaOptions,
TNever,
IntersectOptions,
TypeGuard,
Kind
} from '@sinclair/typebox'
import { Type, TObject, TInteger, TOptional, TSchema, ObjectOptions } from '@sinclair/typebox'
import { jsonSchema, Validator, DataValidatorMap, Ajv } from '@feathersjs/schema'

export * from '@sinclair/typebox'
export * from './default-schemas'

/**
* Feathers TypeBox customisations. Implements the 0.25.0 fallback for Intersect types.
* @see https://github.com/sinclairzx81/typebox/issues/373
*/
export class FeathersTypeBuilder extends ExtendedTypeBuilder {
/** `[Standard]` Creates a Intersect type */
public Intersect(allOf: [], options?: SchemaOptions): TNever
/** `[Standard]` Creates a Intersect type */
public Intersect<T extends [TObject]>(allOf: [...T], options?: SchemaOptions): T[0]
// /** `[Standard]` Creates a Intersect type */
public Intersect<T extends TObject[]>(allOf: [...T], options?: IntersectOptions): TIntersect<T>
public Intersect(allOf: TObject[], options: IntersectOptions = {}) {
const [required, optional] = [new Set<string>(), new Set<string>()]
for (const object of allOf) {
for (const [key, property] of Object.entries(object.properties)) {
if (TypeGuard.TOptional(property) || TypeGuard.TReadonlyOptional(property)) optional.add(key)
}
}
for (const object of allOf) {
for (const key of Object.keys(object.properties)) {
if (!optional.has(key)) required.add(key)
}
}
const properties = {} as Record<string, any>
for (const object of allOf) {
for (const [key, schema] of Object.entries(object.properties)) {
properties[key] =
properties[key] === undefined
? schema
: { [Kind]: 'Union', anyOf: [properties[key], { ...schema }] }
}
}
if (required.size > 0) {
return { ...options, [Kind]: 'Object', type: 'object', properties, required: [...required] } as any
} else {
return { ...options, [Kind]: 'Object', type: 'object', properties } as any
}
}
}

/**
* Exports our own type builder
*/
export const Type = new FeathersTypeBuilder()

export type TDataSchemaMap = {
create: TObject
update?: TObject
Expand All @@ -75,7 +17,7 @@ export type TDataSchemaMap = {
* @param validator The AJV validation instance
* @returns A compiled validation function
*/
export const getValidator = <T = any, R = T>(schema: TObject | TIntersect, validator: Ajv): Validator<T, R> =>
export const getValidator = <T = any, R = T>(schema: TObject, validator: Ajv): Validator<T, R> =>
jsonSchema.getValidator(schema as any, validator)

/**
Expand All @@ -100,7 +42,7 @@ export function StringEnum<T extends string[]>(allowedValues: [...T]) {
return Type.Unsafe<T[number]>({ type: 'string', enum: allowedValues })
}

const arrayOfKeys = <T extends TObject | TIntersect>(type: T) => {
const arrayOfKeys = <T extends TObject>(type: T) => {
const keys = Object.keys(type.properties)
return Type.Unsafe<(keyof T['properties'])[]>({
type: 'array',
Expand All @@ -118,7 +60,7 @@ const arrayOfKeys = <T extends TObject | TIntersect>(type: T) => {
* @param schema The TypeBox object schema
* @returns The `$sort` syntax schema
*/
export function sortDefinition<T extends TObject | TIntersect>(schema: T) {
export function sortDefinition<T extends TObject>(schema: T) {
const properties = Object.keys(schema.properties).reduce((res, key) => {
const result = res CE7 as any

Expand Down Expand Up @@ -146,7 +88,7 @@ export const queryProperty = <T extends TSchema, X extends { [key: string]: TSch
Type.Union([
def,
Type.Partial(
Type.Composite(
Type.Intersect(
[
Type.Object({
$gt: def,
Expand Down Expand Up @@ -177,7 +119,7 @@ type QueryProperty<T extends TSchema, X extends { [key: string]: TSchema }> = Re
* @returns The Feathers query syntax schema
*/
export const queryProperties = <
T extends TObject | TIntersect,
T extends TObject,
X extends { [K in keyof T['properties']]?: { [key: string]: TSchema } }
>(
definition: T,
Expand Down Expand Up @@ -205,7 +147,7 @@ export const queryProperties = <
* @returns A TypeBox object representing the complete Feathers query syntax for the given properties
*/
export const querySyntax = <
T extends TObject | TIntersect,
T extends TObject,
X extends { [K in keyof T['properties']]?: { [key: string]: TSchema } }
>(
type: T,
Expand All @@ -216,7 +158,7 @@ export const querySyntax = <
const $or = Type.Array(propertySchema)
const $and = Type.Array(Type.Union([propertySchema, Type.Object({ $or })]))

return Type.Composite(
return Type.Intersect(
[
Type.Partial(
Type.Object(
Expand Down
5 changes: 0 additions & 5 deletions packages/typebox/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
getValidator,
ObjectIdSchema
} from '../src'
import { FeathersTypeBuilder } from '../src'

describe('@feathersjs/schema/typebox', () => {
describe('querySyntax', () => {
Expand Down Expand Up @@ -101,10 +100,6 @@ describe('@feathersjs/schema/typebox', () => {
assert.ok(validated)
})

it('exports custom type builder', () => {
assert.ok(Type instanceof FeathersTypeBuilder)
})

// Test ObjectId validation
it('ObjectId', async () => {
const schema = Type.Object({
Expand Down
0