|
| 1 | +import type { Annotations } from "effect/schema" |
| 2 | +import { FromJsonSchema, Schema } from "effect/schema" |
| 3 | +import { describe, it } from "vitest" |
| 4 | +import { deepStrictEqual } from "../utils/assert.ts" |
| 5 | + |
| 6 | +function assertRoundtrip(schema: Schema.Top) { |
| 7 | + const document = Schema.makeJsonSchemaDraft2020_12(schema) |
| 8 | + const code = FromJsonSchema.make(document.schema) |
| 9 | + const fn = new Function("Schema", `return ${code}`) |
| 10 | + const generated = fn(Schema) |
| 11 | + const codedocument = Schema.makeJsonSchemaDraft2020_12(generated) |
| 12 | + deepStrictEqual(codedocument, document) |
| 13 | + deepStrictEqual(FromJsonSchema.make(codedocument.schema), code) |
| 14 | +} |
| 15 | + |
| 16 | +function assertOutput( |
| 17 | + input: { |
| 18 | + readonly schema: Record<string, unknown> | boolean |
| 19 | + readonly definitions?: Schema.JsonSchema.Definitions | undefined |
| 20 | + readonly target?: Annotations.JsonSchema.Target | undefined |
| 21 | + }, |
| 22 | + expected: string |
| 23 | +) { |
| 24 | + const code = FromJsonSchema.make(input.schema, { |
| 25 | + target: input.target ?? "2020-12", |
| 26 | + definitions: input.definitions ?? {} |
| 27 | + }) |
| 28 | + deepStrictEqual(code, expected) |
| 29 | +} |
| 30 | + |
| 31 | +describe("FromJsonSchema", () => { |
| 32 | + it("should handle `true` as `Schema.Unknown`", () => { |
| 33 | + assertOutput({ schema: true }, "Schema.Unknown") |
| 34 | + }) |
| 35 | + |
| 36 | + it("should handle `false` as `Schema.Never`", () => { |
| 37 | + assertOutput({ schema: false }, "Schema.Never") |
| 38 | + }) |
| 39 | + |
| 40 | + describe("type as array", () => { |
| 41 | + it("string | number", () => { |
| 42 | + assertOutput({ |
| 43 | + schema: { |
| 44 | + "type": ["string", "number"] |
| 45 | + } |
| 46 | + }, "Schema.Union([Schema.String, Schema.Number])") |
| 47 | + }) |
| 48 | + |
| 49 | + it("string | number & annotations", () => { |
| 50 | + assertOutput({ |
| 51 | + schema: { |
| 52 | + "type": ["string", "number"], |
| 53 | + "description": "description" |
| 54 | + } |
| 55 | + }, `Schema.Union([Schema.String, Schema.Number]).annotate({ description: "description" })`) |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + it("should handle annotations", () => { |
| 60 | + assertOutput( |
| 61 | + { |
| 62 | + schema: { |
| 63 | + "type": "string", |
| 64 | + "title": "title", |
| 65 | + "description": "description", |
| 66 | + "default": "a", |
| 67 | + "examples": ["a", "b"] |
| 68 | + } |
| 69 | + }, |
| 70 | + `Schema.String.annotate({ title: "title", description: "description", default: "a", examples: ["a", "b"] })` |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + it("should handle checks", () => { |
| 75 | + assertOutput( |
| 76 | + { |
| 77 | + schema: { |
| 78 | + "type": "string", |
| 79 | + "minLength": 1 |
| 80 | + } |
| 81 | + }, |
| 82 | + `Schema.String.check(Schema.isMinLength(1))` |
| 83 | + ) |
| 84 | + assertOutput( |
| 85 | + { |
| 86 | + schema: { |
| 87 | + "type": "string", |
| 88 | + "description": "description", |
| 89 | + "minLength": 1 |
| 90 | + } |
| 91 | + }, |
| 92 | + `Schema.String.annotate({ description: "description" }).check(Schema.isMinLength(1))` |
| 93 | + ) |
| 94 | + }) |
| 95 | + |
| 96 | + it("should handle refs", () => { |
| 97 | + assertOutput( |
| 98 | + { |
| 99 | + schema: { |
| 100 | + "$ref": "#/definitions/ID" |
| 101 | + }, |
| 102 | + definitions: { |
| 103 | + "ID": { |
| 104 | + "type": "string" |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | + `Schema.String.annotate({ identifier: "ID" })` |
| 109 | + ) |
| 110 | + }) |
| 111 | + |
| 112 | + describe("roundtrips", () => { |
| 113 | + it("Never", () => { |
| 114 | + assertRoundtrip(Schema.Never) |
| 115 | + }) |
| 116 | + |
| 117 | + it("Unknown", () => { |
| 118 | + assertRoundtrip(Schema.Unknown) |
| 119 | + }) |
| 120 | + |
| 121 | + it("Null", () => { |
| 122 | + assertRoundtrip(Schema.Null) |
| 123 | + }) |
| 124 | + |
| 125 | + describe("String", () => { |
| 126 | + it("basic", () => { |
| 127 | + assertRoundtrip(Schema.String) |
| 128 | + }) |
| 129 | + |
| 130 | + it("with annotations", () => { |
| 131 | + assertRoundtrip(Schema.String.annotate({ title: "title", description: "description" })) |
| 132 | + }) |
| 133 | + |
| 134 | + it("with check", () => { |
| 135 | + assertRoundtrip(Schema.String.check(Schema.isMinLength(1))) |
| 136 | + }) |
| 137 | + |
| 138 | + it("with check and annotations", () => { |
| 139 | + assertRoundtrip(Schema.String.annotate({ description: "description" }).check(Schema.isMinLength(1))) |
| 140 | + assertRoundtrip(Schema.String.check(Schema.isMinLength(1)).annotate({ description: "description" })) |
| 141 | + assertRoundtrip(Schema.String.check(Schema.isMinLength(1, { description: "description" }))) |
| 142 | + }) |
| 143 | + |
| 144 | + it("with checks", () => { |
| 145 | + assertRoundtrip(Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(10))) |
| 146 | + }) |
| 147 | + |
| 148 | + it("with checks and annotations", () => { |
| 149 | + assertRoundtrip( |
| 150 | + Schema.String.annotate({ description: "description" }).check(Schema.isMinLength(1), Schema.isMaxLength(10)) |
| 151 | + ) |
| 152 | + assertRoundtrip( |
| 153 | + Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(10)).annotate({ description: "description" }) |
| 154 | + ) |
| 155 | + assertRoundtrip( |
| 156 | + Schema.String.check(Schema.isMinLength(1, { description: "description" }), Schema.isMaxLength(10)) |
| 157 | + ) |
| 158 | + assertRoundtrip( |
| 159 | + Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(10, { description: "description" })) |
| 160 | + ) |
| 161 | + assertRoundtrip( |
| 162 | + Schema.String.annotate({ description: "description1" }).check( |
| 163 | + Schema.isMinLength(1), |
| 164 | + Schema.isMaxLength(10, { description: "description2" }) |
| 165 | + ) |
| 166 | + ) |
| 167 | + assertRoundtrip( |
| 168 | + Schema.String.check( |
| 169 | + Schema.isMinLength(1, { description: "description1" }), |
| 170 | + Schema.isMaxLength(10, { description: "description2" }) |
| 171 | + ) |
| 172 | + ) |
| 173 | + }) |
| 174 | + }) |
| 175 | + |
| 176 | + it("Number", () => { |
| 177 | + assertRoundtrip(Schema.Number) |
| 178 | + }) |
| 179 | + |
| 180 | + it("Boolean", () => { |
| 181 | + assertRoundtrip(Schema.Boolean) |
| 182 | + }) |
| 183 | + |
| 184 | + it("Int", () => { |
| 185 | + assertRoundtrip(Schema.Int) |
| 186 | + }) |
| 187 | + |
| 188 | + it.skip("Struct", () => { |
| 189 | + assertRoundtrip(Schema.Struct({})) |
| 190 | + }) |
| 191 | + |
| 192 | + describe("Tuple", () => { |
| 193 | + it("empty", () => { |
| 194 | + assertRoundtrip(Schema.Tuple([])) |
| 195 | + }) |
| 196 | + }) |
| 197 | + |
| 198 | + describe("Union", () => { |
| 199 | + it("empty", () => { |
| 200 | + assertRoundtrip(Schema.Union([])) |
| 201 | + }) |
| 202 | + |
| 203 | + it("String | Number", () => { |
| 204 | + assertRoundtrip(Schema.Union([Schema.String, Schema.Number])) |
| 205 | + }) |
| 206 | + }) |
| 207 | + }) |
| 208 | +}) |
0 commit comments