decoders 2.12.2 → 2.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +36 -15
- package/dist/index.d.cts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +35 -14
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/lib/utils.ts
|
|
2
|
+
var MAX_ERRORS = 3;
|
|
2
3
|
// @__NO_SIDE_EFFECTS__
|
|
3
4
|
function qty(n, unit) {
|
|
4
5
|
return n === 1 ? `${n} ${unit}` : `${n} ${unit}s`;
|
|
@@ -220,8 +221,12 @@ function serializeArray(annotation, prefix) {
|
|
|
220
221
|
return "[]";
|
|
221
222
|
}
|
|
222
223
|
const result = [];
|
|
223
|
-
for (
|
|
224
|
-
const [ser, ann] = serializeAnnotation(
|
|
224
|
+
for (let i = 0; i < items.length; ++i) {
|
|
225
|
+
const [ser, ann] = serializeAnnotation(
|
|
226
|
+
items[i],
|
|
227
|
+
`${prefix}${INDENT}`,
|
|
228
|
+
`(at index ${i})`
|
|
229
|
+
);
|
|
225
230
|
result.push(`${prefix}${INDENT}${ser},`);
|
|
226
231
|
if (ann !== void 0) {
|
|
227
232
|
result.push(indent(ann, `${prefix}${INDENT}`));
|
|
@@ -267,7 +272,7 @@ function serializeValue(value) {
|
|
|
267
272
|
}
|
|
268
273
|
}
|
|
269
274
|
}
|
|
270
|
-
function serializeAnnotation(ann, prefix = "") {
|
|
275
|
+
function serializeAnnotation(ann, prefix = "", location) {
|
|
271
276
|
let serialized;
|
|
272
277
|
if (ann.type === "array") {
|
|
273
278
|
serialized = serializeArray(ann, prefix);
|
|
@@ -283,7 +288,8 @@ function serializeAnnotation(ann, prefix = "") {
|
|
|
283
288
|
const text = ann.text;
|
|
284
289
|
if (text !== void 0) {
|
|
285
290
|
const sep = "^".repeat(isMultiline(serialized) ? 1 : serialized.length);
|
|
286
|
-
|
|
291
|
+
const annotation = isMultiline(text) ? [location ? `${sep} ${location}` : sep, text].join("\n") : [sep, text, location].filter(Boolean).join(" ");
|
|
292
|
+
return [serialized, annotation];
|
|
287
293
|
} else {
|
|
288
294
|
return [serialized, void 0];
|
|
289
295
|
}
|
|
@@ -623,24 +629,25 @@ function array(decoder, options) {
|
|
|
623
629
|
const base = options !== void 0 ? sized(poja, options) : poja;
|
|
624
630
|
return base.chain((inputs, ok2, err2) => {
|
|
625
631
|
const results = [];
|
|
632
|
+
let clone = null;
|
|
633
|
+
let errorCount = 0;
|
|
626
634
|
for (let i = 0; i < inputs.length; ++i) {
|
|
627
635
|
const blob = inputs[i];
|
|
628
636
|
const result = decodeFn(blob);
|
|
629
637
|
if (result.ok) {
|
|
630
|
-
|
|
638
|
+
if (clone === null) {
|
|
639
|
+
results.push(result.value);
|
|
640
|
+
}
|
|
631
641
|
} else {
|
|
632
642
|
results.length = 0;
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
public_annotate(ann, ann.text ? `${ann.text} (at index ${i})` : `index ${i}`)
|
|
639
|
-
);
|
|
640
|
-
return err2(public_annotate(clone));
|
|
643
|
+
clone ??= inputs.slice();
|
|
644
|
+
clone[i] = result.error;
|
|
645
|
+
if (++errorCount >= MAX_ERRORS) {
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
641
648
|
}
|
|
642
649
|
}
|
|
643
|
-
return ok2(results);
|
|
650
|
+
return clone !== null ? err2(public_annotate(clone)) : ok2(results);
|
|
644
651
|
});
|
|
645
652
|
}
|
|
646
653
|
// @__NO_SIDE_EFFECTS__
|
|
@@ -939,6 +946,9 @@ function record(fst, snd) {
|
|
|
939
946
|
errors.set(key, result.error);
|
|
940
947
|
rv = {};
|
|
941
948
|
}
|
|
949
|
+
if (errors.size >= MAX_ERRORS) {
|
|
950
|
+
break;
|
|
951
|
+
}
|
|
942
952
|
}
|
|
943
953
|
if (errors.size > 0) {
|
|
944
954
|
return err2(merge(public_annotateObject(input), errors));
|
|
@@ -1016,6 +1026,16 @@ var uuidv4 = (
|
|
|
1016
1026
|
// https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
|
|
1017
1027
|
/* @__PURE__ */ uuid.refine((value) => value[14] === "4", "Must be uuidv4")
|
|
1018
1028
|
);
|
|
1029
|
+
function isKsuid(value) {
|
|
1030
|
+
return /^[0-9a-z]{27}$/i.test(value) && // Base62 alphabet sorts like ASCII, so the 2^160 - 1 upper bound is a
|
|
1031
|
+
// string comparison
|
|
1032
|
+
value <= "aWgEPTl1tmebfsQzFP4bxwgy80V";
|
|
1033
|
+
}
|
|
1034
|
+
// @__NO_SIDE_EFFECTS__
|
|
1035
|
+
function ksuid(options) {
|
|
1036
|
+
const prefix = _optionalChain([options, 'optionalAccess', _8 => _8.prefix]);
|
|
1037
|
+
return prefix ? (/* @__PURE__ */ startsWith(prefix)).refine((s) => isKsuid(s.slice(prefix.length)), "Must be ksuid") : string.refine(isKsuid, "Must be ksuid");
|
|
1038
|
+
}
|
|
1019
1039
|
var decimal = /* @__PURE__ */ regex(/^[0-9]+$/, "Must only contain digits");
|
|
1020
1040
|
var hexadecimal = /* @__PURE__ */ regex(
|
|
1021
1041
|
/^[0-9a-f]+$/i,
|
|
@@ -1183,6 +1203,7 @@ var json = /* @__PURE__ */ either(
|
|
|
1183
1203
|
|
|
1184
1204
|
|
|
1185
1205
|
|
|
1186
|
-
|
|
1206
|
+
|
|
1207
|
+
exports._annotate = public_annotate; exports.always = always; exports.anyNumber = anyNumber; exports.anything = anything; exports.array = array; exports.between = between; exports.bigint = bigint; exports.boolean = boolean; exports.constant = constant; exports.date = date; exports.dateString = dateString; exports.datelike = datelike; exports.decimal = decimal; exports.define = define; exports.either = either; exports.email = email; exports.endsWith = endsWith; exports.enum_ = enum_; exports.err = err; exports.exact = exact; exports.fail = fail; exports.flexDate = flexDate; exports.formatInline = formatInline; exports.formatShort = formatShort; exports.hexadecimal = hexadecimal; exports.httpsUrl = httpsUrl; exports.identifier = identifier; exports.inexact = inexact; exports.instanceOf = instanceOf; exports.integer = integer; exports.isDate = isDate; exports.isDecoder = isDecoder; exports.isPlainObject = isPlainObject; exports.isPromiseLike = isPromiseLike; exports.iso8601 = iso8601; exports.isoDate = isoDate; exports.isoDateString = isoDateString; exports.json = json; exports.jsonArray = jsonArray; exports.jsonObject = jsonObject; exports.ksuid = ksuid; exports.lazy = lazy; exports.mapping = mapping; exports.max = max; exports.min = min; exports.nanoid = nanoid; exports.natural = natural; exports.never = never; exports.nonEmptyArray = nonEmptyArray; exports.nonEmptyString = nonEmptyString; exports.nonNegativeNumber = nonNegativeNumber; exports.null_ = null_; exports.nullable = nullable; exports.nullish = nullish; exports.number = number; exports.numeric = numeric; exports.object = object; exports.ok = ok; exports.oneOf = oneOf; exports.optional = optional; exports.poja = poja; exports.pojo = pojo; exports.positiveInteger = positiveInteger; exports.positiveNumber = positiveNumber; exports.prep = prep; exports.record = record; exports.regex = regex; exports.select = select; exports.setFromArray = setFromArray; exports.sized = sized; exports.startsWith = startsWith; exports.string = string; exports.taggedUnion = taggedUnion; exports.truthy = truthy; exports.tuple = tuple; exports.undefined_ = undefined_; exports.unknown = unknown; exports.url = url; exports.urlString = urlString; exports.uuid = uuid; exports.uuidv1 = uuidv1; exports.uuidv4 = uuidv4;
|
|
1187
1208
|
// istanbul ignore next -- @preserve
|
|
1188
1209
|
// istanbul ignore else -- @preserve
|
package/dist/index.d.cts
CHANGED
|
@@ -645,6 +645,16 @@ declare const uuidv1: Decoder<string>;
|
|
|
645
645
|
* strings.
|
|
646
646
|
*/
|
|
647
647
|
declare const uuidv4: Decoder<string>;
|
|
648
|
+
/**
|
|
649
|
+
* Accepts and returns [KSUID](https://github.com/segmentio/ksuid) strings
|
|
650
|
+
* (K-Sortable Unique IDentifier): 27 base62 chars, encoding a 160-bit value.
|
|
651
|
+
* Optionally, specify a prefix the KSUID must be preceded by, e.g.
|
|
652
|
+
* `ksuid({ prefix: 'msg_' })` accepts `msg_0ujtsYcgvSTl8PAuAdqWYSMnLOv`.
|
|
653
|
+
*/
|
|
654
|
+
declare function ksuid(): Decoder<string>;
|
|
655
|
+
declare function ksuid<P extends string>(options: {
|
|
656
|
+
prefix: P;
|
|
657
|
+
}): Decoder<`${P}${string}`>;
|
|
648
658
|
/**
|
|
649
659
|
* Accepts and returns strings with decimal digits only (base-10).
|
|
650
660
|
* To convert these to numbers, use the `numeric` decoder.
|
|
@@ -733,4 +743,4 @@ declare function isPromiseLike(value: unknown): value is PromiseLike<unknown>;
|
|
|
733
743
|
*/
|
|
734
744
|
declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
735
745
|
|
|
736
|
-
export { type Annotation, type ArrayAnnotation, type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type ObjectAnnotation, type Ok, type OpaqueAnnotation, type Relax, type Result, type Scalar, type ScalarAnnotation, type SizeOptions, type Sized, public_annotate as _annotate, always, anyNumber, anything, array, between, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, flexDate, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, isoDate, isoDateString, json, jsonArray, jsonObject, lazy, mapping, max, min, nanoid, natural, never, nonEmptyArray, nonEmptyString, nonNegativeNumber, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, sized, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, urlString, uuid, uuidv1, uuidv4 };
|
|
746
|
+
export { type Annotation, type ArrayAnnotation, type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type ObjectAnnotation, type Ok, type OpaqueAnnotation, type Relax, type Result, type Scalar, type ScalarAnnotation, type SizeOptions, type Sized, public_annotate as _annotate, always, anyNumber, anything, array, between, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, flexDate, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, isoDate, isoDateString, json, jsonArray, jsonObject, ksuid, lazy, mapping, max, min, nanoid, natural, never, nonEmptyArray, nonEmptyString, nonNegativeNumber, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, sized, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, urlString, uuid, uuidv1, uuidv4 };
|
package/dist/index.d.ts
CHANGED
|
@@ -645,6 +645,16 @@ declare const uuidv1: Decoder<string>;
|
|
|
645
645
|
* strings.
|
|
646
646
|
*/
|
|
647
647
|
declare const uuidv4: Decoder<string>;
|
|
648
|
+
/**
|
|
649
|
+
* Accepts and returns [KSUID](https://github.com/segmentio/ksuid) strings
|
|
650
|
+
* (K-Sortable Unique IDentifier): 27 base62 chars, encoding a 160-bit value.
|
|
651
|
+
* Optionally, specify a prefix the KSUID must be preceded by, e.g.
|
|
652
|
+
* `ksuid({ prefix: 'msg_' })` accepts `msg_0ujtsYcgvSTl8PAuAdqWYSMnLOv`.
|
|
653
|
+
*/
|
|
654
|
+
declare function ksuid(): Decoder<string>;
|
|
655
|
+
declare function ksuid<P extends string>(options: {
|
|
656
|
+
prefix: P;
|
|
657
|
+
}): Decoder<`${P}${string}`>;
|
|
648
658
|
/**
|
|
649
659
|
* Accepts and returns strings with decimal digits only (base-10).
|
|
650
660
|
* To convert these to numbers, use the `numeric` decoder.
|
|
@@ -733,4 +743,4 @@ declare function isPromiseLike(value: unknown): value is PromiseLike<unknown>;
|
|
|
733
743
|
*/
|
|
734
744
|
declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
735
745
|
|
|
736
|
-
export { type Annotation, type ArrayAnnotation, type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type ObjectAnnotation, type Ok, type OpaqueAnnotation, type Relax, type Result, type Scalar, type ScalarAnnotation, type SizeOptions, type Sized, public_annotate as _annotate, always, anyNumber, anything, array, between, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, flexDate, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, isoDate, isoDateString, json, jsonArray, jsonObject, lazy, mapping, max, min, nanoid, natural, never, nonEmptyArray, nonEmptyString, nonNegativeNumber, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, sized, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, urlString, uuid, uuidv1, uuidv4 };
|
|
746
|
+
export { type Annotation, type ArrayAnnotation, type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type ObjectAnnotation, type Ok, type OpaqueAnnotation, type Relax, type Result, type Scalar, type ScalarAnnotation, type SizeOptions, type Sized, public_annotate as _annotate, always, anyNumber, anything, array, between, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, flexDate, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, isoDate, isoDateString, json, jsonArray, jsonObject, ksuid, lazy, mapping, max, min, nanoid, natural, never, nonEmptyArray, nonEmptyString, nonNegativeNumber, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, sized, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, urlString, uuid, uuidv1, uuidv4 };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/lib/utils.ts
|
|
2
|
+
var MAX_ERRORS = 3;
|
|
2
3
|
// @__NO_SIDE_EFFECTS__
|
|
3
4
|
function qty(n, unit) {
|
|
4
5
|
return n === 1 ? `${n} ${unit}` : `${n} ${unit}s`;
|
|
@@ -220,8 +221,12 @@ function serializeArray(annotation, prefix) {
|
|
|
220
221
|
return "[]";
|
|
221
222
|
}
|
|
222
223
|
const result = [];
|
|
223
|
-
for (
|
|
224
|
-
const [ser, ann] = serializeAnnotation(
|
|
224
|
+
for (let i = 0; i < items.length; ++i) {
|
|
225
|
+
const [ser, ann] = serializeAnnotation(
|
|
226
|
+
items[i],
|
|
227
|
+
`${prefix}${INDENT}`,
|
|
228
|
+
`(at index ${i})`
|
|
229
|
+
);
|
|
225
230
|
result.push(`${prefix}${INDENT}${ser},`);
|
|
226
231
|
if (ann !== void 0) {
|
|
227
232
|
result.push(indent(ann, `${prefix}${INDENT}`));
|
|
@@ -267,7 +272,7 @@ function serializeValue(value) {
|
|
|
267
272
|
}
|
|
268
273
|
}
|
|
269
274
|
}
|
|
270
|
-
function serializeAnnotation(ann, prefix = "") {
|
|
275
|
+
function serializeAnnotation(ann, prefix = "", location) {
|
|
271
276
|
let serialized;
|
|
272
277
|
if (ann.type === "array") {
|
|
273
278
|
serialized = serializeArray(ann, prefix);
|
|
@@ -283,7 +288,8 @@ function serializeAnnotation(ann, prefix = "") {
|
|
|
283
288
|
const text = ann.text;
|
|
284
289
|
if (text !== void 0) {
|
|
285
290
|
const sep = "^".repeat(isMultiline(serialized) ? 1 : serialized.length);
|
|
286
|
-
|
|
291
|
+
const annotation = isMultiline(text) ? [location ? `${sep} ${location}` : sep, text].join("\n") : [sep, text, location].filter(Boolean).join(" ");
|
|
292
|
+
return [serialized, annotation];
|
|
287
293
|
} else {
|
|
288
294
|
return [serialized, void 0];
|
|
289
295
|
}
|
|
@@ -623,24 +629,25 @@ function array(decoder, options) {
|
|
|
623
629
|
const base = options !== void 0 ? sized(poja, options) : poja;
|
|
624
630
|
return base.chain((inputs, ok2, err2) => {
|
|
625
631
|
const results = [];
|
|
632
|
+
let clone = null;
|
|
633
|
+
let errorCount = 0;
|
|
626
634
|
for (let i = 0; i < inputs.length; ++i) {
|
|
627
635
|
const blob = inputs[i];
|
|
628
636
|
const result = decodeFn(blob);
|
|
629
637
|
if (result.ok) {
|
|
630
|
-
|
|
638
|
+
if (clone === null) {
|
|
639
|
+
results.push(result.value);
|
|
640
|
+
}
|
|
631
641
|
} else {
|
|
632
642
|
results.length = 0;
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
public_annotate(ann, ann.text ? `${ann.text} (at index ${i})` : `index ${i}`)
|
|
639
|
-
);
|
|
640
|
-
return err2(public_annotate(clone));
|
|
643
|
+
clone ??= inputs.slice();
|
|
644
|
+
clone[i] = result.error;
|
|
645
|
+
if (++errorCount >= MAX_ERRORS) {
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
641
648
|
}
|
|
642
649
|
}
|
|
643
|
-
return ok2(results);
|
|
650
|
+
return clone !== null ? err2(public_annotate(clone)) : ok2(results);
|
|
644
651
|
});
|
|
645
652
|
}
|
|
646
653
|
// @__NO_SIDE_EFFECTS__
|
|
@@ -939,6 +946,9 @@ function record(fst, snd) {
|
|
|
939
946
|
errors.set(key, result.error);
|
|
940
947
|
rv = {};
|
|
941
948
|
}
|
|
949
|
+
if (errors.size >= MAX_ERRORS) {
|
|
950
|
+
break;
|
|
951
|
+
}
|
|
942
952
|
}
|
|
943
953
|
if (errors.size > 0) {
|
|
944
954
|
return err2(merge(public_annotateObject(input), errors));
|
|
@@ -1016,6 +1026,16 @@ var uuidv4 = (
|
|
|
1016
1026
|
// https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
|
|
1017
1027
|
/* @__PURE__ */ uuid.refine((value) => value[14] === "4", "Must be uuidv4")
|
|
1018
1028
|
);
|
|
1029
|
+
function isKsuid(value) {
|
|
1030
|
+
return /^[0-9a-z]{27}$/i.test(value) && // Base62 alphabet sorts like ASCII, so the 2^160 - 1 upper bound is a
|
|
1031
|
+
// string comparison
|
|
1032
|
+
value <= "aWgEPTl1tmebfsQzFP4bxwgy80V";
|
|
1033
|
+
}
|
|
1034
|
+
// @__NO_SIDE_EFFECTS__
|
|
1035
|
+
function ksuid(options) {
|
|
1036
|
+
const prefix = options?.prefix;
|
|
1037
|
+
return prefix ? (/* @__PURE__ */ startsWith(prefix)).refine((s) => isKsuid(s.slice(prefix.length)), "Must be ksuid") : string.refine(isKsuid, "Must be ksuid");
|
|
1038
|
+
}
|
|
1019
1039
|
var decimal = /* @__PURE__ */ regex(/^[0-9]+$/, "Must only contain digits");
|
|
1020
1040
|
var hexadecimal = /* @__PURE__ */ regex(
|
|
1021
1041
|
/^[0-9a-f]+$/i,
|
|
@@ -1142,6 +1162,7 @@ export {
|
|
|
1142
1162
|
json,
|
|
1143
1163
|
jsonArray,
|
|
1144
1164
|
jsonObject,
|
|
1165
|
+
ksuid,
|
|
1145
1166
|
lazy,
|
|
1146
1167
|
mapping,
|
|
1147
1168
|
max,
|