sury
Version:
🧬 The fastest parser in the entire JavaScript ecosystem with a focus on small bundle size and top-notch DX
1,742 lines (1,578 loc) • 134 kB
JavaScript
function some(x) {
if (x === undefined) {
return {
BS_PRIVATE_NESTED_SOME_NONE: 0
};
} else if (x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== undefined) {
return {
BS_PRIVATE_NESTED_SOME_NONE: x.BS_PRIVATE_NESTED_SOME_NONE + 1 | 0
};
} else {
return x;
}
}
function valFromOption(x) {
if (!(x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== undefined)) {
return x;
}
var depth = x.BS_PRIVATE_NESTED_SOME_NONE;
if (depth === 0) {
return ;
} else {
return {
BS_PRIVATE_NESTED_SOME_NONE: depth - 1 | 0
};
}
}
/* No side effect */
function get$2(dict, k) {
if ((k in dict)) {
return some(dict[k]);
}
}
var unsafeDeleteKey = (function (dict,key){
delete dict[key];
});
/* No side effect */
function length(xs) {
var _x = xs;
var _acc = 0;
while(true) {
var acc = _acc;
var x = _x;
if (!x) {
return acc;
}
_acc = acc + 1 | 0;
_x = x.tl;
continue ;
}}
function fillAux(arr, _i, _x) {
while(true) {
var x = _x;
var i = _i;
if (!x) {
return ;
}
arr[i] = x.hd;
_x = x.tl;
_i = i + 1 | 0;
continue ;
}}
function fromArray$1(a) {
var _i = a.length - 1 | 0;
var _res = /* [] */0;
while(true) {
var res = _res;
var i = _i;
if (i < 0) {
return res;
}
_res = {
hd: a[i],
tl: res
};
_i = i - 1 | 0;
continue ;
}}
function toArray$1(x) {
var len = length(x);
var arr = new Array(len);
fillAux(arr, 0, x);
return arr;
}
/* No side effect */
var idMap = new Map();
function create(str) {
var v = idMap.get(str);
var id;
if (v !== undefined) {
var id$1 = v + 1 | 0;
idMap.set(str, id$1);
id = id$1;
} else {
idMap.set(str, 1);
id = 1;
}
return str + ("/" + id);
}
/* idMap Not a pure module */
// Generated by ReScript, PLEASE EDIT WITH CARE
var immutableEmpty = {};
function fromString(string) {
var _idx = 0;
while(true) {
var idx = _idx;
var match = string[idx];
if (match === undefined) {
return "\"" + string + "\"";
}
if (match === "\"") {
return JSON.stringify(string);
}
_idx = idx + 1 | 0;
continue ;
}}
function toArray(path) {
if (path === "") {
return [];
} else {
return JSON.parse(path.split("\"][\"").join("\",\""));
}
}
function fromLocation($$location) {
return "[" + fromString($$location) + "]";
}
function fromArray(array) {
var len = array.length;
if (len !== 1) {
if (len !== 0) {
return "[" + array.map(fromString).join("][") + "]";
} else {
return "";
}
}
var $$location = array[0];
return "[" + fromString($$location) + "]";
}
function concat(path, concatedPath) {
return path + concatedPath;
}
var symbol = Symbol("rescript-schema");
var itemSymbol = Symbol("item");
var Raised = /* @__PURE__ */create("S_Core-RescriptSchema.Raised");
var globalConfig = {
r: 0,
u: "Strip",
n: false
};
class RescriptSchemaError extends Error {
constructor(code, flag, path) {
super();
this.flag = flag;
this.code = code;
this.path = path;
this.s = symbol;
this.RE_EXN_ID = Raised;
this._1 = this;
this.Error = this;
this.name = "RescriptSchemaError";
}
get message() {
return message(this);
}
get reason() {
return reason(this);
}
}
function getOrRethrow(exn) {
if ((exn&&exn.s===symbol)) {
return exn;
}
throw exn;
}
function has(acc, flag) {
return (acc & flag) !== 0;
}
function embed(b, value) {
var e = b.g.e;
var l = e.length;
e[l] = value;
return "e[" + l + "]";
}
function secondAllocate(v) {
var b = this;
b.l = b.l + "," + v;
}
function initialAllocate(v) {
var b = this;
b.l = v;
b.a = secondAllocate;
}
function rootScope(flag) {
var $$global = {
c: "",
l: "",
a: initialAllocate,
v: -1,
o: flag,
e: []
};
$$global.g = $$global;
return $$global;
}
function allocateScope(b) {
((delete b.a));
var varsAllocation = b.l;
if (varsAllocation === "") {
return b.c;
} else {
return "let " + varsAllocation + ";" + b.c;
}
}
function varWithoutAllocation($$global) {
var newCounter = $$global.v + 1;
$$global.v = newCounter;
return "v" + newCounter;
}
function _var(_b) {
return this.i;
}
function _notVar(b) {
var val = this;
var v = varWithoutAllocation(b.g);
var i = val.i;
if (i === "") {
val.b.a(v);
} else if (val.b.a !== (void 0)) {
val.b.a(v + "=" + i);
} else {
b.c = b.c + (v + "=" + i + ";");
b.g.a(v);
}
val.v = _var;
val.i = v;
return v;
}
function allocateVal(b) {
var v = varWithoutAllocation(b.g);
b.a(v);
return {
b: b,
v: _var,
i: v,
a: false
};
}
function objectJoin(inlinedLocation, value) {
return inlinedLocation + ":" + value + ",";
}
function arrayJoin(_inlinedLocation, value) {
return value + ",";
}
function make(b, isArray) {
return {
b: b,
v: _notVar,
i: "",
a: false,
j: isArray ? arrayJoin : objectJoin,
c: 0,
p: ""
};
}
function add(objectVal, inlinedLocation, val) {
objectVal[inlinedLocation] = val;
if (val.a) {
objectVal.p = objectVal.p + val.i + ",";
objectVal.i = objectVal.i + objectVal.j(inlinedLocation, "a[" + (objectVal.c++) + "]");
} else {
objectVal.i = objectVal.i + objectVal.j(inlinedLocation, val.i);
}
}
function merge(target, subObjectVal) {
var inlinedLocations = Object.keys(subObjectVal);
for(var idx = 7 ,idx_finish = inlinedLocations.length; idx < idx_finish; ++idx){
var inlinedLocation = inlinedLocations[idx];
add(target, inlinedLocation, subObjectVal[inlinedLocation]);
}
}
function complete(objectVal, isArray) {
objectVal.i = isArray ? "[" + objectVal.i + "]" : "{" + objectVal.i + "}";
if (objectVal.c) {
objectVal.a = true;
objectVal.i = "Promise.all([" + objectVal.p + "]).then(a=>(" + objectVal.i + "))";
}
return objectVal;
}
function addKey(b, input, key, val) {
return input.v(b) + "[" + key + "]=" + val.i;
}
function set(b, input, val) {
if (input === val) {
return "";
}
var inputVar = input.v(b);
var match = input.a;
if (match) {
var match$1 = val.a;
if (!match$1) {
return inputVar + "=Promise.resolve(" + val.i + ")";
}
} else {
var match$2 = val.a;
if (match$2) {
input.a = true;
return inputVar + "=" + val.i;
}
}
return inputVar + "=" + val.i;
}
function get(b, targetVal, inlinedLocation) {
var val = targetVal[inlinedLocation];
if (val !== undefined) {
return val;
} else {
return {
b: b,
v: _notVar,
i: targetVal.v(b) + ("[" + inlinedLocation + "]"),
a: false
};
}
}
function setInlined(b, input, inlined) {
return input.v(b) + "=" + inlined;
}
function map(inlinedFn, input) {
return {
b: input.b,
v: _notVar,
i: inlinedFn + "(" + input.i + ")",
a: false
};
}
function transform(b, input, operation) {
if (!input.a) {
return operation(b, input);
}
var bb = {
c: "",
l: "",
a: initialAllocate,
g: b.g
};
var operationInput = {
b: b,
v: _var,
i: varWithoutAllocation(bb.g),
a: false
};
var operationOutputVal = operation(bb, operationInput);
var operationCode = allocateScope(bb);
return {
b: input.b,
v: _notVar,
i: input.i + ".then(" + operationInput.v(b) + "=>{" + operationCode + "return " + operationOutputVal.i + "})",
a: true
};
}
function raise(b, code, path) {
throw new RescriptSchemaError(code, b.g.o, path);
}
function embedSyncOperation(b, input, fn) {
if (input.a) {
return {
b: input.b,
v: _notVar,
i: input.i + ".then(" + embed(b, fn) + ")",
a: true
};
} else {
return map(embed(b, fn), input);
}
}
function embedAsyncOperation(b, input, fn) {
if (!(b.g.o & 2)) {
raise(b, "UnexpectedAsync", "");
}
var val = embedSyncOperation(b, input, fn);
val.a = true;
return val;
}
function failWithArg(b, path, fn, arg) {
return embed(b, (function (arg) {
return raise(b, fn(arg), path);
})) + "(" + arg + ")";
}
function fail(b, message, path) {
return embed(b, (function () {
return raise(b, {
TAG: "OperationFailed",
_0: message
}, path);
})) + "()";
}
function effectCtx(b, selfSchema, path) {
return {
schema: selfSchema,
fail: (function (message, customPathOpt) {
var customPath = customPathOpt !== undefined ? customPathOpt : "";
return raise(b, {
TAG: "OperationFailed",
_0: message
}, path + customPath);
})
};
}
function registerInvalidJson(b, selfSchema, path) {
if (b.g.o & 8) {
return raise(b, {
TAG: "InvalidJsonSchema",
_0: selfSchema
}, path);
}
}
function invalidOperation(b, path, description) {
return raise(b, {
TAG: "InvalidOperation",
description: description
}, path);
}
function withCatch(b, input, $$catch, appendSafe, fn) {
var prevCode = b.c;
b.c = "";
var errorVar = varWithoutAllocation(b.g);
var maybeResolveVal = $$catch(b, errorVar);
var catchCode = "if(" + (errorVar + "&&" + errorVar + ".s===s") + "){" + b.c;
b.c = "";
var bb = {
c: "",
l: "",
a: initialAllocate,
g: b.g
};
var fnOutput = fn(bb);
b.c = b.c + allocateScope(bb);
var isNoop = fnOutput.i === input.i && b.c === "";
if (appendSafe !== undefined) {
appendSafe(b, fnOutput);
}
if (isNoop) {
return fnOutput;
}
var isAsync = fnOutput.a;
var output = input === fnOutput ? input : (
appendSafe !== undefined ? fnOutput : ({
b: b,
v: _notVar,
i: "",
a: isAsync
})
);
var catchCode$1 = maybeResolveVal !== undefined ? (function (catchLocation) {
return catchCode + (
catchLocation === 1 ? "return " + maybeResolveVal.i : set(b, output, maybeResolveVal)
) + ("}else{throw " + errorVar + "}");
}) : (function (param) {
return catchCode + "}throw " + errorVar;
});
b.c = prevCode + ("try{" + b.c + (
isAsync ? setInlined(b, output, fnOutput.i + ".catch(" + errorVar + "=>{" + catchCode$1(1) + "})") : set(b, output, fnOutput)
) + "}catch(" + errorVar + "){" + catchCode$1(0) + "}");
return output;
}
function withPathPrepend(b, input, path, maybeDynamicLocationVar, appendSafe, fn) {
if (path === "" && maybeDynamicLocationVar === undefined) {
return fn(b, input, path);
}
try {
return withCatch(b, input, (function (b, errorVar) {
b.c = errorVar + ".path=" + fromString(path) + "+" + (
maybeDynamicLocationVar !== undefined ? "'[\"'+" + maybeDynamicLocationVar + "+'\"]'+" : ""
) + errorVar + ".path";
}), appendSafe, (function (b) {
return fn(b, input, "");
}));
}
catch (exn){
var error = getOrRethrow(exn);
throw new RescriptSchemaError(error.code, error.flag, path + "[]" + error.path);
}
}
function typeFilterCode(b, schema, input, path) {
var inputVar = input.v(b);
return "if(" + schema.f(b, inputVar) + "){" + failWithArg(b, path, (function (input) {
return {
TAG: "InvalidType",
expected: schema,
received: input
};
}), inputVar) + "}";
}
function parseWithTypeValidation(b, schema, input, path) {
if (schema.f !== undefined && (b.g.o & 1 || schema.t.TAG === "literal")) {
b.c = b.c + typeFilterCode(b, schema, input, path);
}
return schema.b(b, input, schema, path);
}
function noop(_b, input, param, param$1) {
return input;
}
function invalidJson(b, input, selfSchema, path) {
registerInvalidJson(b, selfSchema, path);
return input;
}
function noopOperation(i) {
return i;
}
function compile(builder, schema, flag) {
if (flag & 8 && schema["~r"]().t.TAG === "option") {
throw new RescriptSchemaError({
TAG: "InvalidJsonSchema",
_0: schema
}, flag, "");
}
var b = rootScope(flag);
var input = {
b: b,
v: _var,
i: "i",
a: false
};
var output = builder(b, input, schema, "");
schema.i = output.a;
if (b.l !== "") {
b.c = "let " + b.l + ";" + b.c;
}
if (schema.f !== undefined && (flag & 1 || schema.t.TAG === "literal")) {
b.c = typeFilterCode(b, schema, input, "") + b.c;
}
if (b.c === "" && output === input && !(flag & 22)) {
return noopOperation;
}
var inlinedOutput = flag & 4 ? "void 0" : output.i;
if (flag & 16) {
inlinedOutput = "JSON.stringify(" + inlinedOutput + ")";
}
if (flag & 2 && !output.a) {
inlinedOutput = "Promise.resolve(" + inlinedOutput + ")";
}
var inlinedFunction = "i=>{" + b.c + "return " + inlinedOutput + "}";
return new Function("e", "s", "return " + inlinedFunction)(b.g.e, symbol);
}
function operationFn(s, o) {
if ((o in s)) {
return (s[o]);
}
var ss = o & 32 ? s["~r"]() : s;
var f = compile(ss.b, ss, o);
((s[o] = f));
return f;
}
function compile$1(schema, input, output, mode, typeValidationOpt) {
var typeValidation = typeValidationOpt !== undefined ? typeValidationOpt : true;
var flag = 0;
var exit = 0;
switch (output) {
case "Output" :
case "Input" :
exit = 1;
break;
case "Assert" :
flag = flag | 4;
break;
case "Json" :
flag = flag | 8;
break;
case "JsonString" :
flag = flag | 24;
break;
}
if (exit === 1 && output === input) {
throw new Error("[rescript-schema] Can't compile operation to converting value to self");
}
if (mode !== "Sync") {
flag = flag | 2;
}
if (typeValidation) {
flag = flag | 1;
}
if (input === "Output") {
flag = flag | 32;
}
var fn = operationFn(schema, flag);
if (input !== "JsonString") {
return fn;
}
var flag$1 = flag;
return function (jsonString) {
try {
return fn(JSON.parse(jsonString));
}
catch (exn){
throw new RescriptSchemaError({
TAG: "OperationFailed",
_0: exn.message
}, flag$1, "");
}
};
}
function toSelf() {
return this;
}
function onlyChild(factory, schema) {
return function () {
var reversed = schema["~r"]();
if (reversed === schema) {
return this;
} else {
return factory(reversed);
}
};
}
function value(literal) {
return literal.value;
}
function isJsonable(literal) {
return literal.j;
}
function toString(literal) {
return literal.s;
}
function arrayFilterBuilder(b, inputVar, literal) {
var items = literal.i;
return inputVar + "!==" + embed(b, literal.value) + "&&(!Array.isArray(" + inputVar + ")||" + inputVar + ".length!==" + items.length + (
items.length > 0 ? "||" + items.map(function (literal, idx) {
return literal.f(b, inputVar + "[" + idx + "]", literal);
}).join("||") : ""
) + ")";
}
function dictFilterBuilder(b, inputVar, literal) {
var items = literal.i;
var fields = Object.keys(items);
var numberOfFields = fields.length;
return inputVar + "!==" + embed(b, value) + "&&(typeof " + inputVar + "!==\"object\"||!" + inputVar + "||Object.keys(" + inputVar + ").length!==" + numberOfFields + (
numberOfFields > 0 ? "||" + fields.map(function (field) {
var literal = items[field];
return literal.f(b, inputVar + "[" + fromString(field) + "]", literal);
}).join("||") : ""
) + ")";
}
function inlinedStrictEqualFilterBuilder(param, inputVar, literal) {
return inputVar + "!==" + literal.s;
}
function strictEqualFilterBuilder(b, inputVar, literal) {
return inputVar + "!==" + embed(b, literal.value);
}
var undefined_value = undefined;
var $$undefined = {
kind: "Undefined",
value: undefined_value,
s: "undefined",
f: inlinedStrictEqualFilterBuilder,
j: false
};
var null_value = null;
var $$null = {
kind: "Null",
value: null_value,
s: "null",
f: inlinedStrictEqualFilterBuilder,
j: true
};
var nan_value = NaN;
function nan_f(param, inputVar, param$1) {
return "!Number.isNaN(" + inputVar + ")";
}
var nan = {
kind: "NaN",
value: nan_value,
s: "NaN",
f: nan_f,
j: false
};
function parseInternal(value) {
var typeOfValue = typeof value;
if (typeOfValue === "symbol") {
return {
kind: "Symbol",
value: value,
s: value.toString(),
f: strictEqualFilterBuilder,
j: false
};
} else if (typeOfValue === "boolean") {
return {
kind: "Boolean",
value: value,
s: value ? "true" : "false",
f: inlinedStrictEqualFilterBuilder,
j: true
};
} else if (typeOfValue === "string") {
return {
kind: "String",
value: value,
s: fromString(value),
f: inlinedStrictEqualFilterBuilder,
j: true
};
} else if (typeOfValue === "function") {
return {
kind: "Function",
value: value,
s: value.toString(),
f: strictEqualFilterBuilder,
j: false
};
} else if (typeOfValue === "object") {
if (value === null) {
return $$null;
} else if (Array.isArray(value)) {
var items = [];
var isJsonable = true;
var string = "[";
for(var idx = 0 ,idx_finish = value.length; idx < idx_finish; ++idx){
var itemValue = value[idx];
var itemLiteral = parseInternal(itemValue);
if (isJsonable && !itemLiteral.j) {
isJsonable = false;
}
if (idx !== 0) {
string = string + ", ";
}
string = string + itemLiteral.s;
items.push(itemLiteral);
}
return {
kind: "Array",
value: value,
s: string + "]",
f: arrayFilterBuilder,
j: isJsonable,
i: some(items)
};
} else if (value.constructor === Object) {
var items$1 = {};
var string$1 = "{ ";
var isJsonable$1 = true;
var fields = Object.keys(value);
var numberOfFields = fields.length;
for(var idx$1 = 0; idx$1 < numberOfFields; ++idx$1){
var field = fields[idx$1];
var itemValue$1 = value[field];
var itemLiteral$1 = parseInternal(itemValue$1);
if (isJsonable$1 && !itemLiteral$1.j) {
isJsonable$1 = false;
}
if (idx$1 !== 0) {
string$1 = string$1 + ", ";
}
string$1 = string$1 + (fromString(field) + ": " + itemLiteral$1.s);
items$1[field] = itemLiteral$1;
}
return {
kind: "Dict",
value: value,
s: string$1 + " }",
f: dictFilterBuilder,
j: isJsonable$1,
i: some(items$1)
};
} else {
return {
kind: "Object",
value: value,
s: Object.prototype.toString.call(value),
f: strictEqualFilterBuilder,
j: false
};
}
} else if (typeOfValue === "undefined") {
return $$undefined;
} else if (typeOfValue === "number") {
if (Number.isNaN(value)) {
return nan;
} else {
return {
kind: "Number",
value: value,
s: value.toString(),
f: inlinedStrictEqualFilterBuilder,
j: true
};
}
} else {
return {
kind: "BigInt",
value: value,
s: value.toString() + "n",
f: inlinedStrictEqualFilterBuilder,
j: false
};
}
}
function parse(any) {
return parseInternal(any);
}
function isAsync(schema) {
var v = schema.i;
if (typeof v === "boolean") {
return v;
}
try {
var b = rootScope(2);
var input = {
b: b,
v: _var,
i: "i",
a: false
};
var output = schema.b(b, input, schema, "");
schema.i = output.a;
return schema.i;
}
catch (exn){
getOrRethrow(exn);
return false;
}
}
function reverse(schema) {
return schema["~r"]();
}
function parseOrThrow(any, schema) {
return operationFn(schema, 1)(any);
}
function parseJsonStringOrThrow(jsonString, schema) {
var tmp;
try {
tmp = JSON.parse(jsonString);
}
catch (exn){
throw new RescriptSchemaError({
TAG: "OperationFailed",
_0: exn.message
}, 1, "");
}
return parseOrThrow(tmp, schema);
}
function parseAsyncOrThrow(any, schema) {
return operationFn(schema, 3)(any);
}
function convertOrThrow(input, schema) {
return operationFn(schema, 0)(input);
}
function convertToJsonOrThrow(any, schema) {
return operationFn(schema, 8)(any);
}
function convertToJsonStringOrThrow(input, schema) {
return operationFn(schema, 24)(input);
}
function convertAsyncOrThrow(any, schema) {
return operationFn(schema, 2)(any);
}
function reverseConvertOrThrow(value, schema) {
return operationFn(schema, 32)(value);
}
function reverseConvertToJsonOrThrow(value, schema) {
return operationFn(schema, 40)(value);
}
function reverseConvertToJsonStringOrThrow(value, schema, spaceOpt) {
var space = spaceOpt !== undefined ? spaceOpt : 0;
return JSON.stringify(reverseConvertToJsonOrThrow(value, schema), null, space);
}
function assertOrThrow(any, schema) {
return operationFn(schema, 5)(any);
}
function wrapExnToFailure(exn) {
if ((exn&&exn.s===symbol)) {
return {
success: false,
error: exn
};
}
throw exn;
}
function js_safe(fn) {
try {
return {
success: true,
value: fn()
};
}
catch (exn){
return wrapExnToFailure(exn);
}
}
function js_safeAsync(fn) {
try {
return fn().then((function (value) {
return {
success: true,
value: value
};
}), wrapExnToFailure);
}
catch (exn){
return Promise.resolve(wrapExnToFailure(exn));
}
}
function makeReverseSchema(name, tagged, metadataMap, builder, maybeTypeFilter) {
return {
t: tagged,
n: name,
"~r": toSelf,
b: builder,
f: maybeTypeFilter,
i: 0,
m: metadataMap
};
}
function makeSchema(name, tagged, metadataMap, builder, maybeTypeFilter, reverse) {
return {
t: tagged,
n: name,
"~r": (function () {
var original = this;
var reversed = reverse.call(original);
var reversed$1 = original !== reversed && typeof reversed.t === "string" ? makeReverseSchema(reversed.n, reversed.t, reversed.m, reversed.b, reversed.f) : reversed;
original["~r"] = (function () {
return reversed$1;
});
reversed$1["~r"] = (function () {
return original;
});
return reversed$1;
}),
b: builder,
f: maybeTypeFilter,
i: 0,
m: metadataMap
};
}
function make$1(namespace, name) {
return namespace + ":" + name;
}
var Id = {
make: make$1
};
var empty = {};
function set$1(map, id, metadata) {
if (map === empty) {
return ({[id]:metadata});
}
var copy = Object.assign({}, map);
copy[id] = metadata;
return copy;
}
function get$1(schema, id) {
return schema.m[id];
}
function set$2(schema, id, metadata) {
var metadataMap = set$1(schema.m, id, metadata);
return makeSchema(schema.n, schema.t, metadataMap, schema.b, schema.f, (function () {
var schema$1 = schema["~r"]();
return makeReverseSchema(schema$1.n, schema$1.t, metadataMap, schema$1.b, schema$1.f);
}));
}
function primitiveName() {
return this.t;
}
function makePrimitiveSchema(tagged, builder, maybeTypeFilter) {
return makeSchema(primitiveName, tagged, empty, builder, maybeTypeFilter, toSelf);
}
function recursive(fn) {
var r = "r" + globalConfig.r;
globalConfig.r = globalConfig.r + 1 | 0;
var placeholder = {
m: empty,
t: "unknown",
n: (function () {
return "<recursive>";
}),
b: (function (b, input, param, param$1) {
return transform(b, input, (function (_b, input) {
return map(r, input);
}));
}),
"~r": (function () {
return makeReverseSchema(primitiveName, "unknown", empty, (function (_b, input, param, param$1) {
return map(r, input);
}), undefined);
})
};
var schema = fn(placeholder);
placeholder.f = schema.f;
placeholder.t = schema.t;
var initialParseOperationBuilder = schema.b;
schema.b = (function (b, input, selfSchema, path) {
var inputVar = input.v(b);
var bb = {
c: "",
l: "",
a: initialAllocate,
g: b.g
};
var opOutput = initialParseOperationBuilder(bb, input, selfSchema, "");
var opBodyCode = allocateScope(bb) + ("return " + opOutput.i);
b.c = b.c + ("let " + r + "=" + inputVar + "=>{" + opBodyCode + "};");
return withPathPrepend(b, input, path, undefined, undefined, (function (b, input, param) {
return transform(b, input, (function (_b, input) {
var output = map(r, input);
if (opOutput.a) {
output.a = true;
placeholder.b = (function (b, input, param, param$1) {
return transform(b, input, (function (_b, input) {
var output = map(r, input);
output.a = true;
return output;
}));
});
}
return output;
}));
}));
});
var initialReverse = schema["~r"].bind(schema);
schema["~r"] = (function () {
var initialReversed = initialReverse();
var reversed = makeReverseSchema(initialReversed.n, initialReversed.t, initialReversed.m, (function (b, input, selfSchema, path) {
var inputVar = input.v(b);
var bb = {
c: "",
l: "",
a: initialAllocate,
g: b.g
};
var initialInput = {
b: bb,
v: input.v,
i: input.i,
a: input.a
};
var opOutput = initialReversed.b(bb, initialInput, selfSchema, "");
var opBodyCode = allocateScope(bb) + ("return " + opOutput.i);
b.c = b.c + ("let " + r + "=" + inputVar + "=>{" + opBodyCode + "};");
return withPathPrepend(b, input, path, undefined, undefined, (function (_b, input, param) {
return map(r, input);
}));
}), initialReversed.f);
reversed["~r"] = (function () {
return schema;
});
schema["~r"] = (function () {
return reversed;
});
return reversed;
});
return schema;
}
function setName(schema, name) {
return makeSchema((function () {
return name;
}), schema.t, schema.m, schema.b, schema.f, (function () {
return schema["~r"]();
}));
}
function removeTypeValidation(schema) {
return makeSchema(schema.n, schema.t, schema.m, schema.b, undefined, (function () {
return schema["~r"]();
}));
}
function internalRefine(schema, refiner) {
return makeSchema(schema.n, schema.t, schema.m, (function (b, input, selfSchema, path) {
return transform(b, schema.b(b, input, schema, path), (function (b, input) {
var bb = {
c: "",
l: "",
a: initialAllocate,
g: b.g
};
var rCode = refiner(bb, input.v(bb), selfSchema, path);
b.c = b.c + allocateScope(bb) + rCode;
return input;
}));
}), schema.f, (function () {
var schema$1 = schema["~r"]();
return makeReverseSchema(schema$1.n, schema$1.t, schema$1.m, (function (b, input, selfSchema, path) {
var input$1 = transform(b, input, (function (b, input) {
b.c = b.c + refiner(b, input.v(b), selfSchema, path);
return input;
}));
return schema$1.b(b, input$1, schema$1, path);
}), schema$1.f);
}));
}
function refine(schema, refiner) {
return internalRefine(schema, (function (b, inputVar, selfSchema, path) {
return embed(b, refiner(effectCtx(b, selfSchema, path))) + "(" + inputVar + ");";
}));
}
function addRefinement(schema, metadataId, refinement, refiner) {
var refinements = schema.m[metadataId];
return internalRefine(set$2(schema, metadataId, refinements !== undefined ? refinements.concat(refinement) : [refinement]), refiner);
}
function transform$1(schema, transformer) {
return makeSchema(schema.n, schema.t, schema.m, (function (b, input, selfSchema, path) {
var input$1 = schema.b(b, input, schema, path);
var match = transformer(effectCtx(b, selfSchema, path));
var parser = match.p;
if (parser !== undefined) {
if (match.a !== undefined) {
return invalidOperation(b, path, "The S.transform doesn't allow parser and asyncParser at the same time. Remove parser in favor of asyncParser");
} else {
return embedSyncOperation(b, input$1, parser);
}
}
var asyncParser = match.a;
if (asyncParser !== undefined) {
return embedAsyncOperation(b, input$1, asyncParser);
} else if (match.s !== undefined) {
return invalidOperation(b, path, "The S.transform parser is missing");
} else {
return input$1;
}
}), schema.f, (function () {
var schema$1 = schema["~r"]();
return makeReverseSchema(primitiveName, "unknown", empty, (function (b, input, selfSchema, path) {
var match = transformer(effectCtx(b, selfSchema, path));
var serializer = match.s;
if (serializer === undefined) {
if (match.a !== undefined || match.p !== undefined) {
return invalidOperation(b, path, "The S.transform serializer is missing");
} else {
return schema$1.b(b, input, schema$1, path);
}
}
var input$1 = embedSyncOperation(b, input, serializer);
return schema$1.b(b, input$1, schema$1, path);
}), undefined);
}));
}
function custom(name, definer) {
return makeSchema((function () {
return name;
}), "unknown", empty, (function (b, input, selfSchema, path) {
registerInvalidJson(b, selfSchema, path);
var match = definer(effectCtx(b, selfSchema, path));
var parser = match.p;
if (parser !== undefined) {
if (match.a !== undefined) {
return invalidOperation(b, path, "The S.custom doesn't allow parser and asyncParser at the same time. Remove parser in favor of asyncParser");
} else {
return embedSyncOperation(b, input, parser);
}
}
var asyncParser = match.a;
if (asyncParser !== undefined) {
return embedAsyncOperation(b, input, asyncParser);
} else if (match.s !== undefined) {
return invalidOperation(b, path, "The S.custom parser is missing");
} else {
return input;
}
}), undefined, (function () {
return makeReverseSchema((function () {
return name;
}), "unknown", empty, (function (b, input, selfSchema, path) {
registerInvalidJson(b, selfSchema, path);
var match = definer(effectCtx(b, selfSchema, path));
var serializer = match.s;
if (serializer !== undefined) {
return embedSyncOperation(b, input, serializer);
} else if (match.a !== undefined || match.p !== undefined) {
return invalidOperation(b, path, "The S.custom serializer is missing");
} else {
return input;
}
}), undefined);
}));
}
function literal(value) {
var literal$1 = parseInternal(value);
return makeSchema((function () {
return literal$1.s;
}), {
TAG: "literal",
_0: literal$1
}, empty, literal$1.j ? noop : invalidJson, (function (b, inputVar) {
return literal$1.f(b, inputVar, literal$1);
}), toSelf);
}
var unit = literal((void 0));
var defaultMetadataId = "rescript-schema:Option.default";
function name() {
return this.t._0.n() + " | undefined";
}
function $$default(schema) {
return schema.m[defaultMetadataId];
}
function makeBuilder(isNullInput, isNullOutput) {
return function (b, input, selfSchema, path) {
var childSchema = selfSchema.t._0;
var childSchemaTag = childSchema.t.TAG;
var bb = {
c: "",
l: "",
a: initialAllocate,
g: b.g
};
var itemInput = !(b.g.o & 1) && (childSchema.t === "unknown" || childSchemaTag === "option" || childSchemaTag === "literal" && childSchema.t._0.value === (void 0)) ? ({
b: bb,
v: _notVar,
i: embed(bb, valFromOption) + "(" + input.v(b) + ")",
a: false
}) : input;
var itemOutput = childSchema.b(bb, itemInput, childSchema, path);
var itemCode = allocateScope(bb);
var inputLiteral = isNullInput ? "null" : "void 0";
var ouputLiteral = isNullOutput ? "null" : "void 0";
var isTransformed = inputLiteral !== ouputLiteral || itemOutput !== input;
var output = isTransformed ? ({
b: b,
v: _notVar,
i: "",
a: itemOutput.a
}) : input;
if (itemCode !== "" || isTransformed) {
b.c = b.c + ("if(" + input.v(b) + "!==" + inputLiteral + "){" + itemCode + set(b, output, itemOutput) + "}" + (
inputLiteral !== ouputLiteral || output.a ? "else{" + set(b, output, {
b: b,
v: _notVar,
i: ouputLiteral,
a: false
}) + "}" : ""
));
}
return output;
};
}
function maybeTypeFilter(schema, inlinedNoneValue) {
if (schema.f !== undefined) {
return (function (b, inputVar) {
return inputVar + "!==" + inlinedNoneValue + "&&(" + schema.f(b, inputVar) + ")";
});
}
}
function factory(schema) {
return makeSchema(name, {
TAG: "option",
_0: schema
}, empty, makeBuilder(false, false), maybeTypeFilter(schema, "void 0"), onlyChild(factory, schema));
}
function getWithDefault(schema, $$default) {
return makeSchema(schema.n, schema.t, set$1(schema.m, defaultMetadataId, $$default), (function (b, input, param, path) {
return transform(b, schema.b(b, input, schema, path), (function (b, input) {
var inputVar = input.v(b);
var tmp;
tmp = $$default.TAG === "Value" ? embed(b, $$default._0) : embed(b, $$default._0) + "()";
return {
b: b,
v: _notVar,
i: inputVar + "===void 0?" + tmp + ":" + inputVar,
a: false
};
}));
}), schema.f, (function () {
var reversed = schema["~r"]();
if (reversed.t.TAG === "option") {
return reversed.t._0;
} else {
return reversed;
}
}));
}
function getOr(schema, defalutValue) {
return getWithDefault(schema, {
TAG: "Value",
_0: defalutValue
});
}
function getOrWith(schema, defalutCb) {
return getWithDefault(schema, {
TAG: "Callback",
_0: defalutCb
});
}
function factory$1(schema) {
return makeSchema((function () {
return schema.n() + " | null";
}), {
TAG: "null",
_0: schema
}, empty, makeBuilder(true, false), maybeTypeFilter(schema, "null"), (function () {
var child = schema["~r"]();
return makeReverseSchema(name, {
TAG: "option",
_0: child
}, empty, makeBuilder(false, true), maybeTypeFilter(schema, "void 0"));
}));
}
function nullable(schema) {
return factory(factory$1(schema));
}
function builder(b, input, selfSchema, path) {
b.c = b.c + failWithArg(b, path, (function (input) {
return {
TAG: "InvalidType",
expected: selfSchema,
received: input
};
}), input.i) + ";";
return input;
}
var schema = makeSchema(primitiveName, "never", empty, builder, undefined, toSelf);
var metadataId = "rescript-schema:Array.refinements";
function refinements(schema) {
var m = schema.m[metadataId];
if (m !== undefined) {
return m;
} else {
return [];
}
}
function typeFilter(_b, inputVar) {
return "!Array.isArray(" + inputVar + ")";
}
function name$1() {
return "array<" + this.t._0.n() + ">";
}
function factory$2(schema) {
return makeSchema(name$1, {
TAG: "array",
_0: schema
}, empty, (function (b, input, param, path) {
var inputVar = input.v(b);
var iteratorVar = varWithoutAllocation(b.g);
var bb = {
c: "",
l: "",
a: initialAllocate,
g: b.g
};
var itemInput = {
b: bb,
v: _notVar,
i: inputVar + "[" + iteratorVar + "]",
a: false
};
var itemOutput = withPathPrepend(bb, itemInput, path, iteratorVar, undefined, (function (b, input, path) {
return parseWithTypeValidation(b, schema, input, path);
}));
var itemCode = allocateScope(bb);
var isTransformed = itemInput !== itemOutput;
var output = isTransformed ? ({
b: b,
v: _notVar,
i: "new Array(" + inputVar + ".length)",
a: false
}) : input;
if (isTransformed || itemCode !== "") {
b.c = b.c + ("for(let " + iteratorVar + "=0;" + iteratorVar + "<" + inputVar + ".length;++" + iteratorVar + "){" + itemCode + (
isTransformed ? addKey(b, output, iteratorVar, itemOutput) : ""
) + "}");
}
if (itemOutput.a) {
return {
b: output.b,
v: _notVar,
i: "Promise.all(" + output.i + ")",
a: true
};
} else {
return output;
}
}), typeFilter, onlyChild(factory$2, schema));
}
function typeFilter$1(b, inputVar) {
var tagged = this.t;
var code = "typeof " + inputVar + "!==\"object\"||!" + inputVar + (
tagged.unknownKeys === "Strict" ? "||Array.isArray(" + inputVar + ")" : ""
);
var items = tagged.items;
for(var idx = 0 ,idx_finish = items.length; idx < idx_finish; ++idx){
var match = items[idx];
var schema = match.schema;
if (schema.t.TAG === "literal") {
code = code + "||" + schema.f(b, inputVar + ("[" + match.inlinedLocation + "]"));
}
}
return code;
}
function name$2() {
var tagged = this.t;
if (tagged.items.length === 0) {
return "{}";
} else {
return "{ " + tagged.items.map(function (item) {
return item.location + ": " + item.schema.n() + ";";
}).join(" ") + " }";
}
}
function setUnknownKeys(schema, unknownKeys, deep) {
var match = schema.t;
if (typeof match !== "object") {
return schema;
}
if (match.TAG !== "object") {
return schema;
}
if (match.unknownKeys === unknownKeys) {
return schema;
}
var advanced = match.advanced;
var items = match.items;
var tagged;
if (deep) {
var newItems = [];
var newFields = {};
for(var idx = 0 ,idx_finish = items.length; idx < idx_finish; ++idx){
var item = items[idx];
var newSchema = setUnknownKeys(item.schema, unknownKeys, deep);
var newItem = newSchema === item.schema ? item : ({
schema: newSchema,
location: item.location,
inlinedLocation: item.inlinedLocation
});
newFields[item.location] = newItem;
newItems.push(newItem);
}
tagged = {
TAG: "object",
items: newItems,
fields: newFields,
unknownKeys: unknownKeys,
advanced: advanced
};
} else {
tagged = {
TAG: "object",
items: items,
fields: match.fields,
unknownKeys: unknownKeys,
advanced: advanced
};
}
return {
t: tagged,
n: schema.n,
"~r": schema["~r"],
b: schema.b,
f: schema.f,
i: schema.i,
m: schema.m
};
}
function strip(schema) {
return setUnknownKeys(schema, "Strip", false);
}
function deepStrip(schema) {
return setUnknownKeys(schema, "Strip", true);
}
function strict(schema) {
return setUnknownKeys(schema, "Strict", false);
}
function deepStrict(schema) {
return setUnknownKeys(schema, "Strict", true);
}
function name$3() {
return "[" + this.t.items.map(function (item) {
return item.schema.n();
}).join(", ") + "]";
}
function typeFilter$2(b, inputVar) {
var items = this.t.items;
var length = items.length;
var code = typeFilter(b, inputVar) + ("||" + inputVar + ".length!==" + length);
for(var idx = 0; idx < length; ++idx){
var match = items[idx];
var schema = match.schema;
if (schema.t.TAG === "literal") {
code = code + "||" + schema.f(b, inputVar + ("[" + match.inlinedLocation + "]"));
}
}
return code;
}
function typeFilter$3(_b, inputVar) {
return "typeof " + inputVar + "!==\"object\"||!" + inputVar;
}
function factory$3(schema) {
return makeSchema((function () {
return "dict<" + schema.n() + ">";
}), {
TAG: "dict",
_0: schema
}, empty, (function (b, input, param, path) {
var inputVar = input.v(b);
var keyVar = varWithoutAllocation(b.g);
var bb = {
c: "",
l: "",
a: initialAllocate,
g: b.g
};
var itemInput = {
b: bb,
v: _notVar,
i: inputVar + "[" + keyVar + "]",
a: false
};
var itemOutput = withPathPrepend(bb, itemInput, path, keyVar, undefined, (function (b, input, path) {
return parseWithTypeValidation(b, schema, input, path);
}));
var itemCode = allocateScope(bb);
var isTransformed = itemInput !== itemOutput;
var output = isTransformed ? ({
b: b,
v: _notVar,
i: "{}",
a: false
}) : input;
if (isTransformed || itemCode !== "") {
b.c = b.c + ("for(let " + keyVar + " in " + inputVar + "){" + itemCode + (
isTransformed ? addKey(b, output, keyVar, itemOutput) : ""
) + "}");
}
if (!itemOutput.a) {
return output;
}
var resolveVar = varWithoutAllocation(b.g);
var rejectVar = varWithoutAllocation(b.g);
var asyncParseResultVar = varWithoutAllocation(b.g);
var counterVar = varWithoutAllocation(b.g);
var outputVar = output.v(b);
return {
b: b,
v: _notVar,
i: "new Promise((" + resolveVar + "," + rejectVar + ")=>{let " + counterVar + "=Object.keys(" + outputVar + ").length;for(let " + keyVar + " in " + outputVar + "){" + outputVar + "[" + keyVar + "].then(" + asyncParseResultVar + "=>{" + outputVar + "[" + keyVar + "]=" + asyncParseResultVar + ";if(" + counterVar + "--===1){" + resolveVar + "(" + outputVar + ")}}," + rejectVar + ")}})",
a: true
};
}), typeFilter$3, onlyChild(factory$3, schema));
}
var schema$1 = makeSchema(primitiveName, "unknown", empty, invalidJson, undefined, toSelf);
var metadataId$1 = "rescript-schema:String.refinements";
function refinements$1(schema) {
var m = schema.m[metadataId$1];
if (m !== undefined) {
return m;
} else {
return [];
}
}
var cuidRegex = /^c[^\s-]{8,}$/i;
var uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
var emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
var datetimeRe = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/;
function typeFilter$4(_b, inputVar) {
return "typeof " + inputVar + "!==\"string\"";
}
var schema$2 = makePrimitiveSchema("string", noop, typeFilter$4);
function factory$4(schema, spaceOpt) {
var space = spaceOpt !== undefined ? spaceOpt : 0;
return makeSchema(primitiveName, "string", empty, (function (b, input, param, path) {
var jsonVal = allocateVal(b);
b.c = b.c + ("try{" + jsonVal.i + "=JSON.parse(" + input.i + ")}catch(t){" + failWithArg(b, path, (function (message) {
return {
TAG: "OperationFailed",
_0: message
};
}), "t.message") + "}");
return parseWithTypeValidation(b, schema, jsonVal, path);
}), typeFilter$4, (function () {
var reversed = schema["~r"]();
return makeReverseSchema(reversed.n, reversed.t, reversed.m, (function (b, input, param, path) {
var prevFlag = b.g.o;
b.g.o = prevFlag | 8;
if (reversed.t.TAG === "option") {
raise(b, {
TAG: "InvalidJsonSchema",
_0: reversed
}, "");
}
var output = {
b: b,
v: _notVar,
i: "JSON.stringify(" + reversed.b(b, input, reversed, path).i + (
space > 0 ? ",null," + space : ""
) + ")",