Skip to content

Commit d3b58f2

Browse files
committed
v7.1.0 🚀 - Use enum field for union of literals
1 parent a484769 commit d3b58f2

File tree

6 files changed

+63
-34
lines changed

6 files changed

+63
-34
lines changed

‎README.md‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,7 @@ JSONSchema.make(filmSchema)
7878
"Title": { "type": "string" },
7979
"Tags": { "items": { "type": "string" }, "type": "array", "default": [] },
8080
"Rating": {
81-
"anyOf": [
82-
{ "type": "string", "const": "G" },
83-
{ "type": "string", "const": "PG" },
84-
{ "type": "string", "const": "PG13" },
85-
{ "type": "string", "const": "R" }
86-
]
81+
"enum": ["G", "PG", "PG13", "R"]
8782
},
8883
"Age": {
8984
"type": "integer",

‎__tests__/JSONSchema_test.res‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,22 @@ test("Schema of tuple schema", t => {
413413
)
414414
})
415415

416+
test("Schema of enum schema", t => {
417+
let schema = S.enum(["Yes", "No"])
418+
419+
t->Assert.deepEqual(
420+
JSONSchema.make(schema),
421+
Ok(
422+
%raw(`{
423+
"$schema": "http://json-schema.org/draft-07/schema#",
424+
"enum": ["Yes", "No"]
425+
}`),
426+
),
427+
)
428+
})
429+
416430
test("Schema of union schema", t => {
417-
let schema = S.union([S.literal("Yes"), S.literal("No")])
431+
let schema = S.union([S.literal("Yes"), S.string])
418432

419433
t->Assert.deepEqual(
420434
JSONSchema.make(schema),
@@ -427,7 +441,6 @@ test("Schema of union schema", t => {
427441
type: 'string'
428442
},
429443
{
430-
const: 'No',
431444
type: 'string'
432445
}
433446
]
@@ -990,12 +1003,7 @@ module Example = {
9901003
Title: { type: "string" },
9911004
Tags: { items: { type: "string" }, type: "array", default: [] },
9921005
Rating: {
993-
anyOf: [
994-
{ type: "string", const: "G" },
995-
{ type: "string", const: "PG" },
996-
{ type: "string", const: "PG13" },
997-
{ type: "string", const: "R" },
998-
],
1006+
enum: ["G", "PG", "PG13", "R"],
9991007
},
10001008
Age: {
10011009
type: "integer",

‎package-lock.json‎

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rescript-json-schema",
3-
"version": "7.0.0",
3+
"version": "7.1.0",
44
"description": "📄 Typesafe JSON Schema for ReScript",
55
"keywords": [
66
"rescript",
@@ -42,7 +42,7 @@
4242
"@dzakh/rescript-ava": "3.0.0",
4343
"ava": "5.2.0",
4444
"rescript": "11.1.4",
45-
"rescript-schema": "9.0.1",
45+
"rescript-schema": "9.1.0",
4646
"c8": "8.0.1"
4747
},
4848
"peerDependencies": {

‎src/JSONSchema.bs.js‎

Lines changed: 18 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/JSONSchema.res‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,27 @@ let rec fromRescriptSchema:
165165
}
166166

167167
| S.Union(childSchemas) => {
168-
let items = childSchemas->Js.Array2.map(childSchema => {
168+
let literals = []
169+
let items = []
170+
171+
childSchemas->Js.Array2.forEach(childSchema => {
169172
if childSchema->isOptionalSchema {
170173
Error.UnsupportedOptionalItem.raise(schema)
171-
} else {
172-
Definition.schema(fromRescriptSchema(childSchema))
174+
}
175+
176+
items->Js.Array2.push(Definition.schema(fromRescriptSchema(childSchema)))->ignore
177+
switch childSchema->S.classify {
178+
| Literal(l) =>
179+
literals->Js.Array2.push(l->S.Literal.value->(magic: unknown => Js.Json.t))->ignore
180+
| _ => ()
173181
}
174182
})
175-
jsonSchema.anyOf = Some(items)
183+
184+
if literals->Js.Array2.length === items->Js.Array2.length {
185+
jsonSchema.enum = Some(literals)
186+
} else {
187+
jsonSchema.anyOf = Some(items)
188+
}
176189
}
177190

178191
| S.Option(childSchema) => {

0 commit comments

Comments
 (0)