Type.Ref does not work with Type.Codec
Version: 1.1.12
From my understanding `schema3` and `schema4` should output the same result.
## Example Code
```ts
import { Type } from "typebox";
import { Encode } from "typebox/value";
const schema1 = Type.Codec(Type.String()).Decode(v => Number.parseInt(v)).Encode(v => v.toString());
const schema2 = Type.Codec(Type.Object({ a: Type.Ref("schema1") })).Decode(v => v.a).Encode(a => ({ a }));
const schema3 = Type.Codec(schema2).Decode(v => v - 1).Encode(v => v + 1);
const schema4 = Type.Codec(Type.Ref("schema2")).Decode(v => v - 1).Encode(v => v + 1);
const ctx = { schema1, schema2, schema3, schema4 };
console.log(`schema1: ${JSON.stringify(Encode(ctx, schema1, 1), null, 2)}`);
console.log(`schema2: ${JSON.stringify(Encode(ctx, schema2, 1), null, 2)}`);
console.log(`schema3: ${JSON.stringify(Encode(ctx, schema3, 1), null, 2)}`);
console.log(`schema4: ${JSON.stringify(Encode(ctx, schema4, 1), null, 2)}`);
```
## Output
```
> @metabyte/api@1.0.0 a C:\Users\micro\project\metabyte2
> node a.js
schema1: "1"
schema2: {
"a": "1"
}
schema3: {
"a": "2"
}
file:///C:/Users/user/project/node_modules/.pnpm/typebox@1.1.12/node_modules/typebox/build/value/codec/encode.mjs:22
throw new EncodeError(value, Errors(context, type, value));
^
EncodeError: Encode
at Assert (file:///C:/Users/user/project/node_modules/.pnpm/typebox@1.1.12/node_modules/typebox/build/value/codec/encode.mjs:22:15)
at file:///C:/Users/user/project/node_modules/.pnpm/typebox@1.1.12/node_modules/typebox/build/value/codec/encode.mjs:41:31
at file:///C:/Users/user/project/node_modules/.pnpm/typebox@1.1.12/node_modules/typebox/build/value/pipeline/pipeline.mjs:13:50
at Array.reduce (<anonymous>)
at file:///C:/Users/user/project/node_modules/.pnpm/typebox@1.1.12/node_modules/typebox/build/value/pipeline/pipeline.mjs:13:25
at Encode (file:///C:/Users/user/project/node_modules/.pnpm/typebox@1.1.12/node_modules/typebox/build/value/codec/encode.mjs:49:12)
at file:///C:/Users/user/project/a.js:14:40
at ModuleJob.run (node:internal/modules/esm/module_job:430:25)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:661:26)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5) {
[cause]: {
source: 'Encode',
errors: [
{
keyword: 'type',
schemaPath: '#',
instancePath: '',
params: { type: 'object' },
message: 'must be object'
}
],
value: '[object Object]1'
}
}
```
If I use `EncodeUnsafe`, it outputs the following.
```
schema1: "1"
schema2: {
"a": "1"
}
schema3: {
"a": "2"
}
schema4: "[object Object]1"
```
0 条评论