WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 179b07a

Browse files
committed
Add support for the Strip unknownKeys strategy
1 parent f6cce88 commit 179b07a

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

__tests__/JsonSchema_test.res

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ test("Schema of record struct with one string field", t => {
115115
)
116116
})
117117

118+
test("Schema of record struct with Strip unknownKeys strategy allows additionalProperties", t => {
119+
let struct =
120+
S.record1(
121+
~fields=("field", S.string()),
122+
~constructor=field => {field: field}->Ok,
123+
(),
124+
)->S.Record.strip
125+
126+
t->Assert.deepEqual(
127+
JsonSchema.make(struct),
128+
Ok(
129+
%raw(`{
130+
"$schema": "http://json-schema.org/draft-07/schema#",
131+
"type": "object",
132+
"properties": {"field": {"type": "string"}},
133+
"required": ["field"],
134+
"additionalProperties": true,
135+
}`),
136+
),
137+
(),
138+
)
139+
})
140+
118141
test("Schema of record struct with one optional string field", t => {
119142
let struct = S.record1(
120143
~fields=("optionalField", S.option(S.string())),

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rescript-json-schema",
3-
"version": "0.16.0",
3+
"version": "0.17.0",
44
"description": "Typesafe JSON Schema for ReScript",
55
"keywords": [
66
"ReScript",
@@ -42,9 +42,9 @@
4242
"devDependencies": {
4343
"rescript": "9.1.4",
4444
"rescript-ava": "github:DZakh/rescript-ava#007d9f63e17310f73fabab1223eb75a0c9c36035",
45-
"rescript-struct": "^0.8.0"
45+
"rescript-struct": "0.9.0"
4646
},
4747
"peerDependencies": {
48-
"rescript-struct": "^0.8.x"
48+
"rescript-struct": "0.9.x"
4949
}
5050
}

src/JsonSchema.res

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ let rec makeNode:
9999
Error(JsonSchema_Error.UnsupportedNestedOptional.make())
100100
}
101101
})
102-
| S.Record(fields) =>
102+
| S.Record({fields, unknownKeys}) =>
103103
fields
104104
->RescriptStruct_ResultX.Array.mapi((field, _) => {
105105
let (fieldName, fieldStruct) = field
@@ -119,7 +119,14 @@ let rec makeNode:
119119
}
120120
properties->Js.Dict.set(fieldName, fieldNode.rawSchema)
121121
})
122-
Raw.record(~additionalProperties=false, ~properties, ~required)
122+
Raw.record(
123+
~additionalProperties=switch unknownKeys {
124+
| Strict => false
125+
| Strip => true
126+
},
127+
~properties,
128+
~required,
129+
)
123130
}
124131
{
125132
rawSchema: rawSchema,

0 commit comments

Comments
 (0)