Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 8 additions & 2 deletions __tests__/invalid-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,10 @@ describe('cancels invalid request', () => {
totalErrors: 2,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
firstValidationErrors: expect.arrayContaining([
{ path: '/mustSendThings', message: 'Required property' },
{
path: '/mustSendThings',
message: 'Expected required property',
},
{ path: '/mustSendThings', message: 'Expected string' },
]),
},
Expand Down Expand Up @@ -595,7 +598,10 @@ describe('cancels invalid request', () => {
totalErrors: 2,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
firstValidationErrors: expect.arrayContaining([
{ path: '/newRequiredField', message: 'Required property' },
{
path: '/newRequiredField',
message: 'Expected required property',
},
{ path: '/newRequiredField', message: 'Expected string' },
]),
},
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
"peerDependencies": {
"@opentelemetry/api": "^1.7.0",
"@sinclair/typebox": "~0.32.8 || ~0.34.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add these peers to dev dependencies since they're used in tests..etc

"@sinclair/typebox": "~0.34.0"
},
"devDependencies": {
"@opentelemetry/context-async-hooks": "^1.26.0",
Expand Down
35 changes: 23 additions & 12 deletions router/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ export interface SerializedServerSchemaProtocolv1 {
services: Record<string, SerializedServiceSchemaProtocolv1>;
}

export type TStrict<T extends TSchema> = T;

/**
* Omits compositing symbols from this schema.
* The same approach that was previously used in the deprecated Type.Strict function.
* https://github.com/sinclairzx81/typebox/blob/5a5431439f7d5ca6b494d0d18fbfd7b1a356d67c/changelog/0.34.0.md#strict
*/
export function Strict<T extends TSchema>(schema: T): TStrict<T> {
return JSON.parse(JSON.stringify(schema)) as TStrict<T>;
}

// TODO remove once clients migrate to v2
/**
* Same as {@link serializeSchema} but with a format that is compatible with
Expand All @@ -183,7 +194,7 @@ export function serializeSchemaV1Compat(
};

if (handshakeSchema) {
schema.handshakeSchema = Type.Strict(handshakeSchema);
schema.handshakeSchema = Strict(handshakeSchema);
}

return schema;
Expand Down Expand Up @@ -226,7 +237,7 @@ export function serializeSchema(
};

if (handshakeSchema) {
schema.handshakeSchema = Type.Strict(handshakeSchema);
schema.handshakeSchema = Strict(handshakeSchema);
}

return schema;
Expand Down Expand Up @@ -439,8 +450,8 @@ export class ServiceSchema<
Object.entries(this.procedures).map(([procName, procDef]) => [
procName,
{
init: Type.Strict(procDef.requestInit),
output: Type.Strict(procDef.responseData),
init: Strict(procDef.requestInit),
output: Strict(procDef.responseData),
errors: getSerializedProcErrors(procDef),
// Only add `description` field if the type declares it.
...('description' in procDef
Expand All @@ -450,7 +461,7 @@ export class ServiceSchema<
// Only add the `input` field if the type declares it.
...('requestData' in procDef
? {
input: Type.Strict(procDef.requestData),
input: Strict(procDef.requestData),
}
: {}),
},
Expand Down Expand Up @@ -479,8 +490,8 @@ export class ServiceSchema<
{
// BACKWARDS COMPAT: map init to input for protocolv1
// this is the only change needed to make it compatible.
input: Type.Strict(procDef.requestInit),
output: Type.Strict(procDef.responseData),
input: Strict(procDef.requestInit),
output: Strict(procDef.responseData),
errors: getSerializedProcErrors(procDef),
// Only add `description` field if the type declares it.
...('description' in procDef
Expand All @@ -496,15 +507,15 @@ export class ServiceSchema<
return [
procName,
{
init: Type.Strict(procDef.requestInit),
output: Type.Strict(procDef.responseData),
init: Strict(procDef.requestInit),
output: Strict(procDef.responseData),
errors: getSerializedProcErrors(procDef),
// Only add `description` field if the type declares it.
...('description' in procDef
? { description: procDef.description }
: {}),
type: procDef.type,
input: Type.Strict(procDef.requestData),
input: Strict(procDef.requestData),
},
];
},
Expand Down Expand Up @@ -541,14 +552,14 @@ function getSerializedProcErrors(
!('responseError' in procDef) ||
procDef.responseError[Kind] === 'Never'
) {
return Type.Strict(ReaderErrorSchema);
return Strict(ReaderErrorSchema);
}

const withProtocolErrors = flattenErrorType(
Type.Union([procDef.responseError, ReaderErrorSchema]),
);

return Type.Strict(withProtocolErrors);
return Strict(withProtocolErrors);
}

/**
Expand Down