osury 0.2.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.
@@ -0,0 +1,664 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as Errors from "./Errors.res.mjs";
4
+ import * as Core__Array from "@rescript/core/src/Core__Array.res.mjs";
5
+ import * as Core__Option from "@rescript/core/src/Core__Option.res.mjs";
6
+
7
+ function isNullType(json) {
8
+ if (typeof json !== "object" || json === null || Array.isArray(json)) {
9
+ return false;
10
+ }
11
+ let match = json["type"];
12
+ return match === "null";
13
+ }
14
+
15
+ function hasDefault(json) {
16
+ if (typeof json === "object" && json !== null && !Array.isArray(json)) {
17
+ return Core__Option.isSome(json["default"]);
18
+ } else {
19
+ return false;
20
+ }
21
+ }
22
+
23
+ function extractRefName(refPath) {
24
+ let parts = refPath.split("/");
25
+ return Core__Option.getOr(parts[parts.length - 1 | 0], refPath);
26
+ }
27
+
28
+ function parseEnumValues(arr) {
29
+ let values = Core__Array.filterMap(arr, item => {
30
+ if (typeof item === "string") {
31
+ return item;
32
+ }
33
+ });
34
+ if (values.length === arr.length) {
35
+ return values;
36
+ }
37
+ }
38
+
39
+ function extractTagFromConst(dict) {
40
+ let match = dict["_tag"];
41
+ if (match === undefined) {
42
+ return;
43
+ }
44
+ if (typeof match !== "object" || match === null || Array.isArray(match)) {
45
+ return;
46
+ }
47
+ let match$1 = match["const"];
48
+ if (typeof match$1 === "string") {
49
+ return match$1;
50
+ }
51
+ }
52
+
53
+ function parseSchema(json) {
54
+ if (typeof json === "object" && json !== null && !Array.isArray(json)) {
55
+ return parseObject(json);
56
+ }
57
+ return {
58
+ TAG: "Error",
59
+ _0: [Errors.makeError({
60
+ TAG: "InvalidJson",
61
+ _0: "expected object"
62
+ }, undefined, undefined, undefined)]
63
+ };
64
+ }
65
+
66
+ function parsePrimitiveType(dict) {
67
+ let match = dict["type"];
68
+ if (match === undefined) {
69
+ return {
70
+ TAG: "Error",
71
+ _0: [Errors.missingField("type", undefined, undefined, undefined)]
72
+ };
73
+ }
74
+ if (typeof match === "string") {
75
+ switch (match) {
76
+ case "array" :
77
+ return parseArrayType(dict);
78
+ case "boolean" :
79
+ return {
80
+ TAG: "Ok",
81
+ _0: "Boolean"
82
+ };
83
+ case "integer" :
84
+ return {
85
+ TAG: "Ok",
86
+ _0: "Integer"
87
+ };
88
+ case "null" :
89
+ return {
90
+ TAG: "Ok",
91
+ _0: "Null"
92
+ };
93
+ case "number" :
94
+ return {
95
+ TAG: "Ok",
96
+ _0: "Number"
97
+ };
98
+ case "object" :
99
+ return parseObjectType(dict);
100
+ case "string" :
101
+ let match$1 = dict["const"];
102
+ let exit = 0;
103
+ if (match$1 !== undefined) {
104
+ if (typeof match$1 === "string") {
105
+ return {
106
+ TAG: "Ok",
107
+ _0: {
108
+ _tag: "Enum",
109
+ _0: [match$1]
110
+ }
111
+ };
112
+ }
113
+ exit = 2;
114
+ } else {
115
+ exit = 2;
116
+ }
117
+ if (exit === 2) {
118
+ let match$2 = dict["enum"];
119
+ if (match$2 === undefined) {
120
+ return {
121
+ TAG: "Ok",
122
+ _0: "String"
123
+ };
124
+ }
125
+ let exit$1 = 0;
126
+ if (Array.isArray(match$2)) {
127
+ let values = parseEnumValues(match$2);
128
+ if (values !== undefined) {
129
+ return {
130
+ TAG: "Ok",
131
+ _0: {
132
+ _tag: "Enum",
133
+ _0: values
134
+ }
135
+ };
136
+ } else {
137
+ return {
138
+ TAG: "Error",
139
+ _0: [Errors.makeError({
140
+ TAG: "InvalidJson",
141
+ _0: "enum values must be strings"
142
+ }, undefined, undefined, undefined)]
143
+ };
144
+ }
145
+ }
146
+ exit$1 = 3;
147
+ if (exit$1 === 3) {
148
+ return {
149
+ TAG: "Error",
150
+ _0: [Errors.makeError({
151
+ TAG: "InvalidJson",
152
+ _0: "enum must be an array"
153
+ }, undefined, undefined, undefined)]
154
+ };
155
+ }
156
+ }
157
+ break;
158
+ default:
159
+ return {
160
+ TAG: "Error",
161
+ _0: [Errors.unknownType(match, undefined, undefined, undefined)]
162
+ };
163
+ }
164
+ }
165
+ return {
166
+ TAG: "Error",
167
+ _0: [Errors.makeError({
168
+ TAG: "InvalidJson",
169
+ _0: "type must be a string"
170
+ }, undefined, undefined, undefined)]
171
+ };
172
+ }
173
+
174
+ function parseArrayType(dict) {
175
+ let itemSchema = dict["items"];
176
+ if (itemSchema === undefined) {
177
+ return {
178
+ TAG: "Error",
179
+ _0: [Errors.missingField("items", undefined, "array type requires items schema", undefined)]
180
+ };
181
+ }
182
+ let itemType = parseSchema(itemSchema);
183
+ if (itemType.TAG === "Ok") {
184
+ return {
185
+ TAG: "Ok",
186
+ _0: {
187
+ _tag: "Array",
188
+ _0: itemType._0
189
+ }
190
+ };
191
+ } else {
192
+ return {
193
+ TAG: "Error",
194
+ _0: itemType._0
195
+ };
196
+ }
197
+ }
198
+
199
+ function parseAnyOf(items) {
200
+ let hasNull = items.some(isNullType);
201
+ let nonNullItems = items.filter(item => !isNullType(item));
202
+ if (hasNull && nonNullItems.length === 1) {
203
+ let match = nonNullItems[0];
204
+ if (match === undefined) {
205
+ return {
206
+ TAG: "Error",
207
+ _0: [Errors.makeError({
208
+ TAG: "InvalidJson",
209
+ _0: "anyOf with only null types"
210
+ }, undefined, undefined, undefined)]
211
+ };
212
+ }
213
+ if (typeof match === "object" && match !== null && !Array.isArray(match)) {
214
+ let innerType = parseObject(match);
215
+ if (innerType.TAG === "Ok") {
216
+ return {
217
+ TAG: "Ok",
218
+ _0: {
219
+ _tag: "Optional",
220
+ _0: innerType._0
221
+ }
222
+ };
223
+ } else {
224
+ return {
225
+ TAG: "Error",
226
+ _0: innerType._0
227
+ };
228
+ }
229
+ }
230
+ return {
231
+ TAG: "Error",
232
+ _0: [Errors.makeError({
233
+ TAG: "InvalidJson",
234
+ _0: "anyOf item must be object"
235
+ }, undefined, undefined, undefined)]
236
+ };
237
+ }
238
+ if (!hasNull && nonNullItems.length >= 2) {
239
+ let results = nonNullItems.map(parseSchema);
240
+ let errors = Core__Array.filterMap(results, r => {
241
+ if (r.TAG === "Ok") {
242
+ return;
243
+ } else {
244
+ return r._0;
245
+ }
246
+ }).flat();
247
+ if (errors.length > 0) {
248
+ return {
249
+ TAG: "Error",
250
+ _0: errors
251
+ };
252
+ }
253
+ let types = Core__Array.filterMap(results, r => {
254
+ if (r.TAG === "Ok") {
255
+ return r._0;
256
+ }
257
+ });
258
+ return {
259
+ TAG: "Ok",
260
+ _0: {
261
+ _tag: "Union",
262
+ _0: types
263
+ }
264
+ };
265
+ }
266
+ if (!(hasNull && nonNullItems.length >= 2)) {
267
+ return {
268
+ TAG: "Error",
269
+ _0: [Errors.makeError({
270
+ TAG: "InvalidJson",
271
+ _0: "anyOf must have at least 2 items"
272
+ }, undefined, undefined, undefined)]
273
+ };
274
+ }
275
+ let results$1 = nonNullItems.map(parseSchema);
276
+ let errors$1 = Core__Array.filterMap(results$1, r => {
277
+ if (r.TAG === "Ok") {
278
+ return;
279
+ } else {
280
+ return r._0;
281
+ }
282
+ }).flat();
283
+ if (errors$1.length > 0) {
284
+ return {
285
+ TAG: "Error",
286
+ _0: errors$1
287
+ };
288
+ }
289
+ let types$1 = Core__Array.filterMap(results$1, r => {
290
+ if (r.TAG === "Ok") {
291
+ return r._0;
292
+ }
293
+ });
294
+ return {
295
+ TAG: "Ok",
296
+ _0: {
297
+ _tag: "Optional",
298
+ _0: {
299
+ _tag: "Union",
300
+ _0: types$1
301
+ }
302
+ }
303
+ };
304
+ }
305
+
306
+ function parseObjectType(dict) {
307
+ let valueSchema = dict["additionalProperties"];
308
+ let exit = 0;
309
+ if (valueSchema !== undefined) {
310
+ switch (typeof valueSchema) {
311
+ case "boolean" :
312
+ if (valueSchema) {
313
+ return {
314
+ TAG: "Ok",
315
+ _0: {
316
+ _tag: "Dict",
317
+ _0: "String"
318
+ }
319
+ };
320
+ }
321
+ exit = 1;
322
+ break;
323
+ case "object" :
324
+ let valueType = parseSchema(valueSchema);
325
+ if (valueType.TAG === "Ok") {
326
+ return {
327
+ TAG: "Ok",
328
+ _0: {
329
+ _tag: "Dict",
330
+ _0: valueType._0
331
+ }
332
+ };
333
+ } else {
334
+ return {
335
+ TAG: "Error",
336
+ _0: valueType._0
337
+ };
338
+ }
339
+ default:
340
+ exit = 1;
341
+ }
342
+ } else {
343
+ exit = 1;
344
+ }
345
+ if (exit === 1) {
346
+ let match = dict["required"];
347
+ let requiredFields = match !== undefined ? (
348
+ Array.isArray(match) ? Core__Array.filterMap(match, item => {
349
+ if (typeof item === "string") {
350
+ return item;
351
+ }
352
+ }) : []
353
+ ) : [];
354
+ let match$1 = dict["properties"];
355
+ if (match$1 === undefined) {
356
+ return {
357
+ TAG: "Ok",
358
+ _0: {
359
+ _tag: "Object",
360
+ _0: []
361
+ }
362
+ };
363
+ }
364
+ let exit$1 = 0;
365
+ if (typeof match$1 === "object" && match$1 !== null && !Array.isArray(match$1)) {
366
+ let entries = Object.entries(match$1).filter(param => param[0] !== "_tag");
367
+ let results = entries.map(param => {
368
+ let propSchema = param[1];
369
+ let name = param[0];
370
+ let propType = parseSchema(propSchema);
371
+ if (propType.TAG === "Ok") {
372
+ return {
373
+ TAG: "Ok",
374
+ _0: {
375
+ name: name,
376
+ type: propType._0,
377
+ required: requiredFields.includes(name) || hasDefault(propSchema)
378
+ }
379
+ };
380
+ } else {
381
+ return {
382
+ TAG: "Error",
383
+ _0: propType._0
384
+ };
385
+ }
386
+ });
387
+ let errors = Core__Array.filterMap(results, r => {
388
+ if (r.TAG === "Ok") {
389
+ return;
390
+ } else {
391
+ return r._0;
392
+ }
393
+ }).flat();
394
+ if (errors.length > 0) {
395
+ return {
396
+ TAG: "Error",
397
+ _0: errors
398
+ };
399
+ }
400
+ let fields = Core__Array.filterMap(results, r => {
401
+ if (r.TAG === "Ok") {
402
+ return r._0;
403
+ }
404
+ });
405
+ return {
406
+ TAG: "Ok",
407
+ _0: {
408
+ _tag: "Object",
409
+ _0: fields
410
+ }
411
+ };
412
+ }
413
+ exit$1 = 2;
414
+ if (exit$1 === 2) {
415
+ return {
416
+ TAG: "Error",
417
+ _0: [Errors.makeError({
418
+ TAG: "InvalidJson",
419
+ _0: "properties must be an object"
420
+ }, undefined, undefined, undefined)]
421
+ };
422
+ }
423
+ }
424
+ }
425
+
426
+ function parseAllOf(items) {
427
+ let results = items.map(parseSchema);
428
+ let errors = Core__Array.filterMap(results, r => {
429
+ if (r.TAG === "Ok") {
430
+ return;
431
+ } else {
432
+ return r._0;
433
+ }
434
+ }).flat();
435
+ if (errors.length > 0) {
436
+ return {
437
+ TAG: "Error",
438
+ _0: errors
439
+ };
440
+ }
441
+ let allFields = Core__Array.filterMap(results, r => {
442
+ if (r.TAG !== "Ok") {
443
+ return;
444
+ }
445
+ let fields = r._0;
446
+ if (typeof fields !== "object" || fields._tag !== "Object") {
447
+ return;
448
+ } else {
449
+ return fields._0;
450
+ }
451
+ }).flat();
452
+ return {
453
+ TAG: "Ok",
454
+ _0: {
455
+ _tag: "Object",
456
+ _0: allFields
457
+ }
458
+ };
459
+ }
460
+
461
+ function parseOneOf(items) {
462
+ let caseResults = items.map(item => {
463
+ if (typeof item === "object" && item !== null && !Array.isArray(item)) {
464
+ let match = item["properties"];
465
+ let exit = 0;
466
+ if (match !== undefined) {
467
+ if (typeof match === "object" && match !== null && !Array.isArray(match)) {
468
+ let tag = extractTagFromConst(match);
469
+ if (tag === undefined) {
470
+ return {
471
+ TAG: "Error",
472
+ _0: [Errors.makeError({
473
+ TAG: "MissingRequiredField",
474
+ _0: "_tag with const"
475
+ }, undefined, undefined, undefined)]
476
+ };
477
+ }
478
+ let match$1 = item["required"];
479
+ let requiredFields = match$1 !== undefined ? (
480
+ Array.isArray(match$1) ? Core__Array.filterMap(match$1, i => {
481
+ if (typeof i === "string") {
482
+ return i;
483
+ }
484
+ }) : []
485
+ ) : [];
486
+ let entries = Object.entries(match).filter(param => param[0] !== "_tag");
487
+ let fieldResults = entries.map(param => {
488
+ let name = param[0];
489
+ let propType = parseSchema(param[1]);
490
+ if (propType.TAG === "Ok") {
491
+ return {
492
+ TAG: "Ok",
493
+ _0: {
494
+ name: name,
495
+ type: propType._0,
496
+ required: requiredFields.includes(name)
497
+ }
498
+ };
499
+ } else {
500
+ return {
501
+ TAG: "Error",
502
+ _0: propType._0
503
+ };
504
+ }
505
+ });
506
+ let errors = Core__Array.filterMap(fieldResults, r => {
507
+ if (r.TAG === "Ok") {
508
+ return;
509
+ } else {
510
+ return r._0;
511
+ }
512
+ }).flat();
513
+ if (errors.length > 0) {
514
+ return {
515
+ TAG: "Error",
516
+ _0: errors
517
+ };
518
+ }
519
+ let fields = Core__Array.filterMap(fieldResults, r => {
520
+ if (r.TAG === "Ok") {
521
+ return r._0;
522
+ }
523
+ });
524
+ return {
525
+ TAG: "Ok",
526
+ _0: {
527
+ _tag: tag,
528
+ payload: {
529
+ _tag: "Object",
530
+ _0: fields
531
+ }
532
+ }
533
+ };
534
+ }
535
+ exit = 2;
536
+ } else {
537
+ exit = 2;
538
+ }
539
+ if (exit === 2) {
540
+ return {
541
+ TAG: "Error",
542
+ _0: [Errors.makeError({
543
+ TAG: "InvalidJson",
544
+ _0: "oneOf item must have properties"
545
+ }, undefined, undefined, undefined)]
546
+ };
547
+ }
548
+ }
549
+ return {
550
+ TAG: "Error",
551
+ _0: [Errors.makeError({
552
+ TAG: "InvalidJson",
553
+ _0: "oneOf item must be object"
554
+ }, undefined, undefined, undefined)]
555
+ };
556
+ });
557
+ let errors = Core__Array.filterMap(caseResults, r => {
558
+ if (r.TAG === "Ok") {
559
+ return;
560
+ } else {
561
+ return r._0;
562
+ }
563
+ }).flat();
564
+ if (errors.length > 0) {
565
+ return {
566
+ TAG: "Error",
567
+ _0: errors
568
+ };
569
+ }
570
+ let cases = Core__Array.filterMap(caseResults, r => {
571
+ if (r.TAG === "Ok") {
572
+ return r._0;
573
+ }
574
+ });
575
+ return {
576
+ TAG: "Ok",
577
+ _0: {
578
+ _tag: "PolyVariant",
579
+ _0: cases
580
+ }
581
+ };
582
+ }
583
+
584
+ function parseObject(dict) {
585
+ let match = dict["$ref"];
586
+ if (match !== undefined) {
587
+ if (typeof match === "string") {
588
+ return {
589
+ TAG: "Ok",
590
+ _0: {
591
+ _tag: "Ref",
592
+ _0: extractRefName(match)
593
+ }
594
+ };
595
+ }
596
+ return {
597
+ TAG: "Error",
598
+ _0: [Errors.makeError({
599
+ TAG: "InvalidJson",
600
+ _0: "$ref must be a string"
601
+ }, undefined, undefined, undefined)]
602
+ };
603
+ }
604
+ let match$1 = dict["oneOf"];
605
+ if (match$1 !== undefined) {
606
+ if (Array.isArray(match$1)) {
607
+ return parseOneOf(match$1);
608
+ }
609
+ return {
610
+ TAG: "Error",
611
+ _0: [Errors.makeError({
612
+ TAG: "InvalidJson",
613
+ _0: "oneOf must be an array"
614
+ }, undefined, undefined, undefined)]
615
+ };
616
+ }
617
+ let match$2 = dict["allOf"];
618
+ if (match$2 !== undefined) {
619
+ if (Array.isArray(match$2)) {
620
+ return parseAllOf(match$2);
621
+ }
622
+ return {
623
+ TAG: "Error",
624
+ _0: [Errors.makeError({
625
+ TAG: "InvalidJson",
626
+ _0: "allOf must be an array"
627
+ }, undefined, undefined, undefined)]
628
+ };
629
+ }
630
+ let match$3 = dict["anyOf"];
631
+ if (match$3 === undefined) {
632
+ return parsePrimitiveType(dict);
633
+ }
634
+ if (Array.isArray(match$3)) {
635
+ return parseAnyOf(match$3);
636
+ }
637
+ return {
638
+ TAG: "Error",
639
+ _0: [Errors.makeError({
640
+ TAG: "InvalidJson",
641
+ _0: "anyOf must be an array"
642
+ }, undefined, undefined, undefined)]
643
+ };
644
+ }
645
+
646
+ let parse = parseSchema;
647
+
648
+ export {
649
+ isNullType,
650
+ hasDefault,
651
+ extractRefName,
652
+ parseEnumValues,
653
+ extractTagFromConst,
654
+ parseSchema,
655
+ parsePrimitiveType,
656
+ parseArrayType,
657
+ parseAnyOf,
658
+ parseObjectType,
659
+ parseAllOf,
660
+ parseOneOf,
661
+ parseObject,
662
+ parse,
663
+ }
664
+ /* No side effect */