import { ParseResult, Schema as S } from "effect";
export const DumbInputSchema = S.Struct({
id: S.String,
name: S.String,
value: S.String,
lastModified: S.Date,
});
export const DumbOutputSchema = S.Struct({
id: S.String,
name: S.String,
value: S.String,
});
export const inputToOutput = S.transformOrFail(
S.Struct({
...DumbInputSchema.fields,
/**
* without this, the transform fails with:
* Types of property 'lastModified' are incompatible.
Type 'Date' is not assignable to type 'string'.ts(2345)
*/
lastModified: S.DateFromSelf,
}),
DumbOutputSchema,
{
decode: (input) => {
return ParseResult.succeed({
...input,
});
},
encode: (output, _, ast) => {
return ParseResult.fail(
new ParseResult.Forbidden(ast, output, "encoding not supported"),
);
},
},
);
export const decodeInputToOutput = S.decodeSync(inputToOutput);
const input = DumbInputSchema.make({
id: "test-id",
name: "Test Name",
value: "Test Value",
lastModified: new Date("2024-01-01T00:00:00.000Z"),
});
const output = decodeInputToOutput(input);
import { ParseResult, Schema as S } from "effect";
export const DumbInputSchema = S.Struct({
id: S.String,
name: S.String,
value: S.String,
lastModified: S.Date,
});
export const DumbOutputSchema = S.Struct({
id: S.String,
name: S.String,
value: S.String,
});
export const inputToOutput = S.transformOrFail(
S.Struct({
...DumbInputSchema.fields,
/**
* without this, the transform fails with:
* Types of property 'lastModified' are incompatible.
Type 'Date' is not assignable to type 'string'.ts(2345)
*/
lastModified: S.DateFromSelf,
}),
DumbOutputSchema,
{
decode: (input) => {
return ParseResult.succeed({
...input,
});
},
encode: (output, _, ast) => {
return ParseResult.fail(
new ParseResult.Forbidden(ast, output, "encoding not supported"),
);
},
},
);
export const decodeInputToOutput = S.decodeSync(inputToOutput);
const input = DumbInputSchema.make({
id: "test-id",
name: "Test Name",
value: "Test Value",
lastModified: new Date("2024-01-01T00:00:00.000Z"),
});
const output = decodeInputToOutput(input);