schemas/libraries/io-ts/download_compiled/index/unminified.js
schemas/libraries/io-ts/download_compiled/index/unminified.js
schemas/libraries/io-ts/download_compiled/index/unminified.js
schemas/libraries/io-ts/download_compiled/index/unminified.js
function identity$1(a) {
return a;
}
function pipe(
a,
ab,
bc,
cd,
de,
ef,
fg,
gh,
hi,
) {
switch (arguments.length) {
case 1:
return a;
case 2:
return ab(a);
case 3:
return bc(ab(a));
case 4:
return cd(bc(ab(a)));
case 5:
return de(cd(bc(ab(a))));
case 6:
return ef(de(cd(bc(ab(a)))));
case 7:
return fg(ef(de(cd(bc(ab(a))))));
case 8:
return gh(
fg(ef(de(cd(bc(ab(a)))))),
);
case 9:
return hi(
gh(fg(ef(de(cd(bc(ab(a))))))),
);
default:
var ret = arguments[0];
for (
var i = 1;
i < arguments.length;
i++
)
ret = arguments[i](ret);
return ret;
}
}
var isLeft$1 = function (ma) {
return ma._tag === "Left";
};
var left$1 = function (e) {
return {
_tag: "Left",
left: e,
};
};
var right$1 = function (a) {
return {
_tag: "Right",
right: a,
};
};
function as$1(F) {
return function (self, b) {
return F.map(self, function () {
return b;
});
};
}
function asUnit$1(F) {
var asM = as$1(F);
return function (self) {
return asM(self, void 0);
};
}
var left = left$1;
var right = right$1;
var _map = function (fa, f) {
return pipe(fa, map(f));
};
var URI = "Either";
var map = function (f) {
return function (fa) {
return isLeft(fa)
? fa
: right(f(fa.right));
};
};
asUnit$1({
URI,
map: _map,
});
var FromEither = {
URI,
fromEither: identity$1,
};
var isLeft = isLeft$1;
FromEither.fromEither;
var __extends = (function () {
var extendStatics = function (d, b) {
extendStatics =
Object.setPrototypeOf ||
({ __proto__: [] } instanceof
Array &&
function (d, b) {
d.__proto__ = b;
}) ||
function (d, b) {
for (var p in b)
if (
Object.prototype.hasOwnProperty.call(
b,
p,
)
)
d[p] = b[p];
};
return extendStatics(d, b);
};
return function (d, b) {
if (
typeof b !== "function" &&
b !== null
)
throw new TypeError(
"Class extends value " +
String(b) +
" is not a constructor or null",
);
extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype =
b === null
? Object.create(b)
: ((__.prototype = b.prototype),
new __());
};
})();
var __assign = function () {
__assign =
Object.assign ||
function (t) {
for (
var s,
i = 1,
n = arguments.length;
i < n;
i++
) {
s = arguments[i];
for (var p in s)
if (
Object.prototype.hasOwnProperty.call(
s,
p,
)
)
t[p] = s[p];
}
return t;
};
return __assign.apply(
this,
arguments,
);
};
var failures = left;
var failure = function (
value,
context,
message,
) {
return failures([
{
value,
context,
message,
},
]);
};
var success = right;
var Type = (function () {
function Type(
name,
is,
validate,
encode,
) {
this.name = name;
this.is = is;
this.validate = validate;
this.encode = encode;
this.decode =
this.decode.bind(this);
}
Type.prototype.pipe = function (
ab,
name,
) {
var _this = this;
if (name === void 0)
name = "pipe("
.concat(this.name, ", ")
.concat(ab.name, ")");
return new Type(
name,
ab.is,
function (i, c) {
var e = _this.validate(i, c);
if (isLeft(e)) return e;
return ab.validate(e.right, c);
},
this.encode === identity &&
ab.encode === identity
? identity
: function (b) {
return _this.encode(
ab.encode(b),
);
},
);
};
Type.prototype.asDecoder =
function () {
return this;
};
Type.prototype.asEncoder =
function () {
return this;
};
Type.prototype.decode = function (i) {
return this.validate(i, [
{
key: "",
type: this,
actual: i,
},
]);
};
return Type;
})();
var identity = function (a) {
return a;
};
function getFunctionName(f) {
return (
f.displayName ||
f.name ||
"<function".concat(f.length, ">")
);
}
function appendContext(
c,
key,
decoder,
actual,
) {
var len = c.length;
var r = Array(len + 1);
for (var i = 0; i < len; i++)
r[i] = c[i];
r[len] = {
key,
type: decoder,
actual,
};
return r;
}
function pushAll(xs, ys) {
var l = ys.length;
for (var i = 0; i < l; i++)
xs.push(ys[i]);
}
var hasOwnProperty =
Object.prototype.hasOwnProperty;
function getNameFromProps(props) {
return Object.keys(props)
.map(function (k) {
return ""
.concat(k, ": ")
.concat(props[k].name);
})
.join(", ");
}
function useIdentity(codecs) {
for (
var i = 0;
i < codecs.length;
i++
)
if (codecs[i].encode !== identity)
return false;
return true;
}
function getInterfaceTypeName(props) {
return "{ ".concat(
getNameFromProps(props),
" }",
);
}
function getUnionName(codecs) {
return (
"(" +
codecs
.map(function (type) {
return type.name;
})
.join(" | ") +
")"
);
}
function isNonEmpty(as) {
return as.length > 0;
}
var emptyTags = {};
function intersect(a, b) {
var r = [];
for (
var _i = 0, a_1 = a;
_i < a_1.length;
_i++
) {
var v = a_1[_i];
if (b.indexOf(v) !== -1) r.push(v);
}
return r;
}
function mergeTags(a, b) {
if (a === emptyTags) return b;
if (b === emptyTags) return a;
var r = Object.assign({}, a);
for (var k in b)
if (hasOwnProperty.call(a, k)) {
var intersection_1 = intersect(
a[k],
b[k],
);
if (isNonEmpty(intersection_1))
r[k] = intersection_1;
else {
r = emptyTags;
break;
}
} else r[k] = b[k];
return r;
}
function intersectTags(a, b) {
if (
a === emptyTags ||
b === emptyTags
)
return emptyTags;
var r = emptyTags;
for (var k in a)
if (hasOwnProperty.call(b, k)) {
if (
intersect(a[k], b[k]).length ===
0
) {
if (r === emptyTags) r = {};
r[k] = a[k].concat(b[k]);
}
}
return r;
}
function isLiteralC(codec) {
return codec._tag === "LiteralType";
}
function isTypeC(codec) {
return codec._tag === "InterfaceType";
}
function isStrictC(codec) {
return codec._tag === "StrictType";
}
function isExactC(codec) {
return codec._tag === "ExactType";
}
function isRefinementC(codec) {
return (
codec._tag === "RefinementType"
);
}
function isIntersectionC(codec) {
return (
codec._tag === "IntersectionType"
);
}
function isUnionC(codec) {
return codec._tag === "UnionType";
}
function isRecursiveC(codec) {
return codec._tag === "RecursiveType";
}
function isReadonlyC(codec) {
return codec._tag === "ReadonlyType";
}
var lazyCodecs = [];
function getTags(codec) {
if (lazyCodecs.indexOf(codec) !== -1)
return emptyTags;
if (
isTypeC(codec) ||
isStrictC(codec)
) {
var index = emptyTags;
for (var k in codec.props) {
var prop = codec.props[k];
if (isLiteralC(prop)) {
if (index === emptyTags)
index = {};
index[k] = [prop.value];
}
}
return index;
} else if (
isExactC(codec) ||
isRefinementC(codec) ||
isReadonlyC(codec)
)
return getTags(codec.type);
else if (isIntersectionC(codec))
return codec.types.reduce(function (
tags,
codec,
) {
return mergeTags(
tags,
getTags(codec),
);
}, emptyTags);
else if (isUnionC(codec))
return codec.types
.slice(1)
.reduce(function (tags, codec) {
return intersectTags(
tags,
getTags(codec),
);
}, getTags(codec.types[0]));
else if (isRecursiveC(codec)) {
lazyCodecs.push(codec);
var tags = getTags(codec.type);
lazyCodecs.pop();
return tags;
}
return emptyTags;
}
function getIndex(codecs) {
var tags = getTags(codecs[0]);
var keys = Object.keys(tags);
var len = codecs.length;
var _loop_1 = function (k) {
var all = tags[k].slice();
var index = [tags[k]];
for (var i = 1; i < len; i++) {
var codec = codecs[i];
var values = getTags(codec)[k];
if (values === void 0)
return "continue-keys";
else if (
values.some(function (v) {
return all.indexOf(v) !== -1;
})
)
return "continue-keys";
else {
all.push.apply(all, values);
index.push(values);
}
}
return { value: [k, index] };
};
keys: for (
var _i = 0, keys_1 = keys;
_i < keys_1.length;
_i++
) {
var k = keys_1[_i];
var state_1 = _loop_1(k);
if (typeof state_1 === "object")
return state_1.value;
switch (state_1) {
case "continue-keys":
continue keys;
}
}
}
var nullType = new ((function (_super) {
__extends(NullType, _super);
function NullType() {
var _this =
_super.call(
this,
"null",
function (u) {
return u === null;
},
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "NullType";
return _this;
}
return NullType;
})(Type))();
var undefinedType = new ((function (
_super,
) {
__extends(UndefinedType, _super);
function UndefinedType() {
var _this =
_super.call(
this,
"undefined",
function (u) {
return u === void 0;
},
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "UndefinedType";
return _this;
}
return UndefinedType;
})(Type))();
new ((function (_super) {
__extends(VoidType, _super);
function VoidType() {
var _this =
_super.call(
this,
"void",
undefinedType.is,
undefinedType.validate,
identity,
) || this;
_this._tag = "VoidType";
return _this;
}
return VoidType;
})(Type))();
new ((function (_super) {
__extends(UnknownType, _super);
function UnknownType() {
var _this =
_super.call(
this,
"unknown",
function (_) {
return true;
},
success,
identity,
) || this;
_this._tag = "UnknownType";
return _this;
}
return UnknownType;
})(Type))();
var string = new ((function (_super) {
__extends(StringType, _super);
function StringType() {
var _this =
_super.call(
this,
"string",
function (u) {
return typeof u === "string";
},
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "StringType";
return _this;
}
return StringType;
})(Type))();
var number = new ((function (_super) {
__extends(NumberType, _super);
function NumberType() {
var _this =
_super.call(
this,
"number",
function (u) {
return typeof u === "number";
},
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "NumberType";
return _this;
}
return NumberType;
})(Type))();
new ((function (_super) {
__extends(BigIntType, _super);
function BigIntType() {
var _this =
_super.call(
this,
"bigint",
function (u) {
return typeof u === "bigint";
},
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "BigIntType";
return _this;
}
return BigIntType;
})(Type))();
new ((function (_super) {
__extends(BooleanType, _super);
function BooleanType() {
var _this =
_super.call(
this,
"boolean",
function (u) {
return typeof u === "boolean";
},
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "BooleanType";
return _this;
}
return BooleanType;
})(Type))();
var UnknownArray = new ((function (
_super,
) {
__extends(AnyArrayType, _super);
function AnyArrayType() {
var _this =
_super.call(
this,
"UnknownArray",
Array.isArray,
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "AnyArrayType";
return _this;
}
return AnyArrayType;
})(Type))();
var UnknownRecord = new ((function (
_super,
) {
__extends(AnyDictionaryType, _super);
function AnyDictionaryType() {
var _this =
_super.call(
this,
"UnknownRecord",
function (u) {
return (
u !== null &&
typeof u === "object" &&
!Array.isArray(u)
);
},
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "AnyDictionaryType";
return _this;
}
return AnyDictionaryType;
})(Type))();
var LiteralType = (function (_super) {
__extends(LiteralType, _super);
function LiteralType(
name,
is,
validate,
encode,
value,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.value = value;
_this._tag = "LiteralType";
return _this;
}
return LiteralType;
})(Type);
function literal(value, name) {
if (name === void 0)
name = JSON.stringify(value);
var is = function (u) {
return u === value;
};
return new LiteralType(
name,
is,
function (u, c) {
return is(u)
? success(value)
: failure(u, c);
},
identity,
value,
);
}
(function (_super) {
__extends(KeyofType, _super);
function KeyofType(
name,
is,
validate,
encode,
keys,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.keys = keys;
_this._tag = "KeyofType";
return _this;
}
return KeyofType;
})(Type);
var RefinementType = (function (
_super,
) {
__extends(RefinementType, _super);
function RefinementType(
name,
is,
validate,
encode,
type,
predicate,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.type = type;
_this.predicate = predicate;
_this._tag = "RefinementType";
return _this;
}
return RefinementType;
})(Type);
function brand(codec, predicate, name) {
return refinement(
codec,
predicate,
name,
);
}
brand(
number,
function (n) {
return Number.isInteger(n);
},
"Int",
);
var RecursiveType = (function (_super) {
__extends(RecursiveType, _super);
function RecursiveType(
name,
is,
validate,
encode,
runDefinition,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.runDefinition = runDefinition;
_this._tag = "RecursiveType";
return _this;
}
return RecursiveType;
})(Type);
Object.defineProperty(
RecursiveType.prototype,
"type",
{
get: function () {
return this.runDefinition();
},
enumerable: true,
configurable: true,
},
);
var ArrayType = (function (_super) {
__extends(ArrayType, _super);
function ArrayType(
name,
is,
validate,
encode,
type,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.type = type;
_this._tag = "ArrayType";
return _this;
}
return ArrayType;
})(Type);
function array(item, name) {
if (name === void 0)
name = "Array<".concat(
item.name,
">",
);
return new ArrayType(
name,
function (u) {
return (
UnknownArray.is(u) &&
u.every(item.is)
);
},
function (u, c) {
var e = UnknownArray.validate(
u,
c,
);
if (isLeft(e)) return e;
var us = e.right;
var len = us.length;
var as = us;
var errors = [];
for (var i = 0; i < len; i++) {
var ui = us[i];
var result = item.validate(
ui,
appendContext(
c,
String(i),
item,
ui,
),
);
if (isLeft(result))
pushAll(errors, result.left);
else {
var ai = result.right;
if (ai !== ui) {
if (as === us)
as = us.slice();
as[i] = ai;
}
}
}
return errors.length > 0
? failures(errors)
: success(as);
},
item.encode === identity
? identity
: function (a) {
return a.map(item.encode);
},
item,
);
}
var InterfaceType = (function (_super) {
__extends(InterfaceType, _super);
function InterfaceType(
name,
is,
validate,
encode,
props,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.props = props;
_this._tag = "InterfaceType";
return _this;
}
return InterfaceType;
})(Type);
function type(props, name) {
if (name === void 0)
name = getInterfaceTypeName(props);
var keys = Object.keys(props);
var types = keys.map(function (key) {
return props[key];
});
var len = keys.length;
return new InterfaceType(
name,
function (u) {
if (UnknownRecord.is(u)) {
for (var i = 0; i < len; i++) {
var k = keys[i];
var uk = u[k];
if (
(uk === void 0 &&
!hasOwnProperty.call(
u,
k,
)) ||
!types[i].is(uk)
)
return false;
}
return true;
}
return false;
},
function (u, c) {
var e = UnknownRecord.validate(
u,
c,
);
if (isLeft(e)) return e;
var o = e.right;
var a = o;
var errors = [];
for (var i = 0; i < len; i++) {
var k = keys[i];
var ak = a[k];
var type_1 = types[i];
var result = type_1.validate(
ak,
appendContext(
c,
k,
type_1,
ak,
),
);
if (isLeft(result))
pushAll(errors, result.left);
else {
var vak = result.right;
if (
vak !== ak ||
(vak === void 0 &&
!hasOwnProperty.call(
a,
k,
))
) {
if (a === o)
a = __assign({}, o);
a[k] = vak;
}
}
}
return errors.length > 0
? failures(errors)
: success(a);
},
useIdentity(types)
? identity
: function (a) {
var s = __assign({}, a);
for (
var i = 0;
i < len;
i++
) {
var k = keys[i];
var encode =
types[i].encode;
if (encode !== identity)
s[k] = encode(a[k]);
}
return s;
},
props,
);
}
(function (_super) {
__extends(PartialType, _super);
function PartialType(
name,
is,
validate,
encode,
props,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.props = props;
_this._tag = "PartialType";
return _this;
}
return PartialType;
})(Type);
(function (_super) {
__extends(DictionaryType, _super);
function DictionaryType(
name,
is,
validate,
encode,
domain,
codomain,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.domain = domain;
_this.codomain = codomain;
_this._tag = "DictionaryType";
return _this;
}
return DictionaryType;
})(Type);
var UnionType = (function (_super) {
__extends(UnionType, _super);
function UnionType(
name,
is,
validate,
encode,
types,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.types = types;
_this._tag = "UnionType";
return _this;
}
return UnionType;
})(Type);
function union(codecs, name) {
if (name === void 0)
name = getUnionName(codecs);
var index = getIndex(codecs);
if (
index !== void 0 &&
codecs.length > 0
) {
var tag_1 = index[0],
groups_1 = index[1];
var len_1 = groups_1.length;
var find_1 = function (value) {
for (var i = 0; i < len_1; i++)
if (
groups_1[i].indexOf(value) !==
-1
)
return i;
};
return new TaggedUnionType(
name,
function (u) {
if (UnknownRecord.is(u)) {
var i = find_1(u[tag_1]);
return i !== void 0
? codecs[i].is(u)
: false;
}
return false;
},
function (u, c) {
var e = UnknownRecord.validate(
u,
c,
);
if (isLeft(e)) return e;
var r = e.right;
var i = find_1(r[tag_1]);
if (i === void 0)
return failure(u, c);
var codec = codecs[i];
return codec.validate(
r,
appendContext(
c,
String(i),
codec,
r,
),
);
},
useIdentity(codecs)
? identity
: function (a) {
var i = find_1(a[tag_1]);
if (i === void 0)
throw new Error(
"no codec found to encode value in union codec ".concat(
name,
),
);
else
return codecs[i].encode(
a,
);
},
codecs,
tag_1,
);
} else
return new UnionType(
name,
function (u) {
return codecs.some(
function (type) {
return type.is(u);
},
);
},
function (u, c) {
var errors = [];
for (
var i = 0;
i < codecs.length;
i++
) {
var codec = codecs[i];
var result = codec.validate(
u,
appendContext(
c,
String(i),
codec,
u,
),
);
if (isLeft(result))
pushAll(
errors,
result.left,
);
else
return success(
result.right,
);
}
return failures(errors);
},
useIdentity(codecs)
? identity
: function (a) {
for (
var _i = 0,
codecs_1 = codecs;
_i < codecs_1.length;
_i++
) {
var codec = codecs_1[_i];
if (codec.is(a))
return codec.encode(a);
}
throw new Error(
"no codec found to encode value in union type ".concat(
name,
),
);
},
codecs,
);
}
(function (_super) {
__extends(IntersectionType, _super);
function IntersectionType(
name,
is,
validate,
encode,
types,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.types = types;
_this._tag = "IntersectionType";
return _this;
}
return IntersectionType;
})(Type);
(function (_super) {
__extends(TupleType, _super);
function TupleType(
name,
is,
validate,
encode,
types,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.types = types;
_this._tag = "TupleType";
return _this;
}
return TupleType;
})(Type);
(function (_super) {
__extends(ReadonlyType, _super);
function ReadonlyType(
name,
is,
validate,
encode,
type,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.type = type;
_this._tag = "ReadonlyType";
return _this;
}
return ReadonlyType;
})(Type);
(function (_super) {
__extends(ReadonlyArrayType, _super);
function ReadonlyArrayType(
name,
is,
validate,
encode,
type,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.type = type;
_this._tag = "ReadonlyArrayType";
return _this;
}
return ReadonlyArrayType;
})(Type);
(function (_super) {
__extends(ExactType, _super);
function ExactType(
name,
is,
validate,
encode,
type,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.type = type;
_this._tag = "ExactType";
return _this;
}
return ExactType;
})(Type);
new ((function (_super) {
__extends(FunctionType, _super);
function FunctionType() {
var _this =
_super.call(
this,
"Function",
function (u) {
return (
typeof u === "function"
);
},
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "FunctionType";
return _this;
}
return FunctionType;
})(Type))();
new ((function (_super) {
__extends(NeverType, _super);
function NeverType() {
var _this =
_super.call(
this,
"never",
function (_) {
return false;
},
function (u, c) {
return failure(u, c);
},
function () {
throw new Error(
"cannot encode never",
);
},
) || this;
_this._tag = "NeverType";
return _this;
}
return NeverType;
})(Type))();
new ((function (_super) {
__extends(AnyType, _super);
function AnyType() {
var _this =
_super.call(
this,
"any",
function (_) {
return true;
},
success,
identity,
) || this;
_this._tag = "AnyType";
return _this;
}
return AnyType;
})(Type))();
function refinement(
codec,
predicate,
name,
) {
if (name === void 0)
name = "("
.concat(codec.name, " | ")
.concat(
getFunctionName(predicate),
")",
);
return new RefinementType(
name,
function (u) {
return (
codec.is(u) && predicate(u)
);
},
function (i, c) {
var e = codec.validate(i, c);
if (isLeft(e)) return e;
var a = e.right;
return predicate(a)
? success(a)
: failure(a, c);
},
codec.encode,
codec,
predicate,
);
}
refinement(
number,
Number.isInteger,
"Integer",
);
var TaggedUnionType = (function (
_super,
) {
__extends(TaggedUnionType, _super);
function TaggedUnionType(
name,
is,
validate,
encode,
codecs,
tag,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
codecs,
) || this;
_this.tag = tag;
return _this;
}
return TaggedUnionType;
})(UnionType);
new ((function (_super) {
__extends(ObjectType, _super);
function ObjectType() {
var _this =
_super.call(
this,
"object",
function (u) {
return (
u !== null &&
typeof u === "object"
);
},
function (u, c) {
return _this.is(u)
? success(u)
: failure(u, c);
},
identity,
) || this;
_this._tag = "ObjectType";
return _this;
}
return ObjectType;
})(Type))();
(function (_super) {
__extends(StrictType, _super);
function StrictType(
name,
is,
validate,
encode,
props,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
) || this;
_this.props = props;
_this._tag = "StrictType";
return _this;
}
return StrictType;
})(Type);
function fromRefinement(name, is) {
return new Type(
name,
is,
function (u, c) {
return is(u)
? success(u)
: failure(u, c);
},
identity,
);
}
var isDate = function (u) {
return u instanceof Date;
};
var date = fromRefinement(
"Date",
isDate,
);
const stringWithLength = (min, max) =>
refinement(
string,
(s) =>
s.length >= min &&
s.length <= max,
`string with length between ${min} and ${max}`,
);
const numberInRange = (min, max) =>
refinement(
number,
(n) => n >= min && n <= max,
`number between ${min} and ${max}`,
);
const urlString = refinement(
string,
(s) => URL.canParse(s),
"url",
);
const literals = ([
l1,
l2,
...literals
]) =>
union([
literal(l1),
literal(l2),
...literals.map((l) => literal(l)),
]);
const ImageData = type({
id: number,
created: date,
title: stringWithLength(1, 100),
type: literals(["jpg", "png"]),
size: number,
url: urlString,
});
const RatingData = type({
id: number,
stars: numberInRange(1, 5),
title: stringWithLength(1, 100),
text: stringWithLength(1, 1e3),
images: array(ImageData),
});
type(
{
id: number,
created: date,
title: stringWithLength(1, 100),
brand: stringWithLength(1, 30),
description: stringWithLength(
1,
500,
),
price: numberInRange(1, 1e4),
discount: union([
numberInRange(1, 100),
nullType,
]),
quantity: numberInRange(0, 10),
tags: array(
stringWithLength(1, 30),
),
images: array(ImageData),
ratings: array(RatingData),
},
"ProductData",
).decode({});
function identity$1(a) {
return a;
}
function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
switch (arguments.length) {
case 1:
return a;
case 2:
return ab(a);
case 3:
return bc(ab(a));
case 4:
return cd(bc(ab(a)));
case 5:
return de(cd(bc(ab(a))));
case 6:
return ef(de(cd(bc(ab(a)))));
case 7:
return fg(ef(de(cd(bc(ab(a))))));
case 8:
return gh(fg(ef(de(cd(bc(ab(a)))))));
case 9:
return hi(gh(fg(ef(de(cd(bc(ab(a))))))));
default:
var ret = arguments[0];
for (var i = 1; i < arguments.length; i++)
ret = arguments[i](ret);
return ret;
}
}
var isLeft$1 = function (ma) {
return ma._tag === "Left";
};
var left$1 = function (e) {
return {
_tag: "Left",
left: e,
};
};
var right$1 = function (a) {
return {
_tag: "Right",
right: a,
};
};
function as$1(F) {
return function (self, b) {
return F.map(self, function () {
return b;
});
};
}
function asUnit$1(F) {
var asM = as$1(F);
return function (self) {
return asM(self, void 0);
};
}
var left = left$1;
var right = right$1;
var _map = function (fa, f) {
return pipe(fa, map(f));
};
var URI = "Either";
var map = function (f) {
return function (fa) {
return isLeft(fa) ? fa : right(f(fa.right));
};
};
asUnit$1({
URI,
map: _map,
});
var FromEither = {
URI,
fromEither: identity$1,
};
var isLeft = isLeft$1;
FromEither.fromEither;
var __extends = (function () {
var extendStatics = function (d, b) {
extendStatics =
Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array &&
function (d, b) {
d.__proto__ = b;
}) ||
function (d, b) {
for (var p in b)
if (Object.prototype.hasOwnProperty.call(b, p))
d[p] = b[p];
};
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError(
"Class extends value " +
String(b) +
" is not a constructor or null",
);
extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype =
b === null
? Object.create(b)
: ((__.prototype = b.prototype), new __());
};
})();
var __assign = function () {
__assign =
Object.assign ||
function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var failures = left;
var failure = function (value, context, message) {
return failures([
{
value,
context,
message,
},
]);
};
var success = right;
var Type = (function () {
function Type(name, is, validate, encode) {
this.name = name;
this.is = is;
this.validate = validate;
this.encode = encode;
this.decode = this.decode.bind(this);
}
Type.prototype.pipe = function (ab, name) {
var _this = this;
if (name === void 0)
name = "pipe("
.concat(this.name, ", ")
.concat(ab.name, ")");
return new Type(
name,
ab.is,
function (i, c) {
var e = _this.validate(i, c);
if (isLeft(e)) return e;
return ab.validate(e.right, c);
},
this.encode === identity && ab.encode === identity
? identity
: function (b) {
return _this.encode(ab.encode(b));
},
);
};
Type.prototype.asDecoder = function () {
return this;
};
Type.prototype.asEncoder = function () {
return this;
};
Type.prototype.decode = function (i) {
return this.validate(i, [
{
key: "",
type: this,
actual: i,
},
]);
};
return Type;
})();
var identity = function (a) {
return a;
};
function getFunctionName(f) {
return (
f.displayName ||
f.name ||
"<function".concat(f.length, ">")
);
}
function appendContext(c, key, decoder, actual) {
var len = c.length;
var r = Array(len + 1);
for (var i = 0; i < len; i++) r[i] = c[i];
r[len] = {
key,
type: decoder,
actual,
};
return r;
}
function pushAll(xs, ys) {
var l = ys.length;
for (var i = 0; i < l; i++) xs.push(ys[i]);
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
function getNameFromProps(props) {
return Object.keys(props)
.map(function (k) {
return "".concat(k, ": ").concat(props[k].name);
})
.join(", ");
}
function useIdentity(codecs) {
for (var i = 0; i < codecs.length; i++)
if (codecs[i].encode !== identity) return false;
return true;
}
function getInterfaceTypeName(props) {
return "{ ".concat(getNameFromProps(props), " }");
}
function getUnionName(codecs) {
return (
"(" +
codecs
.map(function (type) {
return type.name;
})
.join(" | ") +
")"
);
}
function isNonEmpty(as) {
return as.length > 0;
}
var emptyTags = {};
function intersect(a, b) {
var r = [];
for (var _i = 0, a_1 = a; _i < a_1.length; _i++) {
var v = a_1[_i];
if (b.indexOf(v) !== -1) r.push(v);
}
return r;
}
function mergeTags(a, b) {
if (a === emptyTags) return b;
if (b === emptyTags) return a;
var r = Object.assign({}, a);
for (var k in b)
if (hasOwnProperty.call(a, k)) {
var intersection_1 = intersect(a[k], b[k]);
if (isNonEmpty(intersection_1)) r[k] = intersection_1;
else {
r = emptyTags;
break;
}
} else r[k] = b[k];
return r;
}
function intersectTags(a, b) {
if (a === emptyTags || b === emptyTags) return emptyTags;
var r = emptyTags;
for (var k in a)
if (hasOwnProperty.call(b, k)) {
if (intersect(a[k], b[k]).length === 0) {
if (r === emptyTags) r = {};
r[k] = a[k].concat(b[k]);
}
}
return r;
}
function isLiteralC(codec) {
return codec._tag === "LiteralType";
}
function isTypeC(codec) {
return codec._tag === "InterfaceType";
}
function isStrictC(codec) {
return codec._tag === "StrictType";
}
function isExactC(codec) {
return codec._tag === "ExactType";
}
function isRefinementC(codec) {
return codec._tag === "RefinementType";
}
function isIntersectionC(codec) {
return codec._tag === "IntersectionType";
}
function isUnionC(codec) {
return codec._tag === "UnionType";
}
function isRecursiveC(codec) {
return codec._tag === "RecursiveType";
}
function isReadonlyC(codec) {
return codec._tag === "ReadonlyType";
}
var lazyCodecs = [];
function getTags(codec) {
if (lazyCodecs.indexOf(codec) !== -1) return emptyTags;
if (isTypeC(codec) || isStrictC(codec)) {
var index = emptyTags;
for (var k in codec.props) {
var prop = codec.props[k];
if (isLiteralC(prop)) {
if (index === emptyTags) index = {};
index[k] = [prop.value];
}
}
return index;
} else if (
isExactC(codec) ||
isRefinementC(codec) ||
isReadonlyC(codec)
)
return getTags(codec.type);
else if (isIntersectionC(codec))
return codec.types.reduce(function (tags, codec) {
return mergeTags(tags, getTags(codec));
}, emptyTags);
else if (isUnionC(codec))
return codec.types.slice(1).reduce(function (
tags,
codec,
) {
return intersectTags(tags, getTags(codec));
}, getTags(codec.types[0]));
else if (isRecursiveC(codec)) {
lazyCodecs.push(codec);
var tags = getTags(codec.type);
lazyCodecs.pop();
return tags;
}
return emptyTags;
}
function getIndex(codecs) {
var tags = getTags(codecs[0]);
var keys = Object.keys(tags);
var len = codecs.length;
var _loop_1 = function (k) {
var all = tags[k].slice();
var index = [tags[k]];
for (var i = 1; i < len; i++) {
var codec = codecs[i];
var values = getTags(codec)[k];
if (values === void 0) return "continue-keys";
else if (
values.some(function (v) {
return all.indexOf(v) !== -1;
})
)
return "continue-keys";
else {
all.push.apply(all, values);
index.push(values);
}
}
return { value: [k, index] };
};
keys: for (
var _i = 0, keys_1 = keys;
_i < keys_1.length;
_i++
) {
var k = keys_1[_i];
var state_1 = _loop_1(k);
if (typeof state_1 === "object") return state_1.value;
switch (state_1) {
case "continue-keys":
continue keys;
}
}
}
var nullType = new ((function (_super) {
__extends(NullType, _super);
function NullType() {
var _this =
_super.call(
this,
"null",
function (u) {
return u === null;
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "NullType";
return _this;
}
return NullType;
})(Type))();
var undefinedType = new ((function (_super) {
__extends(UndefinedType, _super);
function UndefinedType() {
var _this =
_super.call(
this,
"undefined",
function (u) {
return u === void 0;
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "UndefinedType";
return _this;
}
return UndefinedType;
})(Type))();
new ((function (_super) {
__extends(VoidType, _super);
function VoidType() {
var _this =
_super.call(
this,
"void",
undefinedType.is,
undefinedType.validate,
identity,
) || this;
_this._tag = "VoidType";
return _this;
}
return VoidType;
})(Type))();
new ((function (_super) {
__extends(UnknownType, _super);
function UnknownType() {
var _this =
_super.call(
this,
"unknown",
function (_) {
return true;
},
success,
identity,
) || this;
_this._tag = "UnknownType";
return _this;
}
return UnknownType;
})(Type))();
var string = new ((function (_super) {
__extends(StringType, _super);
function StringType() {
var _this =
_super.call(
this,
"string",
function (u) {
return typeof u === "string";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "StringType";
return _this;
}
return StringType;
})(Type))();
var number = new ((function (_super) {
__extends(NumberType, _super);
function NumberType() {
var _this =
_super.call(
this,
"number",
function (u) {
return typeof u === "number";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "NumberType";
return _this;
}
return NumberType;
})(Type))();
new ((function (_super) {
__extends(BigIntType, _super);
function BigIntType() {
var _this =
_super.call(
this,
"bigint",
function (u) {
return typeof u === "bigint";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "BigIntType";
return _this;
}
return BigIntType;
})(Type))();
new ((function (_super) {
__extends(BooleanType, _super);
function BooleanType() {
var _this =
_super.call(
this,
"boolean",
function (u) {
return typeof u === "boolean";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "BooleanType";
return _this;
}
return BooleanType;
})(Type))();
var UnknownArray = new ((function (_super) {
__extends(AnyArrayType, _super);
function AnyArrayType() {
var _this =
_super.call(
this,
"UnknownArray",
Array.isArray,
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "AnyArrayType";
return _this;
}
return AnyArrayType;
})(Type))();
var UnknownRecord = new ((function (_super) {
__extends(AnyDictionaryType, _super);
function AnyDictionaryType() {
var _this =
_super.call(
this,
"UnknownRecord",
function (u) {
return (
u !== null &&
typeof u === "object" &&
!Array.isArray(u)
);
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "AnyDictionaryType";
return _this;
}
return AnyDictionaryType;
})(Type))();
var LiteralType = (function (_super) {
__extends(LiteralType, _super);
function LiteralType(name, is, validate, encode, value) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.value = value;
_this._tag = "LiteralType";
return _this;
}
return LiteralType;
})(Type);
function literal(value, name) {
if (name === void 0) name = JSON.stringify(value);
var is = function (u) {
return u === value;
};
return new LiteralType(
name,
is,
function (u, c) {
return is(u) ? success(value) : failure(u, c);
},
identity,
value,
);
}
(function (_super) {
__extends(KeyofType, _super);
function KeyofType(name, is, validate, encode, keys) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.keys = keys;
_this._tag = "KeyofType";
return _this;
}
return KeyofType;
})(Type);
var RefinementType = (function (_super) {
__extends(RefinementType, _super);
function RefinementType(
name,
is,
validate,
encode,
type,
predicate,
) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this.predicate = predicate;
_this._tag = "RefinementType";
return _this;
}
return RefinementType;
})(Type);
function brand(codec, predicate, name) {
return refinement(codec, predicate, name);
}
brand(
number,
function (n) {
return Number.isInteger(n);
},
"Int",
);
var RecursiveType = (function (_super) {
__extends(RecursiveType, _super);
function RecursiveType(
name,
is,
validate,
encode,
runDefinition,
) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.runDefinition = runDefinition;
_this._tag = "RecursiveType";
return _this;
}
return RecursiveType;
})(Type);
Object.defineProperty(RecursiveType.prototype, "type", {
get: function () {
return this.runDefinition();
},
enumerable: true,
configurable: true,
});
var ArrayType = (function (_super) {
__extends(ArrayType, _super);
function ArrayType(name, is, validate, encode, type) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ArrayType";
return _this;
}
return ArrayType;
})(Type);
function array(item, name) {
if (name === void 0)
name = "Array<".concat(item.name, ">");
return new ArrayType(
name,
function (u) {
return UnknownArray.is(u) && u.every(item.is);
},
function (u, c) {
var e = UnknownArray.validate(u, c);
if (isLeft(e)) return e;
var us = e.right;
var len = us.length;
var as = us;
var errors = [];
for (var i = 0; i < len; i++) {
var ui = us[i];
var result = item.validate(
ui,
appendContext(c, String(i), item, ui),
);
if (isLeft(result)) pushAll(errors, result.left);
else {
var ai = result.right;
if (ai !== ui) {
if (as === us) as = us.slice();
as[i] = ai;
}
}
}
return errors.length > 0
? failures(errors)
: success(as);
},
item.encode === identity
? identity
: function (a) {
return a.map(item.encode);
},
item,
);
}
var InterfaceType = (function (_super) {
__extends(InterfaceType, _super);
function InterfaceType(
name,
is,
validate,
encode,
props,
) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = "InterfaceType";
return _this;
}
return InterfaceType;
})(Type);
function type(props, name) {
if (name === void 0) name = getInterfaceTypeName(props);
var keys = Object.keys(props);
var types = keys.map(function (key) {
return props[key];
});
var len = keys.length;
return new InterfaceType(
name,
function (u) {
if (UnknownRecord.is(u)) {
for (var i = 0; i < len; i++) {
var k = keys[i];
var uk = u[k];
if (
(uk === void 0 && !hasOwnProperty.call(u, k)) ||
!types[i].is(uk)
)
return false;
}
return true;
}
return false;
},
function (u, c) {
var e = UnknownRecord.validate(u, c);
if (isLeft(e)) return e;
var o = e.right;
var a = o;
var errors = [];
for (var i = 0; i < len; i++) {
var k = keys[i];
var ak = a[k];
var type_1 = types[i];
var result = type_1.validate(
ak,
appendContext(c, k, type_1, ak),
);
if (isLeft(result)) pushAll(errors, result.left);
else {
var vak = result.right;
if (
vak !== ak ||
(vak === void 0 && !hasOwnProperty.call(a, k))
) {
if (a === o) a = __assign({}, o);
a[k] = vak;
}
}
}
return errors.length > 0
? failures(errors)
: success(a);
},
useIdentity(types)
? identity
: function (a) {
var s = __assign({}, a);
for (var i = 0; i < len; i++) {
var k = keys[i];
var encode = types[i].encode;
if (encode !== identity) s[k] = encode(a[k]);
}
return s;
},
props,
);
}
(function (_super) {
__extends(PartialType, _super);
function PartialType(name, is, validate, encode, props) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = "PartialType";
return _this;
}
return PartialType;
})(Type);
(function (_super) {
__extends(DictionaryType, _super);
function DictionaryType(
name,
is,
validate,
encode,
domain,
codomain,
) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.domain = domain;
_this.codomain = codomain;
_this._tag = "DictionaryType";
return _this;
}
return DictionaryType;
})(Type);
var UnionType = (function (_super) {
__extends(UnionType, _super);
function UnionType(name, is, validate, encode, types) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.types = types;
_this._tag = "UnionType";
return _this;
}
return UnionType;
})(Type);
function union(codecs, name) {
if (name === void 0) name = getUnionName(codecs);
var index = getIndex(codecs);
if (index !== void 0 && codecs.length > 0) {
var tag_1 = index[0],
groups_1 = index[1];
var len_1 = groups_1.length;
var find_1 = function (value) {
for (var i = 0; i < len_1; i++)
if (groups_1[i].indexOf(value) !== -1) return i;
};
return new TaggedUnionType(
name,
function (u) {
if (UnknownRecord.is(u)) {
var i = find_1(u[tag_1]);
return i !== void 0 ? codecs[i].is(u) : false;
}
return false;
},
function (u, c) {
var e = UnknownRecord.validate(u, c);
if (isLeft(e)) return e;
var r = e.right;
var i = find_1(r[tag_1]);
if (i === void 0) return failure(u, c);
var codec = codecs[i];
return codec.validate(
r,
appendContext(c, String(i), codec, r),
);
},
useIdentity(codecs)
? identity
: function (a) {
var i = find_1(a[tag_1]);
if (i === void 0)
throw new Error(
"no codec found to encode value in union codec ".concat(
name,
),
);
else return codecs[i].encode(a);
},
codecs,
tag_1,
);
} else
return new UnionType(
name,
function (u) {
return codecs.some(function (type) {
return type.is(u);
});
},
function (u, c) {
var errors = [];
for (var i = 0; i < codecs.length; i++) {
var codec = codecs[i];
var result = codec.validate(
u,
appendContext(c, String(i), codec, u),
);
if (isLeft(result)) pushAll(errors, result.left);
else return success(result.right);
}
return failures(errors);
},
useIdentity(codecs)
? identity
: function (a) {
for (
var _i = 0, codecs_1 = codecs;
_i < codecs_1.length;
_i++
) {
var codec = codecs_1[_i];
if (codec.is(a)) return codec.encode(a);
}
throw new Error(
"no codec found to encode value in union type ".concat(
name,
),
);
},
codecs,
);
}
(function (_super) {
__extends(IntersectionType, _super);
function IntersectionType(
name,
is,
validate,
encode,
types,
) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.types = types;
_this._tag = "IntersectionType";
return _this;
}
return IntersectionType;
})(Type);
(function (_super) {
__extends(TupleType, _super);
function TupleType(name, is, validate, encode, types) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.types = types;
_this._tag = "TupleType";
return _this;
}
return TupleType;
})(Type);
(function (_super) {
__extends(ReadonlyType, _super);
function ReadonlyType(name, is, validate, encode, type) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ReadonlyType";
return _this;
}
return ReadonlyType;
})(Type);
(function (_super) {
__extends(ReadonlyArrayType, _super);
function ReadonlyArrayType(
name,
is,
validate,
encode,
type,
) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ReadonlyArrayType";
return _this;
}
return ReadonlyArrayType;
})(Type);
(function (_super) {
__extends(ExactType, _super);
function ExactType(name, is, validate, encode, type) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ExactType";
return _this;
}
return ExactType;
})(Type);
new ((function (_super) {
__extends(FunctionType, _super);
function FunctionType() {
var _this =
_super.call(
this,
"Function",
function (u) {
return typeof u === "function";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "FunctionType";
return _this;
}
return FunctionType;
})(Type))();
new ((function (_super) {
__extends(NeverType, _super);
function NeverType() {
var _this =
_super.call(
this,
"never",
function (_) {
return false;
},
function (u, c) {
return failure(u, c);
},
function () {
throw new Error("cannot encode never");
},
) || this;
_this._tag = "NeverType";
return _this;
}
return NeverType;
})(Type))();
new ((function (_super) {
__extends(AnyType, _super);
function AnyType() {
var _this =
_super.call(
this,
"any",
function (_) {
return true;
},
success,
identity,
) || this;
_this._tag = "AnyType";
return _this;
}
return AnyType;
})(Type))();
function refinement(codec, predicate, name) {
if (name === void 0)
name = "("
.concat(codec.name, " | ")
.concat(getFunctionName(predicate), ")");
return new RefinementType(
name,
function (u) {
return codec.is(u) && predicate(u);
},
function (i, c) {
var e = codec.validate(i, c);
if (isLeft(e)) return e;
var a = e.right;
return predicate(a) ? success(a) : failure(a, c);
},
codec.encode,
codec,
predicate,
);
}
refinement(number, Number.isInteger, "Integer");
var TaggedUnionType = (function (_super) {
__extends(TaggedUnionType, _super);
function TaggedUnionType(
name,
is,
validate,
encode,
codecs,
tag,
) {
var _this =
_super.call(
this,
name,
is,
validate,
encode,
codecs,
) || this;
_this.tag = tag;
return _this;
}
return TaggedUnionType;
})(UnionType);
new ((function (_super) {
__extends(ObjectType, _super);
function ObjectType() {
var _this =
_super.call(
this,
"object",
function (u) {
return u !== null && typeof u === "object";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "ObjectType";
return _this;
}
return ObjectType;
})(Type))();
(function (_super) {
__extends(StrictType, _super);
function StrictType(name, is, validate, encode, props) {
var _this =
_super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = "StrictType";
return _this;
}
return StrictType;
})(Type);
function fromRefinement(name, is) {
return new Type(
name,
is,
function (u, c) {
return is(u) ? success(u) : failure(u, c);
},
identity,
);
}
var isDate = function (u) {
return u instanceof Date;
};
var date = fromRefinement("Date", isDate);
const stringWithLength = (min, max) =>
refinement(
string,
(s) => s.length >= min && s.length <= max,
`string with length between ${min} and ${max}`,
);
const numberInRange = (min, max) =>
refinement(
number,
(n) => n >= min && n <= max,
`number between ${min} and ${max}`,
);
const urlString = refinement(
string,
(s) => URL.canParse(s),
"url",
);
const literals = ([l1, l2, ...literals]) =>
union([
literal(l1),
literal(l2),
...literals.map((l) => literal(l)),
]);
const ImageData = type({
id: number,
created: date,
title: stringWithLength(1, 100),
type: literals(["jpg", "png"]),
size: number,
url: urlString,
});
const RatingData = type({
id: number,
stars: numberInRange(1, 5),
title: stringWithLength(1, 100),
text: stringWithLength(1, 1e3),
images: array(ImageData),
});
type(
{
id: number,
created: date,
title: stringWithLength(1, 100),
brand: stringWithLength(1, 30),
description: stringWithLength(1, 500),
price: numberInRange(1, 1e4),
discount: union([numberInRange(1, 100), nullType]),
quantity: numberInRange(0, 10),
tags: array(stringWithLength(1, 30)),
images: array(ImageData),
ratings: array(RatingData),
},
"ProductData",
).decode({});
function identity$1(a) {
return a;
}
function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
switch (arguments.length) {
case 1:
return a;
case 2:
return ab(a);
case 3:
return bc(ab(a));
case 4:
return cd(bc(ab(a)));
case 5:
return de(cd(bc(ab(a))));
case 6:
return ef(de(cd(bc(ab(a)))));
case 7:
return fg(ef(de(cd(bc(ab(a))))));
case 8:
return gh(fg(ef(de(cd(bc(ab(a)))))));
case 9:
return hi(gh(fg(ef(de(cd(bc(ab(a))))))));
default:
var ret = arguments[0];
for (var i = 1; i < arguments.length; i++) ret = arguments[i](ret);
return ret;
}
}
var isLeft$1 = function (ma) {
return ma._tag === "Left";
};
var left$1 = function (e) {
return {
_tag: "Left",
left: e,
};
};
var right$1 = function (a) {
return {
_tag: "Right",
right: a,
};
};
function as$1(F) {
return function (self, b) {
return F.map(self, function () {
return b;
});
};
}
function asUnit$1(F) {
var asM = as$1(F);
return function (self) {
return asM(self, void 0);
};
}
var left = left$1;
var right = right$1;
var _map = function (fa, f) {
return pipe(fa, map(f));
};
var URI = "Either";
var map = function (f) {
return function (fa) {
return isLeft(fa) ? fa : right(f(fa.right));
};
};
asUnit$1({
URI,
map: _map,
});
var FromEither = {
URI,
fromEither: identity$1,
};
var isLeft = isLeft$1;
FromEither.fromEither;
var __extends = (function () {
var extendStatics = function (d, b) {
extendStatics =
Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array &&
function (d, b) {
d.__proto__ = b;
}) ||
function (d, b) {
for (var p in b)
if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
};
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError(
"Class extends value " + String(b) + " is not a constructor or null",
);
extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype =
b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
};
})();
var __assign = function () {
__assign =
Object.assign ||
function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var failures = left;
var failure = function (value, context, message) {
return failures([
{
value,
context,
message,
},
]);
};
var success = right;
var Type = (function () {
function Type(name, is, validate, encode) {
this.name = name;
this.is = is;
this.validate = validate;
this.encode = encode;
this.decode = this.decode.bind(this);
}
Type.prototype.pipe = function (ab, name) {
var _this = this;
if (name === void 0)
name = "pipe(".concat(this.name, ", ").concat(ab.name, ")");
return new Type(
name,
ab.is,
function (i, c) {
var e = _this.validate(i, c);
if (isLeft(e)) return e;
return ab.validate(e.right, c);
},
this.encode === identity && ab.encode === identity
? identity
: function (b) {
return _this.encode(ab.encode(b));
},
);
};
Type.prototype.asDecoder = function () {
return this;
};
Type.prototype.asEncoder = function () {
return this;
};
Type.prototype.decode = function (i) {
return this.validate(i, [
{
key: "",
type: this,
actual: i,
},
]);
};
return Type;
})();
var identity = function (a) {
return a;
};
function getFunctionName(f) {
return f.displayName || f.name || "<function".concat(f.length, ">");
}
function appendContext(c, key, decoder, actual) {
var len = c.length;
var r = Array(len + 1);
for (var i = 0; i < len; i++) r[i] = c[i];
r[len] = {
key,
type: decoder,
actual,
};
return r;
}
function pushAll(xs, ys) {
var l = ys.length;
for (var i = 0; i < l; i++) xs.push(ys[i]);
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
function getNameFromProps(props) {
return Object.keys(props)
.map(function (k) {
return "".concat(k, ": ").concat(props[k].name);
})
.join(", ");
}
function useIdentity(codecs) {
for (var i = 0; i < codecs.length; i++)
if (codecs[i].encode !== identity) return false;
return true;
}
function getInterfaceTypeName(props) {
return "{ ".concat(getNameFromProps(props), " }");
}
function getUnionName(codecs) {
return (
"(" +
codecs
.map(function (type) {
return type.name;
})
.join(" | ") +
")"
);
}
function isNonEmpty(as) {
return as.length > 0;
}
var emptyTags = {};
function intersect(a, b) {
var r = [];
for (var _i = 0, a_1 = a; _i < a_1.length; _i++) {
var v = a_1[_i];
if (b.indexOf(v) !== -1) r.push(v);
}
return r;
}
function mergeTags(a, b) {
if (a === emptyTags) return b;
if (b === emptyTags) return a;
var r = Object.assign({}, a);
for (var k in b)
if (hasOwnProperty.call(a, k)) {
var intersection_1 = intersect(a[k], b[k]);
if (isNonEmpty(intersection_1)) r[k] = intersection_1;
else {
r = emptyTags;
break;
}
} else r[k] = b[k];
return r;
}
function intersectTags(a, b) {
if (a === emptyTags || b === emptyTags) return emptyTags;
var r = emptyTags;
for (var k in a)
if (hasOwnProperty.call(b, k)) {
if (intersect(a[k], b[k]).length === 0) {
if (r === emptyTags) r = {};
r[k] = a[k].concat(b[k]);
}
}
return r;
}
function isLiteralC(codec) {
return codec._tag === "LiteralType";
}
function isTypeC(codec) {
return codec._tag === "InterfaceType";
}
function isStrictC(codec) {
return codec._tag === "StrictType";
}
function isExactC(codec) {
return codec._tag === "ExactType";
}
function isRefinementC(codec) {
return codec._tag === "RefinementType";
}
function isIntersectionC(codec) {
return codec._tag === "IntersectionType";
}
function isUnionC(codec) {
return codec._tag === "UnionType";
}
function isRecursiveC(codec) {
return codec._tag === "RecursiveType";
}
function isReadonlyC(codec) {
return codec._tag === "ReadonlyType";
}
var lazyCodecs = [];
function getTags(codec) {
if (lazyCodecs.indexOf(codec) !== -1) return emptyTags;
if (isTypeC(codec) || isStrictC(codec)) {
var index = emptyTags;
for (var k in codec.props) {
var prop = codec.props[k];
if (isLiteralC(prop)) {
if (index === emptyTags) index = {};
index[k] = [prop.value];
}
}
return index;
} else if (isExactC(codec) || isRefinementC(codec) || isReadonlyC(codec))
return getTags(codec.type);
else if (isIntersectionC(codec))
return codec.types.reduce(function (tags, codec) {
return mergeTags(tags, getTags(codec));
}, emptyTags);
else if (isUnionC(codec))
return codec.types.slice(1).reduce(function (tags, codec) {
return intersectTags(tags, getTags(codec));
}, getTags(codec.types[0]));
else if (isRecursiveC(codec)) {
lazyCodecs.push(codec);
var tags = getTags(codec.type);
lazyCodecs.pop();
return tags;
}
return emptyTags;
}
function getIndex(codecs) {
var tags = getTags(codecs[0]);
var keys = Object.keys(tags);
var len = codecs.length;
var _loop_1 = function (k) {
var all = tags[k].slice();
var index = [tags[k]];
for (var i = 1; i < len; i++) {
var codec = codecs[i];
var values = getTags(codec)[k];
if (values === void 0) return "continue-keys";
else if (
values.some(function (v) {
return all.indexOf(v) !== -1;
})
)
return "continue-keys";
else {
all.push.apply(all, values);
index.push(values);
}
}
return { value: [k, index] };
};
keys: for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var k = keys_1[_i];
var state_1 = _loop_1(k);
if (typeof state_1 === "object") return state_1.value;
switch (state_1) {
case "continue-keys":
continue keys;
}
}
}
var nullType = new ((function (_super) {
__extends(NullType, _super);
function NullType() {
var _this =
_super.call(
this,
"null",
function (u) {
return u === null;
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "NullType";
return _this;
}
return NullType;
})(Type))();
var undefinedType = new ((function (_super) {
__extends(UndefinedType, _super);
function UndefinedType() {
var _this =
_super.call(
this,
"undefined",
function (u) {
return u === void 0;
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "UndefinedType";
return _this;
}
return UndefinedType;
})(Type))();
new ((function (_super) {
__extends(VoidType, _super);
function VoidType() {
var _this =
_super.call(
this,
"void",
undefinedType.is,
undefinedType.validate,
identity,
) || this;
_this._tag = "VoidType";
return _this;
}
return VoidType;
})(Type))();
new ((function (_super) {
__extends(UnknownType, _super);
function UnknownType() {
var _this =
_super.call(
this,
"unknown",
function (_) {
return true;
},
success,
identity,
) || this;
_this._tag = "UnknownType";
return _this;
}
return UnknownType;
})(Type))();
var string = new ((function (_super) {
__extends(StringType, _super);
function StringType() {
var _this =
_super.call(
this,
"string",
function (u) {
return typeof u === "string";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "StringType";
return _this;
}
return StringType;
})(Type))();
var number = new ((function (_super) {
__extends(NumberType, _super);
function NumberType() {
var _this =
_super.call(
this,
"number",
function (u) {
return typeof u === "number";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "NumberType";
return _this;
}
return NumberType;
})(Type))();
new ((function (_super) {
__extends(BigIntType, _super);
function BigIntType() {
var _this =
_super.call(
this,
"bigint",
function (u) {
return typeof u === "bigint";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "BigIntType";
return _this;
}
return BigIntType;
})(Type))();
new ((function (_super) {
__extends(BooleanType, _super);
function BooleanType() {
var _this =
_super.call(
this,
"boolean",
function (u) {
return typeof u === "boolean";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "BooleanType";
return _this;
}
return BooleanType;
})(Type))();
var UnknownArray = new ((function (_super) {
__extends(AnyArrayType, _super);
function AnyArrayType() {
var _this =
_super.call(
this,
"UnknownArray",
Array.isArray,
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "AnyArrayType";
return _this;
}
return AnyArrayType;
})(Type))();
var UnknownRecord = new ((function (_super) {
__extends(AnyDictionaryType, _super);
function AnyDictionaryType() {
var _this =
_super.call(
this,
"UnknownRecord",
function (u) {
return u !== null && typeof u === "object" && !Array.isArray(u);
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "AnyDictionaryType";
return _this;
}
return AnyDictionaryType;
})(Type))();
var LiteralType = (function (_super) {
__extends(LiteralType, _super);
function LiteralType(name, is, validate, encode, value) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.value = value;
_this._tag = "LiteralType";
return _this;
}
return LiteralType;
})(Type);
function literal(value, name) {
if (name === void 0) name = JSON.stringify(value);
var is = function (u) {
return u === value;
};
return new LiteralType(
name,
is,
function (u, c) {
return is(u) ? success(value) : failure(u, c);
},
identity,
value,
);
}
(function (_super) {
__extends(KeyofType, _super);
function KeyofType(name, is, validate, encode, keys) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.keys = keys;
_this._tag = "KeyofType";
return _this;
}
return KeyofType;
})(Type);
var RefinementType = (function (_super) {
__extends(RefinementType, _super);
function RefinementType(name, is, validate, encode, type, predicate) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this.predicate = predicate;
_this._tag = "RefinementType";
return _this;
}
return RefinementType;
})(Type);
function brand(codec, predicate, name) {
return refinement(codec, predicate, name);
}
brand(
number,
function (n) {
return Number.isInteger(n);
},
"Int",
);
var RecursiveType = (function (_super) {
__extends(RecursiveType, _super);
function RecursiveType(name, is, validate, encode, runDefinition) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.runDefinition = runDefinition;
_this._tag = "RecursiveType";
return _this;
}
return RecursiveType;
})(Type);
Object.defineProperty(RecursiveType.prototype, "type", {
get: function () {
return this.runDefinition();
},
enumerable: true,
configurable: true,
});
var ArrayType = (function (_super) {
__extends(ArrayType, _super);
function ArrayType(name, is, validate, encode, type) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ArrayType";
return _this;
}
return ArrayType;
})(Type);
function array(item, name) {
if (name === void 0) name = "Array<".concat(item.name, ">");
return new ArrayType(
name,
function (u) {
return UnknownArray.is(u) && u.every(item.is);
},
function (u, c) {
var e = UnknownArray.validate(u, c);
if (isLeft(e)) return e;
var us = e.right;
var len = us.length;
var as = us;
var errors = [];
for (var i = 0; i < len; i++) {
var ui = us[i];
var result = item.validate(ui, appendContext(c, String(i), item, ui));
if (isLeft(result)) pushAll(errors, result.left);
else {
var ai = result.right;
if (ai !== ui) {
if (as === us) as = us.slice();
as[i] = ai;
}
}
}
return errors.length > 0 ? failures(errors) : success(as);
},
item.encode === identity
? identity
: function (a) {
return a.map(item.encode);
},
item,
);
}
var InterfaceType = (function (_super) {
__extends(InterfaceType, _super);
function InterfaceType(name, is, validate, encode, props) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = "InterfaceType";
return _this;
}
return InterfaceType;
})(Type);
function type(props, name) {
if (name === void 0) name = getInterfaceTypeName(props);
var keys = Object.keys(props);
var types = keys.map(function (key) {
return props[key];
});
var len = keys.length;
return new InterfaceType(
name,
function (u) {
if (UnknownRecord.is(u)) {
for (var i = 0; i < len; i++) {
var k = keys[i];
var uk = u[k];
if ((uk === void 0 && !hasOwnProperty.call(u, k)) || !types[i].is(uk))
return false;
}
return true;
}
return false;
},
function (u, c) {
var e = UnknownRecord.validate(u, c);
if (isLeft(e)) return e;
var o = e.right;
var a = o;
var errors = [];
for (var i = 0; i < len; i++) {
var k = keys[i];
var ak = a[k];
var type_1 = types[i];
var result = type_1.validate(ak, appendContext(c, k, type_1, ak));
if (isLeft(result)) pushAll(errors, result.left);
else {
var vak = result.right;
if (vak !== ak || (vak === void 0 && !hasOwnProperty.call(a, k))) {
if (a === o) a = __assign({}, o);
a[k] = vak;
}
}
}
return errors.length > 0 ? failures(errors) : success(a);
},
useIdentity(types)
? identity
: function (a) {
var s = __assign({}, a);
for (var i = 0; i < len; i++) {
var k = keys[i];
var encode = types[i].encode;
if (encode !== identity) s[k] = encode(a[k]);
}
return s;
},
props,
);
}
(function (_super) {
__extends(PartialType, _super);
function PartialType(name, is, validate, encode, props) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = "PartialType";
return _this;
}
return PartialType;
})(Type);
(function (_super) {
__extends(DictionaryType, _super);
function DictionaryType(name, is, validate, encode, domain, codomain) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.domain = domain;
_this.codomain = codomain;
_this._tag = "DictionaryType";
return _this;
}
return DictionaryType;
})(Type);
var UnionType = (function (_super) {
__extends(UnionType, _super);
function UnionType(name, is, validate, encode, types) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.types = types;
_this._tag = "UnionType";
return _this;
}
return UnionType;
})(Type);
function union(codecs, name) {
if (name === void 0) name = getUnionName(codecs);
var index = getIndex(codecs);
if (index !== void 0 && codecs.length > 0) {
var tag_1 = index[0],
groups_1 = index[1];
var len_1 = groups_1.length;
var find_1 = function (value) {
for (var i = 0; i < len_1; i++)
if (groups_1[i].indexOf(value) !== -1) return i;
};
return new TaggedUnionType(
name,
function (u) {
if (UnknownRecord.is(u)) {
var i = find_1(u[tag_1]);
return i !== void 0 ? codecs[i].is(u) : false;
}
return false;
},
function (u, c) {
var e = UnknownRecord.validate(u, c);
if (isLeft(e)) return e;
var r = e.right;
var i = find_1(r[tag_1]);
if (i === void 0) return failure(u, c);
var codec = codecs[i];
return codec.validate(r, appendContext(c, String(i), codec, r));
},
useIdentity(codecs)
? identity
: function (a) {
var i = find_1(a[tag_1]);
if (i === void 0)
throw new Error(
"no codec found to encode value in union codec ".concat(name),
);
else return codecs[i].encode(a);
},
codecs,
tag_1,
);
} else
return new UnionType(
name,
function (u) {
return codecs.some(function (type) {
return type.is(u);
});
},
function (u, c) {
var errors = [];
for (var i = 0; i < codecs.length; i++) {
var codec = codecs[i];
var result = codec.validate(u, appendContext(c, String(i), codec, u));
if (isLeft(result)) pushAll(errors, result.left);
else return success(result.right);
}
return failures(errors);
},
useIdentity(codecs)
? identity
: function (a) {
for (var _i = 0, codecs_1 = codecs; _i < codecs_1.length; _i++) {
var codec = codecs_1[_i];
if (codec.is(a)) return codec.encode(a);
}
throw new Error(
"no codec found to encode value in union type ".concat(name),
);
},
codecs,
);
}
(function (_super) {
__extends(IntersectionType, _super);
function IntersectionType(name, is, validate, encode, types) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.types = types;
_this._tag = "IntersectionType";
return _this;
}
return IntersectionType;
})(Type);
(function (_super) {
__extends(TupleType, _super);
function TupleType(name, is, validate, encode, types) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.types = types;
_this._tag = "TupleType";
return _this;
}
return TupleType;
})(Type);
(function (_super) {
__extends(ReadonlyType, _super);
function ReadonlyType(name, is, validate, encode, type) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ReadonlyType";
return _this;
}
return ReadonlyType;
})(Type);
(function (_super) {
__extends(ReadonlyArrayType, _super);
function ReadonlyArrayType(name, is, validate, encode, type) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ReadonlyArrayType";
return _this;
}
return ReadonlyArrayType;
})(Type);
(function (_super) {
__extends(ExactType, _super);
function ExactType(name, is, validate, encode, type) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ExactType";
return _this;
}
return ExactType;
})(Type);
new ((function (_super) {
__extends(FunctionType, _super);
function FunctionType() {
var _this =
_super.call(
this,
"Function",
function (u) {
return typeof u === "function";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "FunctionType";
return _this;
}
return FunctionType;
})(Type))();
new ((function (_super) {
__extends(NeverType, _super);
function NeverType() {
var _this =
_super.call(
this,
"never",
function (_) {
return false;
},
function (u, c) {
return failure(u, c);
},
function () {
throw new Error("cannot encode never");
},
) || this;
_this._tag = "NeverType";
return _this;
}
return NeverType;
})(Type))();
new ((function (_super) {
__extends(AnyType, _super);
function AnyType() {
var _this =
_super.call(
this,
"any",
function (_) {
return true;
},
success,
identity,
) || this;
_this._tag = "AnyType";
return _this;
}
return AnyType;
})(Type))();
function refinement(codec, predicate, name) {
if (name === void 0)
name = "("
.concat(codec.name, " | ")
.concat(getFunctionName(predicate), ")");
return new RefinementType(
name,
function (u) {
return codec.is(u) && predicate(u);
},
function (i, c) {
var e = codec.validate(i, c);
if (isLeft(e)) return e;
var a = e.right;
return predicate(a) ? success(a) : failure(a, c);
},
codec.encode,
codec,
predicate,
);
}
refinement(number, Number.isInteger, "Integer");
var TaggedUnionType = (function (_super) {
__extends(TaggedUnionType, _super);
function TaggedUnionType(name, is, validate, encode, codecs, tag) {
var _this = _super.call(this, name, is, validate, encode, codecs) || this;
_this.tag = tag;
return _this;
}
return TaggedUnionType;
})(UnionType);
new ((function (_super) {
__extends(ObjectType, _super);
function ObjectType() {
var _this =
_super.call(
this,
"object",
function (u) {
return u !== null && typeof u === "object";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "ObjectType";
return _this;
}
return ObjectType;
})(Type))();
(function (_super) {
__extends(StrictType, _super);
function StrictType(name, is, validate, encode, props) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = "StrictType";
return _this;
}
return StrictType;
})(Type);
function fromRefinement(name, is) {
return new Type(
name,
is,
function (u, c) {
return is(u) ? success(u) : failure(u, c);
},
identity,
);
}
var isDate = function (u) {
return u instanceof Date;
};
var date = fromRefinement("Date", isDate);
const stringWithLength = (min, max) =>
refinement(
string,
(s) => s.length >= min && s.length <= max,
`string with length between ${min} and ${max}`,
);
const numberInRange = (min, max) =>
refinement(
number,
(n) => n >= min && n <= max,
`number between ${min} and ${max}`,
);
const urlString = refinement(string, (s) => URL.canParse(s), "url");
const literals = ([l1, l2, ...literals]) =>
union([literal(l1), literal(l2), ...literals.map((l) => literal(l))]);
const ImageData = type({
id: number,
created: date,
title: stringWithLength(1, 100),
type: literals(["jpg", "png"]),
size: number,
url: urlString,
});
const RatingData = type({
id: number,
stars: numberInRange(1, 5),
title: stringWithLength(1, 100),
text: stringWithLength(1, 1e3),
images: array(ImageData),
});
type(
{
id: number,
created: date,
title: stringWithLength(1, 100),
brand: stringWithLength(1, 30),
description: stringWithLength(1, 500),
price: numberInRange(1, 1e4),
discount: union([numberInRange(1, 100), nullType]),
quantity: numberInRange(0, 10),
tags: array(stringWithLength(1, 30)),
images: array(ImageData),
ratings: array(RatingData),
},
"ProductData",
).decode({});
function identity$1(a) {
return a;
}
function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
switch (arguments.length) {
case 1:
return a;
case 2:
return ab(a);
case 3:
return bc(ab(a));
case 4:
return cd(bc(ab(a)));
case 5:
return de(cd(bc(ab(a))));
case 6:
return ef(de(cd(bc(ab(a)))));
case 7:
return fg(ef(de(cd(bc(ab(a))))));
case 8:
return gh(fg(ef(de(cd(bc(ab(a)))))));
case 9:
return hi(gh(fg(ef(de(cd(bc(ab(a))))))));
default:
var ret = arguments[0];
for (var i = 1; i < arguments.length; i++) ret = arguments[i](ret);
return ret;
}
}
var isLeft$1 = function (ma) {
return ma._tag === "Left";
};
var left$1 = function (e) {
return {
_tag: "Left",
left: e,
};
};
var right$1 = function (a) {
return {
_tag: "Right",
right: a,
};
};
function as$1(F) {
return function (self, b) {
return F.map(self, function () {
return b;
});
};
}
function asUnit$1(F) {
var asM = as$1(F);
return function (self) {
return asM(self, void 0);
};
}
var left = left$1;
var right = right$1;
var _map = function (fa, f) {
return pipe(fa, map(f));
};
var URI = "Either";
var map = function (f) {
return function (fa) {
return isLeft(fa) ? fa : right(f(fa.right));
};
};
asUnit$1({
URI,
map: _map,
});
var FromEither = {
URI,
fromEither: identity$1,
};
var isLeft = isLeft$1;
FromEither.fromEither;
var __extends = (function () {
var extendStatics = function (d, b) {
extendStatics =
Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array &&
function (d, b) {
d.__proto__ = b;
}) ||
function (d, b) {
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
};
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
};
})();
var __assign = function () {
__assign =
Object.assign ||
function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var failures = left;
var failure = function (value, context, message) {
return failures([
{
value,
context,
message,
},
]);
};
var success = right;
var Type = (function () {
function Type(name, is, validate, encode) {
this.name = name;
this.is = is;
this.validate = validate;
this.encode = encode;
this.decode = this.decode.bind(this);
}
Type.prototype.pipe = function (ab, name) {
var _this = this;
if (name === void 0) name = "pipe(".concat(this.name, ", ").concat(ab.name, ")");
return new Type(
name,
ab.is,
function (i, c) {
var e = _this.validate(i, c);
if (isLeft(e)) return e;
return ab.validate(e.right, c);
},
this.encode === identity && ab.encode === identity
? identity
: function (b) {
return _this.encode(ab.encode(b));
},
);
};
Type.prototype.asDecoder = function () {
return this;
};
Type.prototype.asEncoder = function () {
return this;
};
Type.prototype.decode = function (i) {
return this.validate(i, [
{
key: "",
type: this,
actual: i,
},
]);
};
return Type;
})();
var identity = function (a) {
return a;
};
function getFunctionName(f) {
return f.displayName || f.name || "<function".concat(f.length, ">");
}
function appendContext(c, key, decoder, actual) {
var len = c.length;
var r = Array(len + 1);
for (var i = 0; i < len; i++) r[i] = c[i];
r[len] = {
key,
type: decoder,
actual,
};
return r;
}
function pushAll(xs, ys) {
var l = ys.length;
for (var i = 0; i < l; i++) xs.push(ys[i]);
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
function getNameFromProps(props) {
return Object.keys(props)
.map(function (k) {
return "".concat(k, ": ").concat(props[k].name);
})
.join(", ");
}
function useIdentity(codecs) {
for (var i = 0; i < codecs.length; i++) if (codecs[i].encode !== identity) return false;
return true;
}
function getInterfaceTypeName(props) {
return "{ ".concat(getNameFromProps(props), " }");
}
function getUnionName(codecs) {
return (
"(" +
codecs
.map(function (type) {
return type.name;
})
.join(" | ") +
")"
);
}
function isNonEmpty(as) {
return as.length > 0;
}
var emptyTags = {};
function intersect(a, b) {
var r = [];
for (var _i = 0, a_1 = a; _i < a_1.length; _i++) {
var v = a_1[_i];
if (b.indexOf(v) !== -1) r.push(v);
}
return r;
}
function mergeTags(a, b) {
if (a === emptyTags) return b;
if (b === emptyTags) return a;
var r = Object.assign({}, a);
for (var k in b)
if (hasOwnProperty.call(a, k)) {
var intersection_1 = intersect(a[k], b[k]);
if (isNonEmpty(intersection_1)) r[k] = intersection_1;
else {
r = emptyTags;
break;
}
} else r[k] = b[k];
return r;
}
function intersectTags(a, b) {
if (a === emptyTags || b === emptyTags) return emptyTags;
var r = emptyTags;
for (var k in a)
if (hasOwnProperty.call(b, k)) {
if (intersect(a[k], b[k]).length === 0) {
if (r === emptyTags) r = {};
r[k] = a[k].concat(b[k]);
}
}
return r;
}
function isLiteralC(codec) {
return codec._tag === "LiteralType";
}
function isTypeC(codec) {
return codec._tag === "InterfaceType";
}
function isStrictC(codec) {
return codec._tag === "StrictType";
}
function isExactC(codec) {
return codec._tag === "ExactType";
}
function isRefinementC(codec) {
return codec._tag === "RefinementType";
}
function isIntersectionC(codec) {
return codec._tag === "IntersectionType";
}
function isUnionC(codec) {
return codec._tag === "UnionType";
}
function isRecursiveC(codec) {
return codec._tag === "RecursiveType";
}
function isReadonlyC(codec) {
return codec._tag === "ReadonlyType";
}
var lazyCodecs = [];
function getTags(codec) {
if (lazyCodecs.indexOf(codec) !== -1) return emptyTags;
if (isTypeC(codec) || isStrictC(codec)) {
var index = emptyTags;
for (var k in codec.props) {
var prop = codec.props[k];
if (isLiteralC(prop)) {
if (index === emptyTags) index = {};
index[k] = [prop.value];
}
}
return index;
} else if (isExactC(codec) || isRefinementC(codec) || isReadonlyC(codec))
return getTags(codec.type);
else if (isIntersectionC(codec))
return codec.types.reduce(function (tags, codec) {
return mergeTags(tags, getTags(codec));
}, emptyTags);
else if (isUnionC(codec))
return codec.types.slice(1).reduce(function (tags, codec) {
return intersectTags(tags, getTags(codec));
}, getTags(codec.types[0]));
else if (isRecursiveC(codec)) {
lazyCodecs.push(codec);
var tags = getTags(codec.type);
lazyCodecs.pop();
return tags;
}
return emptyTags;
}
function getIndex(codecs) {
var tags = getTags(codecs[0]);
var keys = Object.keys(tags);
var len = codecs.length;
var _loop_1 = function (k) {
var all = tags[k].slice();
var index = [tags[k]];
for (var i = 1; i < len; i++) {
var codec = codecs[i];
var values = getTags(codec)[k];
if (values === void 0) return "continue-keys";
else if (
values.some(function (v) {
return all.indexOf(v) !== -1;
})
)
return "continue-keys";
else {
all.push.apply(all, values);
index.push(values);
}
}
return { value: [k, index] };
};
keys: for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var k = keys_1[_i];
var state_1 = _loop_1(k);
if (typeof state_1 === "object") return state_1.value;
switch (state_1) {
case "continue-keys":
continue keys;
}
}
}
var nullType = new ((function (_super) {
__extends(NullType, _super);
function NullType() {
var _this =
_super.call(
this,
"null",
function (u) {
return u === null;
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "NullType";
return _this;
}
return NullType;
})(Type))();
var undefinedType = new ((function (_super) {
__extends(UndefinedType, _super);
function UndefinedType() {
var _this =
_super.call(
this,
"undefined",
function (u) {
return u === void 0;
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "UndefinedType";
return _this;
}
return UndefinedType;
})(Type))();
new ((function (_super) {
__extends(VoidType, _super);
function VoidType() {
var _this =
_super.call(this, "void", undefinedType.is, undefinedType.validate, identity) || this;
_this._tag = "VoidType";
return _this;
}
return VoidType;
})(Type))();
new ((function (_super) {
__extends(UnknownType, _super);
function UnknownType() {
var _this =
_super.call(
this,
"unknown",
function (_) {
return true;
},
success,
identity,
) || this;
_this._tag = "UnknownType";
return _this;
}
return UnknownType;
})(Type))();
var string = new ((function (_super) {
__extends(StringType, _super);
function StringType() {
var _this =
_super.call(
this,
"string",
function (u) {
return typeof u === "string";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "StringType";
return _this;
}
return StringType;
})(Type))();
var number = new ((function (_super) {
__extends(NumberType, _super);
function NumberType() {
var _this =
_super.call(
this,
"number",
function (u) {
return typeof u === "number";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "NumberType";
return _this;
}
return NumberType;
})(Type))();
new ((function (_super) {
__extends(BigIntType, _super);
function BigIntType() {
var _this =
_super.call(
this,
"bigint",
function (u) {
return typeof u === "bigint";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "BigIntType";
return _this;
}
return BigIntType;
})(Type))();
new ((function (_super) {
__extends(BooleanType, _super);
function BooleanType() {
var _this =
_super.call(
this,
"boolean",
function (u) {
return typeof u === "boolean";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "BooleanType";
return _this;
}
return BooleanType;
})(Type))();
var UnknownArray = new ((function (_super) {
__extends(AnyArrayType, _super);
function AnyArrayType() {
var _this =
_super.call(
this,
"UnknownArray",
Array.isArray,
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "AnyArrayType";
return _this;
}
return AnyArrayType;
})(Type))();
var UnknownRecord = new ((function (_super) {
__extends(AnyDictionaryType, _super);
function AnyDictionaryType() {
var _this =
_super.call(
this,
"UnknownRecord",
function (u) {
return u !== null && typeof u === "object" && !Array.isArray(u);
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "AnyDictionaryType";
return _this;
}
return AnyDictionaryType;
})(Type))();
var LiteralType = (function (_super) {
__extends(LiteralType, _super);
function LiteralType(name, is, validate, encode, value) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.value = value;
_this._tag = "LiteralType";
return _this;
}
return LiteralType;
})(Type);
function literal(value, name) {
if (name === void 0) name = JSON.stringify(value);
var is = function (u) {
return u === value;
};
return new LiteralType(
name,
is,
function (u, c) {
return is(u) ? success(value) : failure(u, c);
},
identity,
value,
);
}
(function (_super) {
__extends(KeyofType, _super);
function KeyofType(name, is, validate, encode, keys) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.keys = keys;
_this._tag = "KeyofType";
return _this;
}
return KeyofType;
})(Type);
var RefinementType = (function (_super) {
__extends(RefinementType, _super);
function RefinementType(name, is, validate, encode, type, predicate) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this.predicate = predicate;
_this._tag = "RefinementType";
return _this;
}
return RefinementType;
})(Type);
function brand(codec, predicate, name) {
return refinement(codec, predicate, name);
}
brand(
number,
function (n) {
return Number.isInteger(n);
},
"Int",
);
var RecursiveType = (function (_super) {
__extends(RecursiveType, _super);
function RecursiveType(name, is, validate, encode, runDefinition) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.runDefinition = runDefinition;
_this._tag = "RecursiveType";
return _this;
}
return RecursiveType;
})(Type);
Object.defineProperty(RecursiveType.prototype, "type", {
get: function () {
return this.runDefinition();
},
enumerable: true,
configurable: true,
});
var ArrayType = (function (_super) {
__extends(ArrayType, _super);
function ArrayType(name, is, validate, encode, type) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ArrayType";
return _this;
}
return ArrayType;
})(Type);
function array(item, name) {
if (name === void 0) name = "Array<".concat(item.name, ">");
return new ArrayType(
name,
function (u) {
return UnknownArray.is(u) && u.every(item.is);
},
function (u, c) {
var e = UnknownArray.validate(u, c);
if (isLeft(e)) return e;
var us = e.right;
var len = us.length;
var as = us;
var errors = [];
for (var i = 0; i < len; i++) {
var ui = us[i];
var result = item.validate(ui, appendContext(c, String(i), item, ui));
if (isLeft(result)) pushAll(errors, result.left);
else {
var ai = result.right;
if (ai !== ui) {
if (as === us) as = us.slice();
as[i] = ai;
}
}
}
return errors.length > 0 ? failures(errors) : success(as);
},
item.encode === identity
? identity
: function (a) {
return a.map(item.encode);
},
item,
);
}
var InterfaceType = (function (_super) {
__extends(InterfaceType, _super);
function InterfaceType(name, is, validate, encode, props) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = "InterfaceType";
return _this;
}
return InterfaceType;
})(Type);
function type(props, name) {
if (name === void 0) name = getInterfaceTypeName(props);
var keys = Object.keys(props);
var types = keys.map(function (key) {
return props[key];
});
var len = keys.length;
return new InterfaceType(
name,
function (u) {
if (UnknownRecord.is(u)) {
for (var i = 0; i < len; i++) {
var k = keys[i];
var uk = u[k];
if ((uk === void 0 && !hasOwnProperty.call(u, k)) || !types[i].is(uk)) return false;
}
return true;
}
return false;
},
function (u, c) {
var e = UnknownRecord.validate(u, c);
if (isLeft(e)) return e;
var o = e.right;
var a = o;
var errors = [];
for (var i = 0; i < len; i++) {
var k = keys[i];
var ak = a[k];
var type_1 = types[i];
var result = type_1.validate(ak, appendContext(c, k, type_1, ak));
if (isLeft(result)) pushAll(errors, result.left);
else {
var vak = result.right;
if (vak !== ak || (vak === void 0 && !hasOwnProperty.call(a, k))) {
if (a === o) a = __assign({}, o);
a[k] = vak;
}
}
}
return errors.length > 0 ? failures(errors) : success(a);
},
useIdentity(types)
? identity
: function (a) {
var s = __assign({}, a);
for (var i = 0; i < len; i++) {
var k = keys[i];
var encode = types[i].encode;
if (encode !== identity) s[k] = encode(a[k]);
}
return s;
},
props,
);
}
(function (_super) {
__extends(PartialType, _super);
function PartialType(name, is, validate, encode, props) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = "PartialType";
return _this;
}
return PartialType;
})(Type);
(function (_super) {
__extends(DictionaryType, _super);
function DictionaryType(name, is, validate, encode, domain, codomain) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.domain = domain;
_this.codomain = codomain;
_this._tag = "DictionaryType";
return _this;
}
return DictionaryType;
})(Type);
var UnionType = (function (_super) {
__extends(UnionType, _super);
function UnionType(name, is, validate, encode, types) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.types = types;
_this._tag = "UnionType";
return _this;
}
return UnionType;
})(Type);
function union(codecs, name) {
if (name === void 0) name = getUnionName(codecs);
var index = getIndex(codecs);
if (index !== void 0 && codecs.length > 0) {
var tag_1 = index[0],
groups_1 = index[1];
var len_1 = groups_1.length;
var find_1 = function (value) {
for (var i = 0; i < len_1; i++) if (groups_1[i].indexOf(value) !== -1) return i;
};
return new TaggedUnionType(
name,
function (u) {
if (UnknownRecord.is(u)) {
var i = find_1(u[tag_1]);
return i !== void 0 ? codecs[i].is(u) : false;
}
return false;
},
function (u, c) {
var e = UnknownRecord.validate(u, c);
if (isLeft(e)) return e;
var r = e.right;
var i = find_1(r[tag_1]);
if (i === void 0) return failure(u, c);
var codec = codecs[i];
return codec.validate(r, appendContext(c, String(i), codec, r));
},
useIdentity(codecs)
? identity
: function (a) {
var i = find_1(a[tag_1]);
if (i === void 0)
throw new Error("no codec found to encode value in union codec ".concat(name));
else return codecs[i].encode(a);
},
codecs,
tag_1,
);
} else
return new UnionType(
name,
function (u) {
return codecs.some(function (type) {
return type.is(u);
});
},
function (u, c) {
var errors = [];
for (var i = 0; i < codecs.length; i++) {
var codec = codecs[i];
var result = codec.validate(u, appendContext(c, String(i), codec, u));
if (isLeft(result)) pushAll(errors, result.left);
else return success(result.right);
}
return failures(errors);
},
useIdentity(codecs)
? identity
: function (a) {
for (var _i = 0, codecs_1 = codecs; _i < codecs_1.length; _i++) {
var codec = codecs_1[_i];
if (codec.is(a)) return codec.encode(a);
}
throw new Error("no codec found to encode value in union type ".concat(name));
},
codecs,
);
}
(function (_super) {
__extends(IntersectionType, _super);
function IntersectionType(name, is, validate, encode, types) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.types = types;
_this._tag = "IntersectionType";
return _this;
}
return IntersectionType;
})(Type);
(function (_super) {
__extends(TupleType, _super);
function TupleType(name, is, validate, encode, types) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.types = types;
_this._tag = "TupleType";
return _this;
}
return TupleType;
})(Type);
(function (_super) {
__extends(ReadonlyType, _super);
function ReadonlyType(name, is, validate, encode, type) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ReadonlyType";
return _this;
}
return ReadonlyType;
})(Type);
(function (_super) {
__extends(ReadonlyArrayType, _super);
function ReadonlyArrayType(name, is, validate, encode, type) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ReadonlyArrayType";
return _this;
}
return ReadonlyArrayType;
})(Type);
(function (_super) {
__extends(ExactType, _super);
function ExactType(name, is, validate, encode, type) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.type = type;
_this._tag = "ExactType";
return _this;
}
return ExactType;
})(Type);
new ((function (_super) {
__extends(FunctionType, _super);
function FunctionType() {
var _this =
_super.call(
this,
"Function",
function (u) {
return typeof u === "function";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "FunctionType";
return _this;
}
return FunctionType;
})(Type))();
new ((function (_super) {
__extends(NeverType, _super);
function NeverType() {
var _this =
_super.call(
this,
"never",
function (_) {
return false;
},
function (u, c) {
return failure(u, c);
},
function () {
throw new Error("cannot encode never");
},
) || this;
_this._tag = "NeverType";
return _this;
}
return NeverType;
})(Type))();
new ((function (_super) {
__extends(AnyType, _super);
function AnyType() {
var _this =
_super.call(
this,
"any",
function (_) {
return true;
},
success,
identity,
) || this;
_this._tag = "AnyType";
return _this;
}
return AnyType;
})(Type))();
function refinement(codec, predicate, name) {
if (name === void 0) name = "(".concat(codec.name, " | ").concat(getFunctionName(predicate), ")");
return new RefinementType(
name,
function (u) {
return codec.is(u) && predicate(u);
},
function (i, c) {
var e = codec.validate(i, c);
if (isLeft(e)) return e;
var a = e.right;
return predicate(a) ? success(a) : failure(a, c);
},
codec.encode,
codec,
predicate,
);
}
refinement(number, Number.isInteger, "Integer");
var TaggedUnionType = (function (_super) {
__extends(TaggedUnionType, _super);
function TaggedUnionType(name, is, validate, encode, codecs, tag) {
var _this = _super.call(this, name, is, validate, encode, codecs) || this;
_this.tag = tag;
return _this;
}
return TaggedUnionType;
})(UnionType);
new ((function (_super) {
__extends(ObjectType, _super);
function ObjectType() {
var _this =
_super.call(
this,
"object",
function (u) {
return u !== null && typeof u === "object";
},
function (u, c) {
return _this.is(u) ? success(u) : failure(u, c);
},
identity,
) || this;
_this._tag = "ObjectType";
return _this;
}
return ObjectType;
})(Type))();
(function (_super) {
__extends(StrictType, _super);
function StrictType(name, is, validate, encode, props) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = "StrictType";
return _this;
}
return StrictType;
})(Type);
function fromRefinement(name, is) {
return new Type(
name,
is,
function (u, c) {
return is(u) ? success(u) : failure(u, c);
},
identity,
);
}
var isDate = function (u) {
return u instanceof Date;
};
var date = fromRefinement("Date", isDate);
const stringWithLength = (min, max) =>
refinement(
string,
(s) => s.length >= min && s.length <= max,
`string with length between ${min} and ${max}`,
);
const numberInRange = (min, max) =>
refinement(number, (n) => n >= min && n <= max, `number between ${min} and ${max}`);
const urlString = refinement(string, (s) => URL.canParse(s), "url");
const literals = ([l1, l2, ...literals]) =>
union([literal(l1), literal(l2), ...literals.map((l) => literal(l))]);
const ImageData = type({
id: number,
created: date,
title: stringWithLength(1, 100),
type: literals(["jpg", "png"]),
size: number,
url: urlString,
});
const RatingData = type({
id: number,
stars: numberInRange(1, 5),
title: stringWithLength(1, 100),
text: stringWithLength(1, 1e3),
images: array(ImageData),
});
type(
{
id: number,
created: date,
title: stringWithLength(1, 100),
brand: stringWithLength(1, 30),
description: stringWithLength(1, 500),
price: numberInRange(1, 1e4),
discount: union([numberInRange(1, 100), nullType]),
quantity: numberInRange(0, 10),
tags: array(stringWithLength(1, 30)),
images: array(ImageData),
ratings: array(RatingData),
},
"ProductData",
).decode({});