osury 0.17.3 → 0.19.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.
@@ -3,6 +3,7 @@
3
3
  import * as Errors from "./Errors.res.mjs";
4
4
  import * as Core__Array from "@rescript/core/src/Core__Array.res.mjs";
5
5
  import * as Core__Option from "@rescript/core/src/Core__Option.res.mjs";
6
+ import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
6
7
 
7
8
  function isNullType(json) {
8
9
  if (typeof json !== "object" || json === null || Array.isArray(json)) {
@@ -50,6 +51,34 @@ function extractTagFromConst(dict) {
50
51
  }
51
52
  }
52
53
 
54
+ function extractTagFromProperty(dict, propertyName) {
55
+ let match = dict[propertyName];
56
+ if (match === undefined) {
57
+ return;
58
+ }
59
+ if (typeof match !== "object" || match === null || Array.isArray(match)) {
60
+ return;
61
+ }
62
+ let match$1 = match["const"];
63
+ if (typeof match$1 === "string") {
64
+ return match$1;
65
+ }
66
+ }
67
+
68
+ function extractDiscriminatorPropertyName(dict) {
69
+ let match = dict["discriminator"];
70
+ if (match === undefined) {
71
+ return;
72
+ }
73
+ if (typeof match !== "object" || match === null || Array.isArray(match)) {
74
+ return;
75
+ }
76
+ let match$1 = match["propertyName"];
77
+ if (typeof match$1 === "string") {
78
+ return match$1;
79
+ }
80
+ }
81
+
53
82
  function parseSchema(json) {
54
83
  if (typeof json === "object" && json !== null && !Array.isArray(json)) {
55
84
  return parseObject(json);
@@ -65,110 +94,115 @@ function parseSchema(json) {
65
94
 
66
95
  function parsePrimitiveType(dict) {
67
96
  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) {
97
+ if (match !== undefined) {
98
+ if (typeof match === "string") {
99
+ switch (match) {
100
+ case "array" :
101
+ return parseArrayType(dict);
102
+ case "boolean" :
103
+ return {
104
+ TAG: "Ok",
105
+ _0: "Boolean"
106
+ };
107
+ case "integer" :
108
+ return {
109
+ TAG: "Ok",
110
+ _0: "Integer"
111
+ };
112
+ case "null" :
113
+ return {
114
+ TAG: "Ok",
115
+ _0: "Null"
116
+ };
117
+ case "number" :
118
+ return {
119
+ TAG: "Ok",
120
+ _0: "Number"
121
+ };
122
+ case "object" :
123
+ return parseObjectType(dict);
124
+ case "string" :
125
+ let match$1 = dict["const"];
126
+ let exit = 0;
127
+ if (match$1 !== undefined) {
128
+ if (typeof match$1 === "string") {
129
129
  return {
130
130
  TAG: "Ok",
131
131
  _0: {
132
132
  _tag: "Enum",
133
- _0: values
133
+ _0: [match$1]
134
134
  }
135
135
  };
136
- } else {
136
+ }
137
+ exit = 2;
138
+ } else {
139
+ exit = 2;
140
+ }
141
+ if (exit === 2) {
142
+ let match$2 = dict["enum"];
143
+ if (match$2 === undefined) {
144
+ return {
145
+ TAG: "Ok",
146
+ _0: "String"
147
+ };
148
+ }
149
+ let exit$1 = 0;
150
+ if (Array.isArray(match$2)) {
151
+ let values = parseEnumValues(match$2);
152
+ if (values !== undefined) {
153
+ return {
154
+ TAG: "Ok",
155
+ _0: {
156
+ _tag: "Enum",
157
+ _0: values
158
+ }
159
+ };
160
+ } else {
161
+ return {
162
+ TAG: "Error",
163
+ _0: [Errors.makeError({
164
+ TAG: "InvalidJson",
165
+ _0: "enum values must be strings"
166
+ }, undefined, undefined, undefined)]
167
+ };
168
+ }
169
+ }
170
+ exit$1 = 3;
171
+ if (exit$1 === 3) {
137
172
  return {
138
173
  TAG: "Error",
139
174
  _0: [Errors.makeError({
140
175
  TAG: "InvalidJson",
141
- _0: "enum values must be strings"
176
+ _0: "enum must be an array"
142
177
  }, undefined, undefined, undefined)]
143
178
  };
144
179
  }
145
180
  }
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
- };
181
+ break;
182
+ default:
183
+ return {
184
+ TAG: "Error",
185
+ _0: [Errors.unknownType(match, undefined, undefined, undefined)]
186
+ };
187
+ }
163
188
  }
189
+ return {
190
+ TAG: "Error",
191
+ _0: [Errors.makeError({
192
+ TAG: "InvalidJson",
193
+ _0: "type must be a string"
194
+ }, undefined, undefined, undefined)]
195
+ };
196
+ }
197
+ let match$3 = dict["properties"];
198
+ if (match$3 !== undefined) {
199
+ return parseObjectType(dict);
200
+ } else {
201
+ return {
202
+ TAG: "Ok",
203
+ _0: "Unknown"
204
+ };
164
205
  }
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
206
  }
173
207
 
174
208
  function parseArrayType(dict) {
@@ -458,76 +492,23 @@ function parseAllOf(items) {
458
492
  };
459
493
  }
460
494
 
461
- function parseOneOf(items) {
495
+ function parseOneOf(items, discriminatorPropertyNameOpt) {
496
+ let discriminatorPropertyName = discriminatorPropertyNameOpt !== undefined ? Primitive_option.valFromOption(discriminatorPropertyNameOpt) : undefined;
497
+ let propName = Core__Option.getOr(discriminatorPropertyName, "_tag");
462
498
  let caseResults = items.map(item => {
463
499
  if (typeof item === "object" && item !== null && !Array.isArray(item)) {
464
- let match = item["properties"];
500
+ let match = item["$ref"];
465
501
  let exit = 0;
466
502
  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
- });
503
+ if (typeof match === "string") {
504
+ let name = extractRefName(match);
524
505
  return {
525
506
  TAG: "Ok",
526
507
  _0: {
527
- _tag: tag,
508
+ _tag: name,
528
509
  payload: {
529
- _tag: "Object",
530
- _0: fields
510
+ _tag: "Ref",
511
+ _0: name
531
512
  }
532
513
  }
533
514
  };
@@ -537,13 +518,90 @@ function parseOneOf(items) {
537
518
  exit = 2;
538
519
  }
539
520
  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
- };
521
+ let match$1 = item["properties"];
522
+ let exit$1 = 0;
523
+ if (match$1 !== undefined) {
524
+ if (typeof match$1 === "object" && match$1 !== null && !Array.isArray(match$1)) {
525
+ let tag = extractTagFromProperty(match$1, propName);
526
+ if (tag === undefined) {
527
+ return {
528
+ TAG: "Error",
529
+ _0: [Errors.makeError({
530
+ TAG: "MissingRequiredField",
531
+ _0: propName + " with const"
532
+ }, undefined, undefined, undefined)]
533
+ };
534
+ }
535
+ let match$2 = item["required"];
536
+ let requiredFields = match$2 !== undefined ? (
537
+ Array.isArray(match$2) ? Core__Array.filterMap(match$2, i => {
538
+ if (typeof i === "string") {
539
+ return i;
540
+ }
541
+ }) : []
542
+ ) : [];
543
+ let entries = Object.entries(match$1).filter(param => param[0] !== propName);
544
+ let fieldResults = entries.map(param => {
545
+ let name = param[0];
546
+ let propType = parseSchema(param[1]);
547
+ if (propType.TAG === "Ok") {
548
+ return {
549
+ TAG: "Ok",
550
+ _0: {
551
+ name: name,
552
+ type: propType._0,
553
+ required: requiredFields.includes(name)
554
+ }
555
+ };
556
+ } else {
557
+ return {
558
+ TAG: "Error",
559
+ _0: propType._0
560
+ };
561
+ }
562
+ });
563
+ let errors = Core__Array.filterMap(fieldResults, r => {
564
+ if (r.TAG === "Ok") {
565
+ return;
566
+ } else {
567
+ return r._0;
568
+ }
569
+ }).flat();
570
+ if (errors.length > 0) {
571
+ return {
572
+ TAG: "Error",
573
+ _0: errors
574
+ };
575
+ }
576
+ let fields = Core__Array.filterMap(fieldResults, r => {
577
+ if (r.TAG === "Ok") {
578
+ return r._0;
579
+ }
580
+ });
581
+ return {
582
+ TAG: "Ok",
583
+ _0: {
584
+ _tag: tag,
585
+ payload: {
586
+ _tag: "Object",
587
+ _0: fields
588
+ }
589
+ }
590
+ };
591
+ }
592
+ exit$1 = 3;
593
+ } else {
594
+ exit$1 = 3;
595
+ }
596
+ if (exit$1 === 3) {
597
+ return {
598
+ TAG: "Error",
599
+ _0: [Errors.makeError({
600
+ TAG: "InvalidJson",
601
+ _0: "oneOf item must have properties"
602
+ }, undefined, undefined, undefined)]
603
+ };
604
+ }
547
605
  }
548
606
  }
549
607
  return {
@@ -604,7 +662,8 @@ function parseObject(dict) {
604
662
  let match$1 = dict["oneOf"];
605
663
  if (match$1 !== undefined) {
606
664
  if (Array.isArray(match$1)) {
607
- return parseOneOf(match$1);
665
+ let discriminatorPropName = extractDiscriminatorPropertyName(dict);
666
+ return parseOneOf(match$1, Primitive_option.some(discriminatorPropName));
608
667
  }
609
668
  return {
610
669
  TAG: "Error",
@@ -651,6 +710,8 @@ export {
651
710
  extractRefName,
652
711
  parseEnumValues,
653
712
  extractTagFromConst,
713
+ extractTagFromProperty,
714
+ extractDiscriminatorPropertyName,
654
715
  parseSchema,
655
716
  parsePrimitiveType,
656
717
  parseArrayType,