diff --git a/packages/effect/src/schema/AST.ts b/packages/effect/src/schema/AST.ts
index 7e56fc26c0..3f39b46ad1 100644
--- a/packages/effect/src/schema/AST.ts
+++ b/packages/effect/src/schema/AST.ts
@@ -386,7 +386,7 @@ export class Declaration extends Base {
return replaceEncoding(ast, [new Link(to, link.transformation)])
}
} else {
- return requiredDefaultJsonAnnotation(ast)
+ return requiredDefaultJsonSerializerAnnotation(ast)
}
}
/** @internal */
@@ -413,7 +413,7 @@ export class NullKeyword extends AbstractParser {
}
/** @internal */
goStringLeafJson() {
- return coerceNull(this)
+ return replaceEncoding(this, [nullLink])
}
/** @internal */
getExpected(): string {
@@ -438,15 +438,12 @@ export class UndefinedKeyword extends AbstractParser {
}
/** @internal */
goJson() {
- // TODO: Should undefined be optional by default?
- return UndefinedKeyword.optional
+ return coerceUndefined(this)
}
/** @internal */
getExpected(): string {
return "undefined"
}
- /** @internal */
- static optional = new UndefinedKeyword(undefined, undefined, undefined, new Context(true, false))
}
/**
@@ -466,15 +463,12 @@ export class VoidKeyword extends AbstractParser {
}
/** @internal */
goJson() {
- // TODO: Should undefined be optional by default?
- return VoidKeyword.optional
+ return coerceUndefined(this)
}
/** @internal */
getExpected(): string {
return "void"
}
- /** @internal */
- static optional = new VoidKeyword(undefined, undefined, undefined, new Context(true, false))
}
/**
@@ -493,10 +487,6 @@ export class NeverKeyword extends AbstractParser {
return fromRefinement(this, Predicate.isNever)
}
/** @internal */
- goJson() {
- return this
- }
- /** @internal */
getExpected(): string {
return "never"
}
@@ -763,8 +753,8 @@ function coerceLiteral(ast: LiteralType): LiteralType {
new Link(
new LiteralType(s),
new Transformation.Transformation(
- Getter.succeed(ast.literal),
- Getter.succeed(s)
+ Getter.map(() => ast.literal),
+ Getter.map(() => s)
)
)
])
@@ -859,7 +849,7 @@ export class NumberKeyword extends AbstractParser {
}
/** @internal */
goStringLeafJson() {
- return coerceNumber(this)
+ return replaceEncoding(this, [numberLink])
}
/** @internal */
getExpected(): string {
@@ -888,7 +878,7 @@ export class BooleanKeyword extends AbstractParser {
}
/** @internal */
goStringLeafJson() {
- return coerceBoolean(this)
+ return replaceEncoding(this, [booleanLink])
}
/** @internal */
getExpected(): string {
@@ -2365,20 +2355,14 @@ export function getReducer(alg: ReducerAlg) {
}
}
-const stringNull = new LiteralType("")
-
const nullLink = new Link(
- stringNull,
- Transformation.transform({
- decode: () => null,
- encode: () => stringNull.literal
- })
+ undefinedKeyword,
+ new Transformation.Transformation(
+ Getter.map(() => null),
+ Getter.map(() => undefined)
+ )
)
-function coerceNull(ast: NullKeyword): NullKeyword {
- return replaceEncoding(ast, [nullLink])
-}
-
const numberKeywordPattern = appendChecks(stringKeyword, [
Check.regex(new RegExp(NUMBER_KEYWORD_PATTERN), { title: "a string representing a number" })
])
@@ -2388,10 +2372,6 @@ const numberLink = new Link(
Transformation.numberFromString
)
-function coerceNumber(ast: NumberKeyword): NumberKeyword {
- return replaceEncoding(ast, [numberLink])
-}
-
const booleanLink = new Link(
new UnionType([new LiteralType("true"), new LiteralType("false")], "anyOf"),
new Transformation.Transformation(
@@ -2400,10 +2380,6 @@ const booleanLink = new Link(
)
)
-function coerceBoolean(ast: BooleanKeyword): BooleanKeyword {
- return replaceEncoding(ast, [booleanLink])
-}
-
const bigintKeywordPattern = appendChecks(stringKeyword, [
Check.regex(new RegExp(BIGINT_KEYWORD_PATTERN), { title: "a string representing a bigint" })
])
@@ -2432,8 +2408,8 @@ export const goStringLeafJson = memoize((ast: AST): AST => {
}
return replaceEncoding(ast, Arr.append(links.slice(0, links.length - 1), new Link(to, last.transformation)))
}
- const out: any = ast
- return out.goStringLeafJson?.() ?? out.go?.(goStringLeafJson) ?? out
+ const out = (ast as any).goStringLeafJson?.() ?? (ast as any).go?.(goStringLeafJson) ?? ast
+ return isOptional(ast) ? optionalKey(out) : out
})
/** @internal */
@@ -2449,8 +2425,8 @@ export function forbidden(ast: A, message: string): A {
}
/** @internal */
-export function requiredDefaultJsonAnnotation(ast: AST): AST {
- return forbidden(ast, "cannot serialize to JSON, required `defaultJsonSerializer` annotation")
+export function requiredDefaultJsonSerializerAnnotation(ast: AST): AST {
+ return forbidden(ast, `cannot serialize to JSON, required \`defaultJsonSerializer\` annotation for ${ast._tag}`)
}
const SYMBOL_PATTERN = /^Symbol\((.*)\)$/
@@ -2481,6 +2457,18 @@ function coerceSymbol(ast: A): A {
return replaceEncoding(ast, [symbolLink])
}
+const undefinedLink = new Link(
+ nullKeyword,
+ new Transformation.Transformation(
+ Getter.map(() => undefined),
+ Getter.map(() => null)
+ )
+)
+
+function coerceUndefined(ast: A): A {
+ return replaceEncoding(ast, [undefinedLink])
+}
+
/** @internal */
export const getExpected = memoize((ast: AST): string => {
return getIdAnnotation(ast) ?? ast.getExpected(getExpected)
diff --git a/packages/effect/src/schema/Serializer.ts b/packages/effect/src/schema/Serializer.ts
index 4626051dbc..c4a018fa84 100644
--- a/packages/effect/src/schema/Serializer.ts
+++ b/packages/effect/src/schema/Serializer.ts
@@ -36,8 +36,9 @@ const goJson = AST.memoize((ast: AST.AST): AST.AST => {
if (AST.isTypeLiteral(ast) && ast.propertySignatures.some((ps) => !Predicate.isString(ps.name))) {
return AST.forbidden(ast, "cannot serialize to JSON, property names must be strings")
}
- const out: any = ast
- return out.goJson?.(goJson, Schema.make) ?? out.go?.(goJson) ?? AST.requiredDefaultJsonAnnotation(ast)
+ const out = (ast as any).goJson?.(goJson, Schema.make) ?? (ast as any).go?.(goJson) ??
+ AST.requiredDefaultJsonSerializerAnnotation(ast)
+ return AST.isOptional(ast) ? AST.optionalKey(out) : out
})
/**
diff --git a/packages/effect/test/schema/Serializer.test.ts b/packages/effect/test/schema/Serializer.test.ts
index e95acbc968..6c7f0fde3a 100644
--- a/packages/effect/test/schema/Serializer.test.ts
+++ b/packages/effect/test/schema/Serializer.test.ts
@@ -18,6 +18,57 @@ const FiniteFromDate = Schema.Date.pipe(Schema.decodeTo(
describe("Serializer", () => {
describe("json", () => {
describe("default serialization", () => {
+ describe("Unsupported schemas", () => {
+ it("Declaration without defaultJsonSerializer annotation", async () => {
+ class A {
+ readonly _tag = "A"
+ }
+
+ await assertions.serialization.json.typeCodec.fail(
+ Schema.declare((u): u is A => u instanceof A),
+ new A(),
+ `cannot serialize to JSON, required \`defaultJsonSerializer\` annotation for Declaration`
+ )
+ })
+
+ it("Any", async () => {
+ await assertions.serialization.json.typeCodec.fail(
+ Schema.Any,
+ "a",
+ `cannot serialize to JSON, required \`defaultJsonSerializer\` annotation for AnyKeyword`
+ )
+ })
+
+ it("Unknown", async () => {
+ await assertions.serialization.json.typeCodec.fail(
+ Schema.Unknown,
+ "a",
+ `cannot serialize to JSON, required \`defaultJsonSerializer\` annotation for UnknownKeyword`
+ )
+ })
+
+ it("Object", async () => {
+ await assertions.serialization.json.typeCodec.fail(
+ Schema.Object,
+ {},
+ `cannot serialize to JSON, required \`defaultJsonSerializer\` annotation for ObjectKeyword`
+ )
+ })
+
+ it("Struct with Symbol property name", async () => {
+ const a = Symbol.for("a")
+ const schema = Schema.Struct({
+ [a]: Schema.String
+ })
+
+ await assertions.serialization.json.typeCodec.fail(
+ schema,
+ { [a]: "b" },
+ "cannot serialize to JSON, property names must be strings"
+ )
+ })
+ })
+
describe("should return the same reference if nothing changed", () => {
it("Struct", async () => {
const schema = Schema.Struct({
@@ -53,7 +104,7 @@ describe("Serializer", () => {
})
})
- it("should apply the construction process to the provided link in the annotations", async () => {
+ it("should apply the construction process to the provided link in the defaultJsonSerializer annotation", async () => {
const schema = Schema.Struct({
a: Schema.Date.annotate({
defaultJsonSerializer: () =>
@@ -70,10 +121,91 @@ describe("Serializer", () => {
})
})
+ describe("instanceOf with defaultJsonSerializer annotation", () => {
+ it("arg: message: string", async () => {
+ class MyError extends Error {
+ constructor(message?: string) {
+ super(message)
+ this.name = "MyError"
+ Object.setPrototypeOf(this, MyError.prototype)
+ }
+ }
+
+ const schema = Schema.instanceOf(
+ MyError,
+ {
+ title: "MyError",
+ defaultJsonSerializer: () =>
+ Schema.link()(
+ Schema.String,
+ Transformation.transform({
+ decode: (message) => new MyError(message),
+ encode: (e) => e.message
+ })
+ )
+ }
+ )
+
+ await assertions.serialization.json.typeCodec.succeed(schema, new MyError("a"), "a")
+ await assertions.deserialization.json.typeCodec.succeed(schema, "a", new MyError("a"))
+ })
+
+ it("arg: struct", async () => {
+ class MyError extends Error {
+ static Props = Schema.Struct({
+ message: Schema.String,
+ cause: Schema.String
+ })
+
+ constructor(props: typeof MyError.Props["Type"]) {
+ super(props.message, { cause: props.cause })
+ this.name = "MyError"
+ Object.setPrototypeOf(this, MyError.prototype)
+ }
+
+ static schema = Schema.instanceOf(
+ MyError,
+ {
+ title: "MyError",
+ defaultJsonSerializer: () =>
+ Schema.link()(
+ MyError.Props,
+ Transformation.transform({
+ decode: (props) => new MyError(props),
+ encode: (e) => ({
+ message: e.message,
+ cause: typeof e.cause === "string" ? e.cause : String(e.cause)
+ })
+ })
+ )
+ }
+ )
+ }
+
+ const schema = MyError.schema
+
+ await assertions.serialization.json.typeCodec.succeed(schema, new MyError({ message: "a", cause: "b" }), {
+ message: "a",
+ cause: "b"
+ })
+ await assertions.deserialization.json.typeCodec.succeed(
+ schema,
+ { message: "a", cause: "b" },
+ new MyError({ message: "a", cause: "b" })
+ )
+ })
+ })
+
it("Undefined", async () => {
const schema = Schema.Undefined
- await assertions.serialization.json.typeCodec.succeed(schema, undefined)
+ await assertions.serialization.json.typeCodec.succeed(schema, undefined, null)
+ })
+
+ it("Void", async () => {
+ const schema = Schema.Void
+
+ await assertions.serialization.json.typeCodec.succeed(schema, undefined, null)
})
it("Null", async () => {
@@ -86,6 +218,21 @@ describe("Serializer", () => {
const schema = Schema.String
await assertions.serialization.json.typeCodec.succeed(schema, "a")
+ await assertions.deserialization.json.typeCodec.succeed(schema, "a", "a")
+ })
+
+ it("Number", async () => {
+ const schema = Schema.Number
+
+ await assertions.serialization.json.typeCodec.succeed(schema, 1)
+ await assertions.deserialization.json.typeCodec.succeed(schema, 1, 1)
+ })
+
+ it("Boolean", async () => {
+ const schema = Schema.Boolean
+
+ await assertions.serialization.json.typeCodec.succeed(schema, true)
+ await assertions.deserialization.json.typeCodec.succeed(schema, true)
})
it("Symbol", async () => {
@@ -154,6 +301,15 @@ describe("Serializer", () => {
})
})
+ it("Literals", async () => {
+ const schema = Schema.Literals(["a", 1, 2n, true])
+ await assertions.deserialization.json.typeCodec.fail(
+ schema,
+ "-",
+ `Expected "a" | 1 | "2" | true, got "-"`
+ )
+ })
+
describe("TemplateLiteral", () => {
it("1n + string", async () => {
const schema = Schema.TemplateLiteral([1n, Schema.String])
@@ -170,43 +326,302 @@ describe("Serializer", () => {
})
})
- it("URL", async () => {
- const schema = Schema.URL
+ it("Enums", async () => {
+ enum Fruits {
+ Apple,
+ Banana
+ }
+ const schema = Schema.Enums(Fruits)
+
+ await assertions.serialization.json.typeCodec.succeed(schema, Fruits.Apple, 0)
+ await assertions.serialization.json.typeCodec.succeed(schema, Fruits.Banana, 1)
+ await assertions.deserialization.json.typeCodec.succeed(schema, 0, Fruits.Apple)
+ await assertions.deserialization.json.typeCodec.succeed(schema, 1, Fruits.Banana)
+ })
+
+ describe("Struct", () => {
+ it("Date", async () => {
+ const schema = Schema.Struct({
+ a: Schema.Date
+ })
+
+ await assertions.serialization.json.typeCodec.succeed(
+ schema,
+ { a: new Date("2021-01-01") },
+ { a: "2021-01-01T00:00:00.000Z" }
+ )
+ })
+
+ it("UndefinedOr(Date)", async () => {
+ const schema = Schema.Struct({
+ a: Schema.UndefinedOr(Schema.Date)
+ })
+
+ await assertions.serialization.json.typeCodec.succeed(schema, { a: new Date("2021-01-01") }, {
+ a: "2021-01-01T00:00:00.000Z"
+ })
+ await assertions.serialization.json.typeCodec.succeed(schema, { a: undefined }, { a: null })
+
+ await assertions.deserialization.json.typeCodec.succeed(schema, { a: "2021-01-01T00:00:00.000Z" }, {
+ a: new Date("2021-01-01")
+ })
+ await assertions.deserialization.json.typeCodec.succeed(schema, { a: null }, { a: undefined })
+ })
+
+ it("NullOr(Date)", async () => {
+ const schema = Schema.Struct({
+ a: Schema.NullOr(Schema.Date)
+ })
+
+ await assertions.serialization.json.typeCodec.succeed(schema, { a: new Date("2021-01-01") }, {
+ a: "2021-01-01T00:00:00.000Z"
+ })
+ await assertions.serialization.json.typeCodec.succeed(schema, { a: null }, { a: null })
+
+ await assertions.deserialization.json.typeCodec.succeed(schema, { a: "2021-01-01T00:00:00.000Z" }, {
+ a: new Date("2021-01-01")
+ })
+ await assertions.deserialization.json.typeCodec.succeed(schema, { a: null }, { a: null })
+ })
+
+ it("optionalKey(Date)", async () => {
+ const schema = Schema.Struct({
+ a: Schema.optionalKey(Schema.Date)
+ })
+
+ await assertions.serialization.json.typeCodec.succeed(schema, { a: new Date("2021-01-01") }, {
+ a: "2021-01-01T00:00:00.000Z"
+ })
+ await assertions.serialization.json.typeCodec.succeed(schema, {}, {})
+
+ await assertions.deserialization.json.typeCodec.succeed(schema, { a: "2021-01-01T00:00:00.000Z" }, {
+ a: new Date("2021-01-01")
+ })
+ await assertions.deserialization.json.typeCodec.succeed(schema, {}, {})
+ })
+
+ it("optional(Date)", async () => {
+ const schema = Schema.Struct({
+ a: Schema.optional(Schema.Date)
+ })
+
+ await assertions.serialization.json.typeCodec.succeed(schema, { a: new Date("2021-01-01") }, {
+ a: "2021-01-01T00:00:00.000Z"
+ })
+ await assertions.serialization.json.typeCodec.succeed(schema, {}, {})
+ await assertions.serialization.json.typeCodec.succeed(schema, { a: undefined }, { a: null })
+
+ await assertions.deserialization.json.typeCodec.succeed(schema, { a: "2021-01-01T00:00:00.000Z" }, {
+ a: new Date("2021-01-01")
+ })
+ await assertions.deserialization.json.typeCodec.succeed(schema, {}, {})
+ await assertions.deserialization.json.typeCodec.succeed(schema, { a: null }, { a: undefined })
+ })
+ })
+
+ it("Record(Symbol, Date)", async () => {
+ const schema = Schema.Record(Schema.Symbol, Schema.Date)
await assertions.serialization.json.typeCodec.succeed(
schema,
- new URL("https://example.com"),
- "https://example.com/"
+ { [Symbol.for("a")]: new Date("2021-01-01"), [Symbol.for("b")]: new Date("2021-01-01") },
+ { "Symbol(a)": "2021-01-01T00:00:00.000Z", "Symbol(b)": "2021-01-01T00:00:00.000Z" }
)
+
await assertions.deserialization.json.typeCodec.succeed(
schema,
- "https://example.com",
- new URL("https://example.com")
- )
- await assertions.deserialization.json.typeCodec.succeed(
- schema,
- "https://example.com/",
- new URL("https://example.com")
- )
- await assertions.deserialization.json.typeCodec.fail(
- schema,
- "not a url",
- isDeno ? `TypeError: Invalid URL: 'not a url'` : `TypeError: Invalid URL`
+ { "Symbol(a)": "2021-01-01T00:00:00.000Z", "Symbol(b)": "2021-01-01T00:00:00.000Z" },
+ { [Symbol.for("a")]: new Date("2021-01-01"), [Symbol.for("b")]: new Date("2021-01-01") }
)
})
- it("declareRefinement without annotation", async () => {
- class A {
- readonly _tag = "A"
- }
- const schema = Schema.declare((u): u is A => u instanceof A)
- await assertions.serialization.json.typeCodec.fail(
+ describe("Tuple", () => {
+ it("Date", async () => {
+ const schema = Schema.Tuple([Schema.Date])
+
+ await assertions.serialization.json.typeCodec.succeed(
+ schema,
+ [new Date("2021-01-01")],
+ ["2021-01-01T00:00:00.000Z"]
+ )
+ await assertions.deserialization.json.typeCodec.succeed(
+ schema,
+ ["2021-01-01T00:00:00.000Z"],
+ [new Date("2021-01-01")]
+ )
+ })
+
+ it("UndefinedOr(Date)", async () => {
+ const schema = Schema.Tuple([Schema.UndefinedOr(Schema.Date)])
+
+ await assertions.serialization.json.typeCodec.succeed(
+ schema,
+ [new Date("2021-01-01")],
+ ["2021-01-01T00:00:00.000Z"]
+ )
+ await assertions.serialization.json.typeCodec.succeed(schema, [undefined], [null])
+
+ await assertions.deserialization.json.typeCodec.succeed(schema, ["2021-01-01T00:00:00.000Z"], [
+ new Date("2021-01-01")
+ ])
+ await assertions.deserialization.json.typeCodec.succeed(schema, [null], [undefined])
+ })
+
+ it("NullOr(Date)", async () => {
+ const schema = Schema.Tuple([Schema.NullOr(Schema.Date)])
+
+ await assertions.serialization.json.typeCodec.succeed(schema, [new Date("2021-01-01")], [
+ "2021-01-01T00:00:00.000Z"
+ ])
+ await assertions.serialization.json.typeCodec.succeed(schema, [null], [null])
+
+ await assertions.deserialization.json.typeCodec.succeed(schema, ["2021-01-01T00:00:00.000Z"], [
+ new Date("2021-01-01")
+ ])
+ await assertions.deserialization.json.typeCodec.succeed(schema, [null], [null])
+ })
+
+ it("optionalKey(Date)", async () => {
+ const schema = Schema.Tuple([Schema.optionalKey(Schema.Date)])
+
+ await assertions.serialization.json.typeCodec.succeed(schema, [new Date("2021-01-01")], [
+ "2021-01-01T00:00:00.000Z"
+ ])
+ await assertions.serialization.json.typeCodec.succeed(schema, [], [])
+
+ await assertions.deserialization.json.typeCodec.succeed(schema, ["2021-01-01T00:00:00.000Z"], [
+ new Date("2021-01-01")
+ ])
+ await assertions.deserialization.json.typeCodec.succeed(schema, [], [])
+ })
+
+ it("optional(Date)", async () => {
+ const schema = Schema.Tuple([Schema.optional(Schema.Date)])
+
+ await assertions.serialization.json.typeCodec.succeed(schema, [new Date("2021-01-01")], [
+ "2021-01-01T00:00:00.000Z"
+ ])
+ await assertions.serialization.json.typeCodec.succeed(schema, [], [])
+ await assertions.serialization.json.typeCodec.succeed(schema, [undefined], [null])
+
+ await assertions.deserialization.json.typeCodec.succeed(schema, ["2021-01-01T00:00:00.000Z"], [
+ new Date("2021-01-01")
+ ])
+ await assertions.deserialization.json.typeCodec.succeed(schema, [], [])
+ await assertions.deserialization.json.typeCodec.succeed(schema, [null], [undefined])
+ })
+ })
+
+ it("Array(Date)", async () => {
+ const schema = Schema.Array(Schema.Date)
+
+ await assertions.serialization.json.typeCodec.succeed(
schema,
- new A(),
- "cannot serialize to JSON, required `defaultJsonSerializer` annotation"
+ [new Date("2021-01-01"), new Date("2021-01-02")],
+ ["2021-01-01T00:00:00.000Z", "2021-01-02T00:00:00.000Z"]
)
})
+ describe("Union", () => {
+ it("NullOr(Date)", async () => {
+ const schema = Schema.NullOr(Schema.String)
+
+ await assertions.serialization.json.typeCodec.succeed(schema, "a", "a")
+ await assertions.serialization.json.typeCodec.succeed(schema, null)
+ await assertions.deserialization.json.typeCodec.succeed(schema, null)
+ await assertions.deserialization.json.typeCodec.succeed(schema, "a", "a")
+ })
+
+ it("NullOr(Number)", async () => {
+ const schema = Schema.NullOr(Schema.Number)
+
+ await assertions.serialization.json.typeCodec.succeed(schema, 1, 1)
+ await assertions.serialization.json.typeCodec.succeed(schema, null)
+ await assertions.deserialization.json.typeCodec.succeed(schema, null)
+ await assertions.deserialization.json.typeCodec.succeed(schema, 1, 1)
+ })
+
+ it("Array(NullOr(Number))", async () => {
+ const schema = Schema.Array(Schema.NullOr(Schema.Number))
+
+ await assertions.serialization.json.typeCodec.succeed(schema, [1, null], [1, null])
+ await assertions.deserialization.json.typeCodec.succeed(schema, [1, null], [1, null])
+ })
+
+ it("Union(Schema.Date, FiniteFromDate)", async () => {
+ const schema = Schema.Union([Schema.Date, FiniteFromDate])
+
+ await assertions.serialization.json.typeCodec.succeed(
+ schema,
+ new Date("2021-01-01"),
+ "2021-01-01T00:00:00.000Z"
+ )
+ await assertions.serialization.json.typeCodec.succeed(schema, 0)
+ })
+ })
+
+ it("Suspend", async () => {
+ interface Category {
+ readonly a: A
+ readonly categories: ReadonlyArray
+ }
+ interface CategoryType extends Category {}
+ interface CategoryEncoded extends Category {}
+
+ const schema = Schema.Struct({
+ a: Schema.FiniteFromString.check(Check.greaterThan(0)),
+ categories: Schema.Array(Schema.suspend((): Schema.Codec => schema))
+ })
+
+ await assertions.serialization.json.typeCodec.succeed(schema, { a: 1, categories: [] }, {
+ a: 1,
+ categories: []
+ })
+ await assertions.serialization.json.typeCodec.succeed(schema, {
+ a: 1,
+ categories: [{ a: 2, categories: [] }]
+ }, {
+ a: 1,
+ categories: [
+ { a: 2, categories: [] }
+ ]
+ })
+ await assertions.deserialization.json.typeCodec.succeed(schema, {
+ a: 1,
+ categories: []
+ }, { a: 1, categories: [] })
+ await assertions.deserialization.json.typeCodec.succeed(schema, {
+ a: 1,
+ categories: [
+ { a: 2, categories: [] }
+ ]
+ }, { a: 1, categories: [{ a: 2, categories: [] }] })
+ })
+
+ it("FiniteFromDate", async () => {
+ const schema = FiniteFromDate
+
+ await assertions.serialization.json.typeCodec.succeed(schema, 0)
+ })
+
+ it("Class", async () => {
+ class A extends Schema.Class("A")(Schema.Struct({
+ a: FiniteFromDate
+ })) {}
+
+ await assertions.serialization.json.typeCodec.succeed(A, new A({ a: 0 }), { a: 0 })
+ await assertions.deserialization.json.typeCodec.succeed(A, { a: 0 }, new A({ a: 0 }))
+ })
+
+ it("ErrorClass", async () => {
+ class E extends Schema.ErrorClass("E")({
+ a: FiniteFromDate
+ }) {}
+
+ await assertions.serialization.json.typeCodec.succeed(E, new E({ a: 0 }), { a: 0 })
+ await assertions.deserialization.json.typeCodec.succeed(E, { a: 0 }, new E({ a: 0 }))
+ })
+
it("Date", async () => {
const schema = Schema.Date
@@ -254,6 +669,31 @@ describe("Serializer", () => {
)
})
+ it("URL", async () => {
+ const schema = Schema.URL
+
+ await assertions.serialization.json.typeCodec.succeed(
+ schema,
+ new URL("https://example.com"),
+ "https://example.com/"
+ )
+ await assertions.deserialization.json.typeCodec.succeed(
+ schema,
+ "https://example.com",
+ new URL("https://example.com")
+ )
+ await assertions.deserialization.json.typeCodec.succeed(
+ schema,
+ "https://example.com/",
+ new URL("https://example.com")
+ )
+ await assertions.deserialization.json.typeCodec.fail(
+ schema,
+ "not a url",
+ isDeno ? `TypeError: Invalid URL: 'not a url'` : `TypeError: Invalid URL`
+ )
+ })
+
it("Option(Date)", async () => {
const schema = Schema.Option(Schema.Date)
@@ -280,62 +720,6 @@ describe("Serializer", () => {
await assertions.deserialization.json.typeCodec.succeed(schema, ["a"], Redacted.make(Option.some("a")))
})
- it("Struct", async () => {
- const schema = Schema.Struct({
- a: Schema.Date,
- b: Schema.Date
- })
-
- await assertions.serialization.json.typeCodec.succeed(
- schema,
- { a: new Date("2021-01-01"), b: new Date("2021-01-01") },
- { a: "2021-01-01T00:00:00.000Z", b: "2021-01-01T00:00:00.000Z" }
- )
- })
-
- it("Record(Symbol, Date)", async () => {
- const schema = Schema.Record(Schema.Symbol, Schema.Date)
-
- await assertions.serialization.json.typeCodec.succeed(
- schema,
- { [Symbol.for("a")]: new Date("2021-01-01"), [Symbol.for("b")]: new Date("2021-01-01") },
- { "Symbol(a)": "2021-01-01T00:00:00.000Z", "Symbol(b)": "2021-01-01T00:00:00.000Z" }
- )
-
- await assertions.deserialization.json.typeCodec.succeed(
- schema,
- { "Symbol(a)": "2021-01-01T00:00:00.000Z", "Symbol(b)": "2021-01-01T00:00:00.000Z" },
- { [Symbol.for("a")]: new Date("2021-01-01"), [Symbol.for("b")]: new Date("2021-01-01") }
- )
- })
-
- it("Tuple(Date, Date)", async () => {
- const schema = Schema.Tuple([Schema.Date, Schema.Date])
-
- await assertions.serialization.json.typeCodec.succeed(
- schema,
- [new Date("2021-01-01"), new Date("2021-01-01")],
- ["2021-01-01T00:00:00.000Z", "2021-01-01T00:00:00.000Z"]
- )
- })
-
- it("FiniteFromDate", async () => {
- const schema = FiniteFromDate
-
- await assertions.serialization.json.typeCodec.succeed(schema, 0, 0)
- })
-
- it("Union(Schema.Date, Schema.Date)", async () => {
- const schema = Schema.Union([Schema.Date, FiniteFromDate])
-
- await assertions.serialization.json.typeCodec.succeed(
- schema,
- new Date("2021-01-01"),
- "2021-01-01T00:00:00.000Z"
- )
- await assertions.serialization.json.typeCodec.succeed(schema, 0, 0)
- })
-
it("Map", async () => {
const schema = Schema.Map(Schema.Option(Schema.Date), FiniteFromDate)
@@ -353,37 +737,6 @@ describe("Serializer", () => {
new Map([[Option.some(new Date("2021-01-01")), 0]])
)
})
-
- it("Class", async () => {
- class A extends Schema.Class("A")(Schema.Struct({
- a: FiniteFromDate
- })) {}
-
- await assertions.serialization.json.typeCodec.succeed(A, new A({ a: 0 }), { a: 0 })
- await assertions.deserialization.json.typeCodec.succeed(A, { a: 0 }, new A({ a: 0 }))
- })
-
- it("ErrorClass", async () => {
- class E extends Schema.ErrorClass("E")({
- a: FiniteFromDate
- }) {}
-
- await assertions.serialization.json.typeCodec.succeed(E, new E({ a: 0 }), { a: 0 })
- await assertions.deserialization.json.typeCodec.succeed(E, { a: 0 }, new E({ a: 0 }))
- })
-
- it("Enums", async () => {
- enum Fruits {
- Apple,
- Banana
- }
- const schema = Schema.Enums(Fruits)
-
- await assertions.serialization.json.typeCodec.succeed(schema, Fruits.Apple, 0)
- await assertions.serialization.json.typeCodec.succeed(schema, Fruits.Banana, 1)
- await assertions.deserialization.json.typeCodec.succeed(schema, 0, Fruits.Apple)
- await assertions.deserialization.json.typeCodec.succeed(schema, 1, Fruits.Banana)
- })
})
describe("custom serialization", () => {
@@ -416,6 +769,37 @@ describe("Serializer", () => {
)
})
+ it("Class", async () => {
+ class A extends Schema.Class("A")(Schema.Struct({
+ a: FiniteFromDate
+ })) {}
+
+ await assertions.serialization.json.codec.succeed(A, new A({ a: 0 }), { a: "1970-01-01T00:00:00.000Z" })
+ await assertions.deserialization.json.codec.succeed(A, { a: "1970-01-01T00:00:00.000Z" }, new A({ a: 0 }))
+ })
+
+ it("Error", async () => {
+ class E extends Schema.ErrorClass("E")({
+ a: FiniteFromDate
+ }) {}
+
+ await assertions.serialization.json.codec.succeed(E, new E({ a: 0 }), { a: "1970-01-01T00:00:00.000Z" })
+ await assertions.deserialization.json.codec.succeed(E, { a: "1970-01-01T00:00:00.000Z" }, new E({ a: 0 }))
+ })
+
+ it("Enums", async () => {
+ enum Fruits {
+ Apple,
+ Banana = "banana"
+ }
+ const schema = Schema.Enums(Fruits)
+
+ await assertions.serialization.json.codec.succeed(schema, Fruits.Apple, 0)
+ await assertions.serialization.json.codec.succeed(schema, Fruits.Banana, "banana")
+ await assertions.deserialization.json.codec.succeed(schema, 0, Fruits.Apple)
+ await assertions.deserialization.json.codec.succeed(schema, "banana", Fruits.Banana)
+ })
+
it("Option(Option(FiniteFromDate))", async () => {
const schema = Schema.Option(Schema.Option(FiniteFromDate))
@@ -465,117 +849,11 @@ describe("Serializer", () => {
}])
await assertions.serialization.json.codec.succeed(schema, Cause.interrupt(), [{
_tag: "Interrupt",
- fiberId: undefined
+ fiberId: null
}])
})
})
- describe("instanceOf", () => {
- it("arg: message: string", async () => {
- class MyError extends Error {
- constructor(message?: string) {
- super(message)
- this.name = "MyError"
- Object.setPrototypeOf(this, MyError.prototype)
- }
- }
-
- const schema = Schema.instanceOf(
- MyError,
- {
- title: "MyError",
- defaultJsonSerializer: () =>
- Schema.link()(
- Schema.String,
- Transformation.transform({
- decode: (message) => new MyError(message),
- encode: (e) => e.message
- })
- )
- }
- )
-
- await assertions.serialization.json.typeCodec.succeed(schema, new MyError("a"), "a")
- await assertions.deserialization.json.typeCodec.succeed(schema, "a", new MyError("a"))
- })
-
- it("arg: struct", async () => {
- class MyError extends Error {
- static Props = Schema.Struct({
- message: Schema.String,
- cause: Schema.String
- })
-
- constructor(props: typeof MyError.Props["Type"]) {
- super(props.message, { cause: props.cause })
- this.name = "MyError"
- Object.setPrototypeOf(this, MyError.prototype)
- }
-
- static schema = Schema.instanceOf(
- MyError,
- {
- title: "MyError",
- defaultJsonSerializer: () =>
- Schema.link()(
- MyError.Props,
- Transformation.transform({
- decode: (props) => new MyError(props),
- encode: (e) => ({
- message: e.message,
- cause: typeof e.cause === "string" ? e.cause : String(e.cause)
- })
- })
- )
- }
- )
- }
-
- const schema = MyError.schema
-
- await assertions.serialization.json.typeCodec.succeed(schema, new MyError({ message: "a", cause: "b" }), {
- message: "a",
- cause: "b"
- })
- await assertions.deserialization.json.typeCodec.succeed(
- schema,
- { message: "a", cause: "b" },
- new MyError({ message: "a", cause: "b" })
- )
- })
- })
-
- it("Class", async () => {
- class A extends Schema.Class("A")(Schema.Struct({
- a: FiniteFromDate
- })) {}
-
- await assertions.serialization.json.codec.succeed(A, new A({ a: 0 }), { a: "1970-01-01T00:00:00.000Z" })
- await assertions.deserialization.json.codec.succeed(A, { a: "1970-01-01T00:00:00.000Z" }, new A({ a: 0 }))
- })
-
- it("Error", async () => {
- class E extends Schema.ErrorClass("E")({
- a: FiniteFromDate
- }) {}
-
- await assertions.serialization.json.codec.succeed(E, new E({ a: 0 }), { a: "1970-01-01T00:00:00.000Z" })
- await assertions.deserialization.json.codec.succeed(E, { a: "1970-01-01T00:00:00.000Z" }, new E({ a: 0 }))
- })
-
- it("Enums", async () => {
- enum Fruits {
- Apple,
- Banana = "banana"
- }
- const schema = Schema.Enums(Fruits)
-
- await assertions.serialization.json.codec.succeed(schema, Fruits.Apple, 0)
- await assertions.serialization.json.codec.succeed(schema, Fruits.Banana, "banana")
- await assertions.deserialization.json.codec.succeed(schema, 0, Fruits.Apple)
- await assertions.deserialization.json.codec.succeed(schema, "banana", Fruits.Banana)
- })
-
it("StandardSchemaV1FailureResult", async () => {
const b = Symbol.for("b")
@@ -643,22 +921,83 @@ describe("Serializer", () => {
})
describe("default serialization", () => {
- it("Symbol", async () => {
- const schema = Schema.Symbol
+ describe("Unsupported schemas", () => {
+ it("Declaration without annotation", async () => {
+ class A {
+ readonly _tag = "A"
+ }
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, Symbol.for("a"), "Symbol(a)")
- await assertions.serialization.stringLeafJson.typeCodec.fail(
- schema,
- Symbol("a"),
- "cannot serialize to string, Symbol is not registered"
- )
- await assertions.serialization.stringLeafJson.typeCodec.fail(
- schema,
- Symbol(),
- "cannot serialize to string, Symbol has no description"
- )
+ await assertions.serialization.stringLeafJson.typeCodec.fail(
+ Schema.declare((u): u is A => u instanceof A),
+ new A(),
+ `cannot serialize to JSON, required \`defaultJsonSerializer\` annotation for Declaration`
+ )
+ })
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "Symbol(a)", Symbol.for("a"))
+ it("Any", async () => {
+ await assertions.serialization.stringLeafJson.typeCodec.fail(
+ Schema.Any,
+ "a",
+ `cannot serialize to JSON, required \`defaultJsonSerializer\` annotation for AnyKeyword`
+ )
+ })
+
+ it("Unknown", async () => {
+ await assertions.serialization.stringLeafJson.typeCodec.fail(
+ Schema.Unknown,
+ "a",
+ `cannot serialize to JSON, required \`defaultJsonSerializer\` annotation for UnknownKeyword`
+ )
+ })
+
+ it("Object", async () => {
+ await assertions.serialization.stringLeafJson.typeCodec.fail(
+ Schema.Object,
+ {},
+ `cannot serialize to JSON, required \`defaultJsonSerializer\` annotation for ObjectKeyword`
+ )
+ })
+
+ it("Struct with Symbol property name", async () => {
+ const a = Symbol.for("a")
+ const schema = Schema.Struct({
+ [a]: Schema.String
+ })
+
+ await assertions.serialization.stringLeafJson.typeCodec.fail(
+ schema,
+ { [a]: "b" },
+ "cannot serialize to JSON, property names must be strings"
+ )
+ })
+ })
+
+ it("Undefined", async () => {
+ const schema = Schema.Undefined
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, undefined)
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, undefined)
+ })
+
+ it("Void", async () => {
+ const schema = Schema.Void
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, undefined)
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, undefined)
+ })
+
+ it("Null", async () => {
+ const schema = Schema.Null
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, null, undefined)
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, undefined, null)
+ })
+
+ it("String", async () => {
+ const schema = Schema.String
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, "a")
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "a")
})
it("Number", async () => {
@@ -677,11 +1016,40 @@ describe("Serializer", () => {
await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "false", false)
})
- it("Null", async () => {
- const schema = Schema.Null
+ it("Symbol", async () => {
+ const schema = Schema.Symbol
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, null, "")
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "", null)
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, Symbol.for("a"), "Symbol(a)")
+ await assertions.serialization.stringLeafJson.typeCodec.fail(
+ schema,
+ Symbol("a"),
+ "cannot serialize to string, Symbol is not registered"
+ )
+ await assertions.serialization.stringLeafJson.typeCodec.fail(
+ schema,
+ Symbol(),
+ "cannot serialize to string, Symbol has no description"
+ )
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "Symbol(a)", Symbol.for("a"))
+ })
+
+ it("BigInt", async () => {
+ const schema = Schema.BigInt
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, 1n, "1")
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "1", 1n)
+ })
+
+ it("PropertyKey", async () => {
+ const schema = Schema.PropertyKey
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, "a", "a")
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, 1, "1")
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, Symbol.for("a"), "Symbol(a)")
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "a", "a")
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "1", "1")
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "Symbol(a)", Symbol.for("a"))
})
describe("Literal", () => {
@@ -723,79 +1091,255 @@ describe("Serializer", () => {
)
})
- describe("Enums", () => {
- it("should serialize the enum value", async () => {
- enum Fruits {
- Apple,
- Banana = "banana"
- }
- const schema = Schema.Enums(Fruits)
+ describe("TemplateLiteral", () => {
+ it("1n + string", async () => {
+ const schema = Schema.TemplateLiteral([1n, Schema.String])
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, Fruits.Apple, "0")
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, Fruits.Banana, "banana")
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "0", Fruits.Apple)
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "banana", Fruits.Banana)
+ await assertions.serialization.json.typeCodec.succeed(schema, "1a")
+ await assertions.deserialization.json.typeCodec.succeed(schema, "1a")
+ })
+
+ it(`"a" + bigint`, async () => {
+ const schema = Schema.TemplateLiteral(["a", Schema.BigInt])
+
+ await assertions.serialization.json.typeCodec.succeed(schema, "a1")
+ await assertions.deserialization.json.typeCodec.succeed(schema, "a1")
})
})
- it("TemplateLiteral", async () => {
- const schema = Schema.TemplateLiteral(["a", Schema.Literal(1), "b"])
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, "a1b")
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "a1b")
+ it("Enums", async () => {
+ enum Fruits {
+ Apple,
+ Banana
+ }
+ const schema = Schema.Enums(Fruits)
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, Fruits.Apple, "0")
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, Fruits.Banana, "1")
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "0", Fruits.Apple)
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "1", Fruits.Banana)
})
- it("NullOr(String)", async () => {
- const schema = Schema.NullOr(Schema.String)
+ describe("Struct", () => {
+ it("Date", async () => {
+ const schema = Schema.Struct({
+ a: Schema.Date
+ })
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, "a", "a")
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, null, "")
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "", "")
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "a", "a")
- })
-
- it("NullOr(Number)", async () => {
- const schema = Schema.NullOr(Schema.Number)
-
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, 1, "1")
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, null, "")
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "", null)
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "1", 1)
- })
-
- it("Array(NullOr(Number))", async () => {
- const schema = Schema.Array(Schema.NullOr(Schema.Number))
-
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [1, null], ["1", ""])
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, ["1", ""], [1, null])
- })
-
- it("Struct", async () => {
- const schema = Schema.Struct({
- a: Schema.NullOr(Schema.Number),
- b: Schema.NullOr(Schema.Number)
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, { a: new Date("2021-01-01") }, {
+ a: "2021-01-01T00:00:00.000Z"
+ })
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, { a: "2021-01-01T00:00:00.000Z" }, {
+ a: new Date("2021-01-01")
+ })
})
- await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, { a: 1, b: null }, {
- a: "1",
- b: ""
+ it("UndefinedOr(Date)", async () => {
+ const schema = Schema.Struct({
+ a: Schema.UndefinedOr(Schema.Date)
+ })
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, { a: new Date("2021-01-01") }, {
+ a: "2021-01-01T00:00:00.000Z"
+ })
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, { a: undefined }, { a: undefined })
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, { a: "2021-01-01T00:00:00.000Z" }, {
+ a: new Date("2021-01-01")
+ })
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, { a: undefined }, { a: undefined })
+ })
+
+ it("NullOr(Date)", async () => {
+ const schema = Schema.Struct({
+ a: Schema.NullOr(Schema.Date)
+ })
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, { a: new Date("2021-01-01") }, {
+ a: "2021-01-01T00:00:00.000Z"
+ })
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, { a: null }, { a: undefined })
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, { a: "2021-01-01T00:00:00.000Z" }, {
+ a: new Date("2021-01-01")
+ })
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, { a: undefined }, { a: null })
+ })
+
+ it("optionalKey(Date)", async () => {
+ const schema = Schema.Struct({
+ a: Schema.optionalKey(Schema.Date)
+ })
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, { a: new Date("2021-01-01") }, {
+ a: "2021-01-01T00:00:00.000Z"
+ })
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, {}, {})
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, { a: "2021-01-01T00:00:00.000Z" }, {
+ a: new Date("2021-01-01")
+ })
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, {}, {})
+ })
+
+ it("optional(Date)", async () => {
+ const schema = Schema.Struct({
+ a: Schema.optional(Schema.Date)
+ })
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, { a: new Date("2021-01-01") }, {
+ a: "2021-01-01T00:00:00.000Z"
+ })
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, {}, {})
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, { a: undefined }, { a: undefined })
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, { a: "2021-01-01T00:00:00.000Z" }, {
+ a: new Date("2021-01-01")
+ })
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, {}, {})
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, { a: undefined }, { a: undefined })
})
- await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, {
- a: "1",
- b: ""
- }, { a: 1, b: null })
})
- it("Struct with Symbol property name", async () => {
- const a = Symbol.for("a")
- const schema = Schema.Struct({
- [a]: Schema.String
- })
+ it("Record(Symbol, Date)", async () => {
+ const schema = Schema.Record(Schema.Symbol, Schema.Date)
- await assertions.serialization.stringLeafJson.typeCodec.fail(
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(
schema,
- { [a]: "b" },
- "cannot serialize to JSON, property names must be strings"
+ { [Symbol.for("a")]: new Date("2021-01-01"), [Symbol.for("b")]: new Date("2021-01-01") },
+ { "Symbol(a)": "2021-01-01T00:00:00.000Z", "Symbol(b)": "2021-01-01T00:00:00.000Z" }
)
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ { "Symbol(a)": "2021-01-01T00:00:00.000Z", "Symbol(b)": "2021-01-01T00:00:00.000Z" },
+ { [Symbol.for("a")]: new Date("2021-01-01"), [Symbol.for("b")]: new Date("2021-01-01") }
+ )
+ })
+
+ describe("Tuple", () => {
+ it("Date", async () => {
+ const schema = Schema.Tuple([Schema.Date])
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ [new Date("2021-01-01")],
+ ["2021-01-01T00:00:00.000Z"]
+ )
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ ["2021-01-01T00:00:00.000Z"],
+ [new Date("2021-01-01")]
+ )
+ })
+
+ it("UndefinedOr(Date)", async () => {
+ const schema = Schema.Tuple([Schema.UndefinedOr(Schema.Date)])
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ [new Date("2021-01-01")],
+ ["2021-01-01T00:00:00.000Z"]
+ )
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [undefined])
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, ["2021-01-01T00:00:00.000Z"], [
+ new Date("2021-01-01")
+ ])
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, [undefined])
+ })
+
+ it("NullOr(Date)", async () => {
+ const schema = Schema.Tuple([Schema.NullOr(Schema.Date)])
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [new Date("2021-01-01")], [
+ "2021-01-01T00:00:00.000Z"
+ ])
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [null], [undefined])
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, ["2021-01-01T00:00:00.000Z"], [
+ new Date("2021-01-01")
+ ])
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, [undefined], [null])
+ })
+
+ it("optionalKey(Date)", async () => {
+ const schema = Schema.Tuple([Schema.optionalKey(Schema.Date)])
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [new Date("2021-01-01")], [
+ "2021-01-01T00:00:00.000Z"
+ ])
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [], [])
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, ["2021-01-01T00:00:00.000Z"], [
+ new Date("2021-01-01")
+ ])
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, [], [])
+ })
+
+ it("optional(Date)", async () => {
+ const schema = Schema.Tuple([Schema.optional(Schema.Date)])
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [new Date("2021-01-01")], [
+ "2021-01-01T00:00:00.000Z"
+ ])
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [], [])
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [undefined])
+
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, ["2021-01-01T00:00:00.000Z"], [
+ new Date("2021-01-01")
+ ])
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, [], [])
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, [undefined])
+ })
+ })
+
+ it("Array(Date)", async () => {
+ const schema = Schema.Array(Schema.Date)
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ [new Date("2021-01-01"), new Date("2021-01-02")],
+ ["2021-01-01T00:00:00.000Z", "2021-01-02T00:00:00.000Z"]
+ )
+ })
+
+ describe("Union", () => {
+ it("NullOr(Date)", async () => {
+ const schema = Schema.NullOr(Schema.String)
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, "a", "a")
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, null, undefined)
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, undefined, null)
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "a", "a")
+ })
+
+ it("NullOr(Number)", async () => {
+ const schema = Schema.NullOr(Schema.Number)
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, 1, "1")
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, null, undefined)
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, undefined, null)
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, "1", 1)
+ })
+
+ it("Array(NullOr(Number))", async () => {
+ const schema = Schema.Array(Schema.NullOr(Schema.Number))
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, [1, null], ["1", undefined])
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, ["1", undefined], [1, null])
+ })
+
+ it("Union(Schema.Date, FiniteFromDate)", async () => {
+ const schema = Schema.Union([Schema.Date, FiniteFromDate])
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ new Date("2021-01-01"),
+ "2021-01-01T00:00:00.000Z"
+ )
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, 0, "0")
+ })
})
it("Suspend", async () => {
@@ -835,6 +1379,150 @@ describe("Serializer", () => {
]
}, { a: 1, categories: [{ a: 2, categories: [] }] })
})
+
+ it("FiniteFromDate", async () => {
+ const schema = FiniteFromDate
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, 0, "0")
+ })
+
+ it("Class", async () => {
+ class A extends Schema.Class("A")(Schema.Struct({
+ a: FiniteFromDate
+ })) {}
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(A, new A({ a: 0 }), { a: "0" })
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(A, { a: "0" }, new A({ a: 0 }))
+ })
+
+ it("ErrorClass", async () => {
+ class E extends Schema.ErrorClass("E")({
+ a: FiniteFromDate
+ }) {}
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(E, new E({ a: 0 }), { a: "0" })
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(E, { a: "0" }, new E({ a: 0 }))
+ })
+
+ it("Date", async () => {
+ const schema = Schema.Date
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ new Date("2021-01-01"),
+ "2021-01-01T00:00:00.000Z"
+ )
+ })
+
+ it("Error", async () => {
+ const schema = Schema.Error
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ new Error("a"),
+ { name: "Error", message: "a" }
+ )
+ // Error: message only
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ { message: "a" },
+ new Error("a", { cause: { message: "a" } })
+ )
+ // Error: message and name
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ { name: "b", message: "a" },
+ (() => {
+ const err = new Error("a", { cause: { message: "a", name: "b" } })
+ err.name = "b"
+ return err
+ })()
+ )
+ // Error: message, name, and stack
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ { name: "b", message: "a", stack: "c" },
+ (() => {
+ const err = new Error("a", { cause: { message: "a", name: "b", stack: "c" } })
+ err.name = "b"
+ err.stack = "c"
+ return err
+ })()
+ )
+ })
+
+ it("URL", async () => {
+ const schema = Schema.URL
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ new URL("https://example.com"),
+ "https://example.com/"
+ )
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ "https://example.com",
+ new URL("https://example.com")
+ )
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ "https://example.com/",
+ new URL("https://example.com")
+ )
+ await assertions.deserialization.stringLeafJson.typeCodec.fail(
+ schema,
+ "not a url",
+ isDeno ? `TypeError: Invalid URL: 'not a url'` : `TypeError: Invalid URL`
+ )
+ })
+
+ it("Option(Date)", async () => {
+ const schema = Schema.Option(Schema.Date)
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, Option.some(new Date("2021-01-01")), [
+ "2021-01-01T00:00:00.000Z"
+ ])
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(schema, Option.none(), [])
+ })
+
+ it("Redacted(Option(String))", async () => {
+ const schema = Schema.Redacted(Schema.Option(Schema.String))
+
+ await assertions.serialization.stringLeafJson.typeCodec.fail(
+ schema,
+ Redacted.make(Option.none()),
+ `Cannot serialize Redacted`
+ )
+ await assertions.serialization.stringLeafJson.typeCodec.fail(
+ schema,
+ Redacted.make(Option.some("a")),
+ `Cannot serialize Redacted`
+ )
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(schema, [], Redacted.make(Option.none()))
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ ["a"],
+ Redacted.make(Option.some("a"))
+ )
+ })
+
+ it("Map", async () => {
+ const schema = Schema.Map(Schema.Option(Schema.Date), FiniteFromDate)
+
+ await assertions.serialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ new Map([[Option.some(new Date("2021-01-01")), 0]]),
+ [[
+ ["2021-01-01T00:00:00.000Z"],
+ "0"
+ ]]
+ )
+ await assertions.deserialization.stringLeafJson.typeCodec.succeed(
+ schema,
+ [[["2021-01-01T00:00:00.000Z"], "0"]],
+ new Map([[Option.some(new Date("2021-01-01")), 0]])
+ )
+ })
})
})