@kaiko.io/rescript-deser 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/package.json +1 -1
- package/src/JSON.res +9 -1
package/README.md
CHANGED
|
@@ -60,6 +60,8 @@ module type Serializable = {
|
|
|
60
60
|
|
|
61
61
|
- `String`, parses a JSON string into a ReScript `string`.
|
|
62
62
|
|
|
63
|
+
- `Literal(expected)`, parses a JSON string that must match the expected value.
|
|
64
|
+
|
|
63
65
|
- `Int`, parses a JSON number into a ReScript `int`
|
|
64
66
|
|
|
65
67
|
- `Float`, parses a JSON number into a ReScript `float`
|
package/package.json
CHANGED
package/src/JSON.res
CHANGED
|
@@ -34,6 +34,7 @@ module Field = {
|
|
|
34
34
|
type rec t =
|
|
35
35
|
| Any
|
|
36
36
|
| String
|
|
37
|
+
| Literal(string)
|
|
37
38
|
| Int
|
|
38
39
|
| Float
|
|
39
40
|
| Boolean
|
|
@@ -102,6 +103,7 @@ module Field = {
|
|
|
102
103
|
switch type_ {
|
|
103
104
|
| Any => "Any"
|
|
104
105
|
| String => "String"
|
|
106
|
+
| Literal(lit) => `Literal: ${lit}`
|
|
105
107
|
| Int => "Integer"
|
|
106
108
|
| Float => "Float"
|
|
107
109
|
| Boolean => "Boolean"
|
|
@@ -165,6 +167,12 @@ module Field = {
|
|
|
165
167
|
and fromUntagged = (untagged: Js.Json.t, shape: t, self: t): FieldValue.t => {
|
|
166
168
|
switch (shape, untagged->Js.Json.classify) {
|
|
167
169
|
| (Any, _) => untagged->FieldValue.any
|
|
170
|
+
| (Literal(expected), Js.Json.JSONString(text)) =>
|
|
171
|
+
if text == expected {
|
|
172
|
+
FieldValue.string(text)
|
|
173
|
+
} else {
|
|
174
|
+
raise(TypeError(`Expecting literal ${expected}, got ${text}`))
|
|
175
|
+
}
|
|
168
176
|
| (String, Js.Json.JSONString(text)) => FieldValue.string(text)
|
|
169
177
|
| (Int, Js.Json.JSONNumber(number)) => FieldValue.int(number->Float.toInt)
|
|
170
178
|
| (Float, Js.Json.JSONNumber(number)) => FieldValue.float(number)
|
|
@@ -262,7 +270,7 @@ module Field = {
|
|
|
262
270
|
| (Self, true) => Ok()
|
|
263
271
|
|
|
264
272
|
| (Any, _) => Ok()
|
|
265
|
-
| (String, _) | (Float, _) | (Int, _) => Ok()
|
|
273
|
+
| (String, _) | (Float, _) | (Int, _) | (Literal(_), _) => Ok()
|
|
266
274
|
| (Boolean, _) | (Date, _) | (Datetime, _) => Ok()
|
|
267
275
|
| (Morphism(_, _), _) => Ok()
|
|
268
276
|
|