decoders 2.1.0 → 2.2.0-test

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.
Files changed (99) hide show
  1. package/README.md +12 -12
  2. package/dist/annotate-0PUmWHxH.d.cts +33 -0
  3. package/dist/annotate-0PUmWHxH.d.ts +33 -0
  4. package/dist/chunk-2C72BP5L.cjs +179 -0
  5. package/dist/chunk-2C72BP5L.cjs.map +1 -0
  6. package/dist/chunk-BPSZE2VX.js +13 -0
  7. package/dist/chunk-BPSZE2VX.js.map +1 -0
  8. package/dist/chunk-Q3YXBCTD.cjs +13 -0
  9. package/dist/chunk-Q3YXBCTD.cjs.map +1 -0
  10. package/dist/chunk-RUMDX66L.js +179 -0
  11. package/dist/chunk-RUMDX66L.js.map +1 -0
  12. package/dist/format.cjs +13 -0
  13. package/dist/format.cjs.map +1 -0
  14. package/dist/format.d.cts +9 -0
  15. package/dist/format.d.ts +9 -0
  16. package/dist/format.js +13 -0
  17. package/dist/format.js.map +1 -0
  18. package/dist/index.cjs +659 -0
  19. package/dist/index.cjs.map +1 -0
  20. package/dist/index.d.cts +454 -0
  21. package/dist/index.d.ts +454 -0
  22. package/dist/index.js +659 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/result.cjs +9 -0
  25. package/dist/result.cjs.map +1 -0
  26. package/dist/result.d.cts +26 -0
  27. package/dist/result.d.ts +26 -0
  28. package/dist/result.js +9 -0
  29. package/dist/result.js.map +1 -0
  30. package/format.cjs +13 -0
  31. package/package.json +163 -100
  32. package/result.cjs +9 -0
  33. package/CHANGELOG.md +0 -670
  34. package/Decoder.d.ts +0 -94
  35. package/Decoder.js +0 -105
  36. package/Decoder.js.flow +0 -286
  37. package/Decoder.mjs +0 -101
  38. package/NotSupportedTSVersion.d.ts +0 -1
  39. package/_utils.d.ts +0 -9
  40. package/_utils.js +0 -80
  41. package/_utils.js.flow +0 -107
  42. package/_utils.mjs +0 -70
  43. package/annotate.d.ts +0 -62
  44. package/annotate.js +0 -145
  45. package/annotate.js.flow +0 -218
  46. package/annotate.mjs +0 -131
  47. package/format.d.ts +0 -6
  48. package/format.js +0 -117
  49. package/format.js.flow +0 -128
  50. package/format.mjs +0 -110
  51. package/index.d.ts +0 -40
  52. package/index.js +0 -67
  53. package/index.js.flow +0 -44
  54. package/index.mjs +0 -11
  55. package/lib/_helpers.d.ts +0 -79
  56. package/lib/arrays.d.ts +0 -59
  57. package/lib/arrays.js +0 -85
  58. package/lib/arrays.js.flow +0 -138
  59. package/lib/arrays.mjs +0 -75
  60. package/lib/basics.d.ts +0 -93
  61. package/lib/basics.js +0 -74
  62. package/lib/basics.js.flow +0 -124
  63. package/lib/basics.mjs +0 -60
  64. package/lib/booleans.d.ts +0 -16
  65. package/lib/booleans.js +0 -21
  66. package/lib/booleans.js.flow +0 -22
  67. package/lib/booleans.mjs +0 -15
  68. package/lib/dates.d.ts +0 -15
  69. package/lib/dates.js +0 -23
  70. package/lib/dates.js.flow +0 -40
  71. package/lib/dates.mjs +0 -17
  72. package/lib/json.d.ts +0 -35
  73. package/lib/json.js +0 -25
  74. package/lib/json.js.flow +0 -50
  75. package/lib/json.mjs +0 -18
  76. package/lib/numbers.d.ts +0 -31
  77. package/lib/numbers.js +0 -34
  78. package/lib/numbers.js.flow +0 -46
  79. package/lib/numbers.mjs +0 -25
  80. package/lib/objects.d.ts +0 -76
  81. package/lib/objects.js +0 -138
  82. package/lib/objects.js.flow +0 -238
  83. package/lib/objects.mjs +0 -128
  84. package/lib/strings.d.ts +0 -54
  85. package/lib/strings.js +0 -54
  86. package/lib/strings.js.flow +0 -90
  87. package/lib/strings.mjs +0 -40
  88. package/lib/unions.d.ts +0 -55
  89. package/lib/unions.js +0 -82
  90. package/lib/unions.js.flow +0 -155
  91. package/lib/unions.mjs +0 -75
  92. package/lib/utilities.d.ts +0 -40
  93. package/lib/utilities.js +0 -44
  94. package/lib/utilities.js.flow +0 -65
  95. package/lib/utilities.mjs +0 -35
  96. package/result.d.ts +0 -16
  97. package/result.js +0 -21
  98. package/result.js.flow +0 -26
  99. package/result.mjs +0 -15
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/annotate.ts","../src/Decoder.ts","../src/lib/objects.ts","../src/lib/utilities.ts","../src/lib/unions.ts","../src/lib/basics.ts","../src/lib/arrays.ts","../src/lib/numbers.ts","../src/lib/booleans.ts","../src/lib/strings.ts","../src/lib/dates.ts","../src/lib/json.ts"],"names":["err","value","ok","object","mapping","unknown","array","regex","date"],"mappings":";;;;;;;;;;;;;;;AAEA,IAAM,YAAiC,oBAAI,QAAQ;AA4CnD,SAAS,MAA4B,KAAW;AAC9C,YAAU,IAAI,GAAG;AACjB,SAAO;AACT;AAEO,SAAS,OACd,QACA,MACkB;AAClB,SAAO,MAAM,EAAE,MAAM,UAAU,QAAQ,KAAK,CAAC;AAC/C;AAEO,SAAS,MAAM,OAA8B,MAAgC;AAClF,SAAO,MAAM;AAAA,IACX,MAAM;AAAA,IACN;AAAA,IACA;AAAA,EACF,CAAC;AACH;AAEO,SAAS,KAAK,MAAmC;AACtD,SAAO,MAAM;AAAA,IACX,MAAM;AAAA,IACN;AAAA,EACF,CAAC;AACH;AAEO,SAAS,QAAQ,OAAgB,MAAkC;AACxE,SAAO,MAAM;AAAA,IACX,MAAM;AAAA,IACN;AAAA,IACA;AAAA,EACF,CAAC;AACH;AAEO,SAAS,OAAO,OAAgB,MAAiC;AACtE,SAAO,MAAM;AAAA,IACX,MAAM;AAAA,IACN;AAAA,IACA;AAAA,EACF,CAAC;AACH;AAEO,SAAS,YAAY,MAAsC;AAChE,SAAO,MAAM;AAAA,IACX,MAAM;AAAA,IACN;AAAA,EACF,CAAC;AACH;AAKO,SAAS,WAAiC,YAAe,MAAkB;AAChF,MAAI,SAAS,QAAW;AACtB,WAAO,MAAM,EAAE,GAAG,YAAY,KAAK,CAAC;AAAA,EACtC,OAAO;AACL,WAAO;AAAA,EACT;AACF;AAKO,SAAS,MACd,eACA,QACkB;AAClB,QAA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CAAC,UAAU,MAAM,EAAE,MAAM,KAAK,gBAAgB;AAAA;AAOrD,IAAM;AAAA;AAAA,EAEX,KAAK,OAAO,CAAC,UAAU,MAAM,EAAE,MAAM,KAAK,gBAAgB;AAAA;;;AC/E5D,IAAM,aACJ;AAKK,IAAM,OAAsB,OAAO,CAAC,MAAML,KAAIF,SAAQ;AAC3D,QAAMQ,QAAO,OAAO,IAAI;AACxB,SAAOA,UAAS,OAAON,IAAGM,KAAI,IAAIR,KAAI,gBAAgB;AACxD,CAAC;AASM,IAAM;AAAA;AAAA,EAEX,MAAM,YAAY,wBAAwB,EAAE;AAAA;AAAA,IAE1C,CAAC,UAAkB;AACjB,YAAMQ,QAAO,IAAI,KAAK,KAAK;AAC3B,UAAI,MAAMA,MAAK,QAAQ,CAAC,GAAG;AACzB,cAAM,IAAI,MAAM,+BAA+B;AAAA,MACjD;AACA,aAAOA;AAAA,IACT;AAAA,EACF;AAAA;;;ACpBK,IAAM,aAAkC,KAAK,MAAM,KAAK,IAAI,CAAC;AAK7D,IAAM,YAAgC,KAAK,MAAMF,OAAM,IAAI,CAAC;AAkB5D,IAAM,OAA2B;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,EAAE,SAAS,0BAA0B","sourcesContent":["import { isPojo } from './_utils';\n\nconst _register: WeakSet<Annotation> = new WeakSet();\n\nexport interface ObjectAnnotation {\n readonly type: 'object';\n readonly fields: { readonly [key: string]: Annotation };\n readonly text?: string;\n}\n\nexport interface ArrayAnnotation {\n readonly type: 'array';\n readonly items: readonly Annotation[];\n readonly text?: string;\n}\n\nexport interface ScalarAnnotation {\n readonly type: 'scalar';\n readonly value: unknown;\n readonly text?: string;\n}\n\nexport interface FunctionAnnotation {\n readonly type: 'function';\n readonly text?: string;\n}\n\nexport interface CircularRefAnnotation {\n readonly type: 'circular-ref';\n readonly text?: string;\n}\n\nexport interface UnknownAnnotation {\n readonly type: 'unknown';\n readonly value: unknown;\n readonly text?: string;\n}\n\nexport type Annotation =\n | ObjectAnnotation\n | ArrayAnnotation\n | ScalarAnnotation\n | FunctionAnnotation\n | CircularRefAnnotation\n | UnknownAnnotation;\n\nfunction brand<A extends Annotation>(ann: A): A {\n _register.add(ann);\n return ann;\n}\n\nexport function object(\n fields: { readonly [key: string]: Annotation },\n text?: string,\n): ObjectAnnotation {\n return brand({ type: 'object', fields, text });\n}\n\nexport function array(items: readonly Annotation[], text?: string): ArrayAnnotation {\n return brand({\n type: 'array',\n items,\n text,\n });\n}\n\nexport function func(text?: string): FunctionAnnotation {\n return brand({\n type: 'function',\n text,\n });\n}\n\nexport function unknown(value: unknown, text?: string): UnknownAnnotation {\n return brand({\n type: 'unknown',\n value,\n text,\n });\n}\n\nexport function scalar(value: unknown, text?: string): ScalarAnnotation {\n return brand({\n type: 'scalar',\n value,\n text,\n });\n}\n\nexport function circularRef(text?: string): CircularRefAnnotation {\n return brand({\n type: 'circular-ref',\n text,\n });\n}\n\n/**\n * Given an existing Annotation, set the annotation's text to a new value.\n */\nexport function updateText<A extends Annotation>(annotation: A, text?: string): A {\n if (text !== undefined) {\n return brand({ ...annotation, text });\n } else {\n return annotation;\n }\n}\n\n/**\n * Given an existing ObjectAnnotation, merges new Annotations in there.\n */\nexport function merge(\n objAnnotation: ObjectAnnotation,\n fields: { readonly [key: string]: Annotation },\n): ObjectAnnotation {\n const newFields = { ...objAnnotation.fields, ...fields };\n return object(newFields, objAnnotation.text);\n}\n\nexport function asAnnotation(thing: unknown): Annotation | undefined {\n return typeof thing === 'object' && thing !== null && _register.has(thing as Annotation)\n ? (thing as Annotation)\n : undefined;\n}\n\ntype RefSet = WeakSet<object>;\n\n/** @internal */\nfunction annotateArray(\n value: readonly unknown[],\n text: string | undefined,\n seen: RefSet,\n): ArrayAnnotation | CircularRefAnnotation {\n seen.add(value);\n\n const items = value.map((v) => annotate(v, undefined, seen));\n return array(items, text);\n}\n\n/** @internal */\nfunction annotateObject(\n obj: Record<string, unknown>,\n text: string | undefined,\n seen: RefSet,\n): ObjectAnnotation {\n seen.add(obj);\n\n const fields: Record<string, Annotation> = {};\n for (const key of Object.keys(obj)) {\n const value = obj[key];\n fields[key] = annotate(value, undefined, seen);\n }\n return object(fields, text);\n}\n\n/** @internal */\nfunction annotate(value: unknown, text: string | undefined, seen: RefSet): Annotation {\n if (\n value === null ||\n value === undefined ||\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n typeof value === 'symbol' ||\n typeof (value as Record<string, unknown>).getMonth === 'function'\n ) {\n return scalar(value, text);\n }\n\n const ann = asAnnotation(value);\n if (ann) {\n return updateText(ann, text);\n }\n\n if (Array.isArray(value)) {\n // \"Circular references\" can only exist in objects or arrays\n if (seen.has(value)) {\n return circularRef(text);\n } else {\n return annotateArray(value, text, seen);\n }\n }\n\n if (isPojo(value)) {\n // \"Circular references\" can only exist in objects or arrays\n if (seen.has(value)) {\n return circularRef(text);\n } else {\n return annotateObject(value, text, seen);\n }\n }\n\n if (typeof value === 'function') {\n return func(text);\n }\n\n return unknown(value, text);\n}\n\nfunction public_annotate(value: unknown, text?: string): Annotation {\n return annotate(value, text, new WeakSet());\n}\n\nfunction public_annotateObject(\n obj: { readonly [field: string]: unknown },\n text?: string,\n): ObjectAnnotation {\n return annotateObject(obj, text, new WeakSet());\n}\n\nexport {\n // This construct just ensures the \"seen\" weakmap (used for circular\n // reference detection) isn't made part of the public API.\n public_annotate as annotate,\n public_annotateObject as annotateObject,\n\n /** @internal */\n annotate as __private_annotate,\n};\n","import { annotate } from './annotate';\nimport { formatInline } from './format';\nimport { err as makeErr, ok as makeOk } from './result';\nimport type { Annotation } from './annotate';\nimport type { Formatter } from './format';\nimport type { Result } from './result';\n\nexport type Scalar = string | number | boolean | symbol | undefined | null;\n\nexport type DecodeResult<T> = Result<T, Annotation>;\n\nexport type AcceptanceFn<T, InputT = unknown> = (\n blob: InputT,\n ok: (value: T) => DecodeResult<T>,\n err: (msg: string | Annotation) => DecodeResult<T>,\n) => DecodeResult<T>;\n\nexport type Decoder<T> = {\n /**\n * Verifies untrusted input. Either returns a value, or throws a decoding\n * error.\n */\n verify(blob: unknown, formatterFn?: (ann: Annotation) => string | Error): T;\n\n /**\n * Verifies untrusted input. Either returns a value, or returns undefined.\n */\n value(blob: unknown): T | undefined;\n\n /**\n * Verifies untrusted input. Always returns a DecodeResult, which is either\n * an \"ok\" value or an \"error\" annotation.\n */\n decode(blob: unknown): DecodeResult<T>;\n\n /**\n * Build a new decoder from the the current one, with an extra acceptance\n * criterium.\n */\n refine<N extends T>(predicate: (value: T) => value is N, msg: string): Decoder<N>;\n refine(predicate: (value: T) => boolean, msg: string): Decoder<T>;\n\n /**\n * Build a new decoder from the current one, with an extra rejection\n * criterium.\n */\n reject(rejectFn: (value: T) => string | Annotation | null): Decoder<T>;\n\n /**\n * Build a new decoder from the current one, modifying its outputted value.\n */\n transform<V>(transformFn: (value: T) => V): Decoder<V>;\n\n /**\n * Build a new decoder from the current one, with a mutated error message\n * in case of a rejection.\n */\n describe(message: string): Decoder<T>;\n\n /**\n * Chain together the current decoder with another acceptance function.\n */\n then<V>(next: AcceptanceFn<V, T>): Decoder<V>;\n\n // Experimental APIs (please don't rely on these yet)\n peek_UNSTABLE<V>(next: AcceptanceFn<V, [unknown, T]>): Decoder<V>;\n};\n\n/**\n * Helper type to return the \"type\" of a Decoder.\n *\n * You can use it on types:\n *\n * DecoderType<Decoder<string>> // string\n * DecoderType<Decoder<number[]>> // number[]\n *\n * Or on \"values\", by using the `typeof` keyword:\n *\n * DecoderType<typeof string> // string\n * DecoderType<typeof truthy> // boolean\n *\n */\nexport type DecoderType<D extends Decoder<any>> = D extends Decoder<infer T> ? T : never;\n\nfunction noThrow<T, V>(fn: (value: T) => V): (blob: T) => DecodeResult<V> {\n return (t) => {\n try {\n const v = fn(t);\n return makeOk(v);\n } catch (e) {\n return makeErr(annotate(t, e instanceof Error ? e.message : String(e)));\n }\n };\n}\n\nfunction format(err: Annotation, formatter: Formatter): Error {\n const formatted = formatter(err);\n\n // Formatter functions may return a string or an error for convenience of\n // writing them. If it already returns an Error, return it unmodified. If\n // it returns a string, wrap it in a \"Decoding error\" instance.\n if (typeof formatted === 'string') {\n const err = new Error(`\\n${formatted}`);\n err.name = 'Decoding error';\n return err;\n } else {\n return formatted;\n }\n}\n\n/**\n * Defines a new `Decoder<T>`, by implementing a custom acceptance function.\n * The function receives three arguments:\n *\n * 1. `blob` - the raw/unknown input (aka your external data)\n * 2. `ok` - Call `ok(value)` to accept the input and return ``value``\n * 3. `err` - Call `err(message)` to reject the input with error ``message``\n *\n * The expected return value should be a `DecodeResult<T>`, which can be\n * obtained by returning the result of calling the provided `ok` or `err`\n * helper functions. Please note that `ok()` and `err()` don't perform side\n * effects! You'll need to _return_ those values.\n */\nexport function define<T>(fn: AcceptanceFn<T>): Decoder<T> {\n /**\n * Verifies the untrusted/unknown input and either accepts or rejects it.\n *\n * Contrasted with `.verify()`, calls to `.decode()` will never fail and\n * instead return a result type.\n */\n function decode(blob: unknown): DecodeResult<T> {\n return fn(blob, makeOk, (msg: Annotation | string) =>\n makeErr(typeof msg === 'string' ? annotate(blob, msg) : msg),\n );\n }\n\n /**\n * Verifies the untrusted/unknown input and either accepts or rejects it.\n * When accepted, returns a value of type `T`. Otherwise fail with\n * a runtime error.\n */\n function verify(blob: unknown, formatter: Formatter = formatInline): T {\n const result = decode(blob);\n if (result.ok) {\n return result.value;\n } else {\n throw format(result.error, formatter);\n }\n }\n\n /**\n * Verifies the untrusted/unknown input and either accepts or rejects it.\n * When accepted, returns the decoded `T` value directly. Otherwise returns\n * `undefined`.\n *\n * Use this when you're not interested in programmatically handling the\n * error message.\n */\n function value(blob: unknown): T | undefined {\n return decode(blob).value;\n }\n\n /**\n * Accepts any value the given decoder accepts, and on success, will call\n * the given function **on the decoded result**. If the transformation\n * function throws an error, the whole decoder will fail using the error\n * message as the failure reason.\n */\n function transform<V>(transformFn: (result: T) => V): Decoder<V> {\n return then(noThrow(transformFn));\n }\n\n /**\n * Adds an extra predicate to a decoder. The new decoder is like the\n * original decoder, but only accepts values that also meet the\n * predicate.\n */\n function refine(predicateFn: (value: T) => boolean, errmsg: string): Decoder<T> {\n return reject((value) =>\n predicateFn(value)\n ? // Don't reject\n null\n : // Reject with the given error message\n errmsg,\n );\n }\n\n /**\n * Chain together the current decoder with another.\n *\n * > _**NOTE:** This is an advanced, low-level, API. It's not recommended\n * > to reach for this construct unless there is no other way. Most cases can\n * > be covered more elegantly by `.transform()` or `.refine()` instead._\n *\n * If the current decoder accepts an input, the resulting ``T`` value will\n * get passed into the given ``next`` acceptance function to further decide\n * whether or not the value should get accepted or rejected.\n *\n * This works similar to how you would `define()` a new decoder, except\n * that the ``blob`` param will now be ``T`` (a known type), rather than\n * ``unknown``. This will allow the function to make a stronger assumption\n * about its input and avoid re-refining inputs.\n *\n * If it helps, you can think of `define(...)` as equivalent to\n * `unknown.then(...)`.\n */\n function then<V>(next: AcceptanceFn<V, T>): Decoder<V> {\n return define((blob, ok, err) => {\n const result = decode(blob);\n return result.ok ? next(result.value, ok, err) : result;\n });\n }\n\n /**\n * Adds an extra predicate to a decoder. The new decoder is like the\n * original decoder, but only accepts values that aren't rejected by the\n * given function.\n *\n * The given function can return `null` to accept the decoded value, or\n * return a specific error message to reject.\n *\n * Unlike `.refine()`, you can use this function to return a dynamic error\n * message.\n */\n function reject(rejectFn: (value: T) => string | Annotation | null): Decoder<T> {\n return then((value, ok, err) => {\n const errmsg = rejectFn(value);\n return errmsg === null\n ? ok(value)\n : err(typeof errmsg === 'string' ? annotate(value, errmsg) : errmsg);\n });\n }\n\n /**\n * Uses the given decoder, but will use an alternative error message in\n * case it rejects. This can be used to simplify or shorten otherwise\n * long or low-level/technical errors.\n */\n function describe(message: string): Decoder<T> {\n return define((blob, _, err) => {\n // Decode using the given decoder...\n const result = decode(blob);\n if (result.ok) {\n return result;\n } else {\n // ...but in case of error, annotate this with the custom given\n // message instead\n return err(annotate(result.error, message));\n }\n });\n }\n\n /**\n * WARNING: This is an EXPERIMENTAL API that will likely change in the\n * future. Please DO NOT rely on it.\n *\n * Chain together the current decoder with another, but also pass along\n * the original input.\n *\n * This is like `.then()`, but instead of this function receiving just\n * the decoded result ``T``, it also receives the original input.\n *\n * This is an advanced, low-level, decoder.\n */\n function peek_UNSTABLE<V>(next: AcceptanceFn<V, [unknown, T]>): Decoder<V> {\n return define((blob, ok, err) => {\n const result = decode(blob);\n return result.ok ? next([blob, result.value], ok, err) : result;\n });\n }\n\n return Object.freeze({\n verify,\n value,\n decode,\n transform,\n refine,\n reject,\n describe,\n then,\n\n // EXPERIMENTAL - please DO NOT rely on this method\n peek_UNSTABLE,\n });\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { annotateObject, merge, updateText } from '../annotate';\nimport { define } from '../Decoder';\nimport { subtract, isPojo } from '../_utils';\nimport type { Annotation } from '../annotate';\nimport type { Decoder, DecodeResult } from '../Decoder';\nimport type { UndefinedToOptional } from './_helpers';\n\ntype ObjectDecoderType<T> = UndefinedToOptional<{\n [K in keyof T]: T[K] extends Decoder<infer V> ? V : never;\n}>;\n\n/**\n * Accepts any \"plain old JavaScript object\", but doesn't validate its keys or\n * values further.\n */\nexport const pojo: Decoder<Record<string, unknown>> = define((blob, ok, err) =>\n isPojo(blob) ? ok(blob) : err('Must be an object'),\n);\n\n/**\n * Accepts objects with fields matching the given decoders. Extra fields that\n * exist on the input object are ignored and will not be returned.\n */\nexport function object(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;\nexport function object<DS extends Record<string, Decoder<any>>>(\n decodersByKey: DS,\n): Decoder<ObjectDecoderType<DS>>;\nexport function object<DS extends Record<string, Decoder<any>>>(\n decodersByKey: DS,\n): Decoder<ObjectDecoderType<DS>> {\n // Compute this set at decoder definition time\n const knownKeys = new Set(Object.keys(decodersByKey));\n\n return pojo.then((plainObj, ok, err) => {\n const actualKeys = new Set(Object.keys(plainObj));\n\n // At this point, \"missingKeys\" will also include all fields that may\n // validly be optional. We'll let the underlying decoder decide and\n // remove the key from this missing set if the decoder accepts the\n // value.\n const missingKeys = subtract(knownKeys, actualKeys);\n\n const record = {};\n let errors: Record<string, Annotation> | null = null;\n\n for (const key of Object.keys(decodersByKey)) {\n const decoder = decodersByKey[key];\n const rawValue = plainObj[key];\n const result: DecodeResult<unknown> = decoder.decode(rawValue);\n\n if (result.ok) {\n const value = result.value;\n if (value !== undefined) {\n // @ts-expect-error - look into this later\n record[key] = value;\n }\n\n // If this succeeded, remove the key from the missing keys\n // tracker\n missingKeys.delete(key);\n } else {\n const ann = result.error;\n\n // Keep track of the annotation, but don't return just yet. We\n // want to collect more error information.\n if (rawValue === undefined) {\n // Explicitly add it to the missing set if the value is\n // undefined. This covers explicit undefineds to be\n // treated the same as implicit undefineds (aka missing\n // keys).\n missingKeys.add(key);\n } else {\n if (errors === null) {\n errors = {};\n }\n errors[key] = ann;\n }\n }\n }\n\n // Deal with errors now. There are two classes of errors we want to\n // report. First of all, we want to report any inline errors in this\n // object. Lastly, any fields that are missing should be annotated on\n // the outer object itself.\n if (errors || missingKeys.size > 0) {\n let objAnn = annotateObject(plainObj);\n\n if (errors) {\n objAnn = merge(objAnn, errors);\n }\n\n if (missingKeys.size > 0) {\n const errMsg = Array.from(missingKeys)\n .map((key) => `\"${key}\"`)\n .join(', ');\n const pluralized = missingKeys.size > 1 ? 'keys' : 'key';\n objAnn = updateText(objAnn, `Missing ${pluralized}: ${errMsg}`);\n }\n\n return err(objAnn);\n }\n\n return ok(record as ObjectDecoderType<DS>);\n });\n}\n\n/**\n * Like `object()`, but will reject inputs that contain extra fields that are\n * not specified explicitly.\n */\nexport function exact(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;\nexport function exact<O extends Record<string, Decoder<any>>>(\n decodersByKey: O,\n): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;\nexport function exact<O extends Record<string, Decoder<any>>>(\n decodersByKey: O,\n): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }> {\n // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n // Ditto (see above)\n\n // Compute this set at decoder definition time\n const allowedKeys = new Set(Object.keys(decodersByKey));\n\n // Check the inputted object for any unexpected extra keys\n const checked = pojo.reject((plainObj) => {\n const actualKeys = new Set(Object.keys(plainObj));\n const extraKeys = subtract(actualKeys, allowedKeys);\n return extraKeys.size > 0\n ? `Unexpected extra keys: ${Array.from(extraKeys).join(', ')}`\n : // Don't reject\n null;\n });\n\n // Defer to the \"object\" decoder for doing the real decoding work. Since\n // we made sure there are no superfluous keys in this structure, it's now\n // safe to force-cast it to an $Exact<> type.\n return checked.then(object(decodersByKey).decode);\n}\n\n/**\n * Like `object()`, but will pass through any extra fields on the input object\n * unvalidated that will thus be of `unknown` type statically.\n */\nexport function inexact(\n decodersByKey: Record<any, never>,\n): Decoder<Record<string, unknown>>;\nexport function inexact<O extends Record<string, Decoder<any>>>(\n decodersByKey: O,\n): Decoder<\n { [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] } & Record<string, unknown>\n>;\nexport function inexact<O extends Record<string, Decoder<any>>>(\n decodersByKey: O,\n): Decoder<\n { [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] } & Record<string, unknown>\n> {\n return pojo.then((plainObj) => {\n const allkeys = new Set(Object.keys(plainObj));\n const decoder = object(decodersByKey).transform((safepart) => {\n const safekeys = new Set(Object.keys(decodersByKey));\n\n // To account for hard-coded keys that aren't part of the input\n for (const k of safekeys) allkeys.add(k);\n\n const rv = {} as {\n [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];\n } & Record<string, unknown>;\n for (const k of allkeys) {\n if (safekeys.has(k)) {\n const value = safepart[k];\n if (value !== undefined) {\n // @ts-expect-error - look into this later\n rv[k] = value;\n }\n } else {\n // @ts-expect-error - look into this later\n rv[k] = plainObj[k];\n }\n }\n return rv;\n });\n return decoder.decode(plainObj);\n });\n}\n\n/**\n * Accepts objects where all values match the given decoder, and returns the\n * result as a `Record<string, T>`.\n *\n * The main difference between `object()` and `dict()` is that you'd typically\n * use `object()` if this is a record-like object, where all field names are\n * known and the values are heterogeneous. Whereas with `dict()` the keys are\n * typically dynamic and the values homogeneous, like in a dictionary,\n * a lookup table, or a cache.\n */\nexport function dict<T>(decoder: Decoder<T>): Decoder<Record<string, T>> {\n return pojo.then((plainObj, ok, err) => {\n let rv: Record<string, T> = {};\n let errors: Record<string, Annotation> | null = null;\n\n for (const key of Object.keys(plainObj)) {\n const value = plainObj[key];\n const result = decoder.decode(value);\n if (result.ok) {\n if (errors === null) {\n rv[key] = result.value;\n }\n } else {\n rv = {}; // Clear the success value so it can get garbage collected early\n if (errors === null) {\n errors = {};\n }\n errors[key] = result.error;\n }\n }\n\n if (errors !== null) {\n return err(merge(annotateObject(plainObj), errors));\n } else {\n return ok(rv);\n }\n });\n}\n\n/**\n * Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6\n * Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))\n * instead.\n */\nexport function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {\n return dict(decoder).transform(\n (obj) =>\n new Map(\n // This is effectively Object.entries(obj), but in a way that Flow\n // will know the types are okay\n Object.keys(obj).map((key) => [key, obj[key]]),\n ),\n );\n}\n","import { annotate } from '../annotate';\nimport { define } from '../Decoder';\nimport type { Decoder } from '../Decoder';\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport interface Klass<T> extends Function {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new (...args: readonly any[]): T;\n}\n\nexport type Instance<K> = K extends Klass<infer T> ? T : never;\n\n/**\n * Accepts any value that is an ``instanceof`` the given class.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>> {\n return define((blob, ok, err) =>\n blob instanceof klass ? ok(blob) : err(`Must be ${klass.name} instance`),\n );\n}\n\n/**\n * Lazily evaluate the given decoder. This is useful to build self-referential\n * types for recursive data structures.\n */\nexport function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T> {\n return define((blob) => decoderFn().decode(blob));\n}\n\n/**\n * Pre-process the data input before passing it into the decoder. This gives\n * you the ability to arbitrarily customize the input on the fly before passing\n * it to the decoder. Of course, the input value at that point is still of\n * ``unknown`` type, so you will have to deal with that accordingly.\n */\nexport function prep<T>(\n mapperFn: (blob: unknown) => unknown,\n decoder: Decoder<T>,\n): Decoder<T> {\n return define((originalInput, _, err) => {\n let blob;\n try {\n blob = mapperFn(originalInput);\n } catch (e: unknown) {\n return err(\n annotate(\n originalInput,\n // istanbul ignore next\n e instanceof Error ? e.message : String(e),\n ),\n );\n }\n\n const r = decoder.decode(blob);\n return r.ok ? r : err(annotate(originalInput, r.error.text));\n // ^^^^^^^^^^^^^\n // Annotates the _original_ input value\n // (instead of echoing back blob)\n });\n}\n\n/**\n * Rejects all inputs, and always fails with the given error message. May be\n * useful for explicitly disallowing keys, or for testing purposes.\n */\nexport function never(msg: string): Decoder<never> {\n return define((_, __, err) => err(msg));\n}\n\n/**\n * Alias of never().\n */\nexport const fail = never;\n","import { define } from '../Decoder';\nimport { indent, summarize } from '../_utils';\nimport { object } from './objects';\nimport { prep } from './utilities';\nimport type { Decoder, DecoderType, DecodeResult, Scalar } from '../Decoder';\nimport type { Ok } from '../result';\n\ntype Values<T extends object> = T[keyof T];\n\ntype DecoderTypes<T> = T extends readonly Decoder<infer U>[] ? U : never;\n\nconst EITHER_PREFIX = 'Either:\\n';\n\n/**\n * Indents and adds a dash in front of this (potentially multiline) string.\n */\nfunction itemize(s: string): string {\n return `-${indent(s).substring(1)}`;\n}\n\n/**\n * Nests another error as an item under a new-to-be-created \"Either error\". If\n * the given subitem already is an \"Either error\" of itself, don't indent, but\n * just \"inject\" its items at the same error level, for nicely flattened either\n * expressions.\n *\n * Avoids:\n *\n * Either:\n * - Either:\n * - Must be P\n * - Either:\n * - Must be Q\n * - Must be R\n * - Must be S\n *\n * And \"flattens\" these to:\n *\n * Either:\n * - Must be P\n * - Must be Q\n * - Must be R\n * - Must be S\n *\n */\nfunction nest(errText: string): string {\n return errText.startsWith(EITHER_PREFIX)\n ? errText.substr(EITHER_PREFIX.length)\n : itemize(errText);\n}\n\n/**\n * Accepts values accepted by any of the given decoders.\n *\n * The decoders are tried on the input one by one, in the given order. The\n * first one that accepts the input \"wins\". If all decoders reject the input,\n * the input gets rejected.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function either<T extends readonly Decoder<any>[]>(\n ...decoders: T\n): Decoder<DecoderTypes<T>> {\n if (decoders.length === 0) {\n throw new Error('Pass at least one decoder to either()');\n }\n\n return define((blob, _, err) => {\n // Collect errors here along the way\n const errors = [];\n\n for (let i = 0; i < decoders.length; i++) {\n const result: DecodeResult<unknown> = decoders[i].decode(blob);\n if (result.ok) {\n return result as Ok<DecoderTypes<T>>;\n } else {\n errors.push(result.error);\n }\n }\n\n // Decoding all alternatives failed, return the combined error message\n const text =\n EITHER_PREFIX + errors.map((err) => nest(summarize(err).join('\\n'))).join('\\n');\n return err(text);\n });\n}\n\n/**\n * Accepts any value that is strictly-equal (using `===`) to one of the\n * specified values.\n */\nexport function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C> {\n return define((blob, ok, err) => {\n const winner = constants.find((c) => c === blob);\n if (winner !== undefined) {\n return ok(winner);\n }\n return err(\n `Must be one of ${constants.map((value) => JSON.stringify(value)).join(', ')}`,\n );\n });\n}\n\n/**\n * If you are decoding tagged unions you may want to use the `taggedUnion()`\n * decoder instead of the general purpose `either()` decoder to get better\n * error messages and better performance.\n *\n * This decoder is optimized for [tagged\n * unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of\n * objects where one field is used as the discriminator.\n *\n * ```ts\n * const A = object({ tag: constant('A'), foo: string });\n * const B = object({ tag: constant('B'), bar: number });\n *\n * const AorB = taggedUnion('tag', { A, B });\n * // ^^^\n * ```\n *\n * Decoding now works in two steps:\n *\n * 1. Look at the `'tag'` field in the incoming object (this is the field\n * that decides which decoder will be used)\n * 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then\n * decoder `B` will be used. Otherwise, this will fail.\n *\n * This is effectively equivalent to `either(A, B)`, but will provide better\n * error messages and is more performant at runtime because it doesn't have to\n * try all decoders one by one.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function taggedUnion<O extends Record<string, Decoder<any>>>(\n field: string,\n mapping: O,\n): Decoder<Values<{ [key in keyof O]: DecoderType<O[key]> }>> {\n const base: Decoder<string> = object({\n [field]: prep(String, oneOf(Object.keys(mapping))),\n }).transform((o) => o[field]);\n return base.peek_UNSTABLE(([blob, key]) => {\n const decoder = mapping[key];\n return decoder.decode(blob);\n });\n}\n","import { define } from '../Decoder';\nimport { either } from './unions';\nimport type { Decoder, Scalar } from '../Decoder';\nimport { lazyval } from '../_utils';\n\n/**\n * Accepts and returns only the literal `null` value.\n */\nexport const null_: Decoder<null> = define((blob, ok, err) =>\n blob === null ? ok(blob) : err('Must be null'),\n);\n\n/**\n * Accepts and returns only the literal `undefined` value.\n */\nexport const undefined_: Decoder<undefined> = define((blob, ok, err) =>\n blob === undefined ? ok(blob) : err('Must be undefined'),\n);\n\nconst undefined_or_null: Decoder<null | undefined> = define((blob, ok, err) =>\n blob === undefined || blob === null\n ? ok(blob)\n : // Combine error message into a single line for readability\n err('Must be undefined or null'),\n);\n\n/**\n * Accepts whatever the given decoder accepts, or `undefined`.\n *\n * If a default value is explicitly provided, return that instead in the\n * `undefined` case.\n */\nexport function optional<T>(decoder: Decoder<T>): Decoder<T | undefined>;\nexport function optional<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>; // prettier-ignore\nexport function optional<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>; // prettier-ignore\nexport function optional<T, V>(\n decoder: Decoder<T>,\n defaultValue?: (() => V) | V,\n): Decoder<T | V | undefined> {\n const rv = either(undefined_, decoder);\n return arguments.length >= 2\n ? rv.transform((value) => value ?? lazyval(defaultValue as (() => V) | V))\n : rv;\n}\n\n/**\n * Accepts whatever the given decoder accepts, or `null`.\n *\n * If a default value is explicitly provided, return that instead in the `null`\n * case.\n */\nexport function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;\nexport function nullable<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>; // prettier-ignore\nexport function nullable<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>; // prettier-ignore\nexport function nullable<T, V>(\n decoder: Decoder<T>,\n defaultValue?: (() => V) | V,\n): Decoder<T | V | null> {\n const rv = either(null_, decoder);\n return arguments.length >= 2\n ? rv.transform((value) => value ?? lazyval(defaultValue as (() => V) | V))\n : rv;\n}\n\n/**\n * Accepts whatever the given decoder accepts, or `null`, or `undefined`.\n *\n * If a default value is explicitly provided, return that instead in the\n * `null`/`undefined` case.\n */\nexport function maybe<T>(decoder: Decoder<T>): Decoder<T | null | undefined>;\nexport function maybe<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>; // prettier-ignore\nexport function maybe<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>; // prettier-ignore\nexport function maybe<T, V>(\n decoder: Decoder<T>,\n defaultValue?: (() => V) | V,\n): Decoder<T | V | null | undefined> {\n const rv = either(undefined_or_null, decoder);\n return arguments.length >= 2\n ? rv.transform((value) => value ?? lazyval(defaultValue as (() => V) | V))\n : rv;\n}\n\n/**\n * Accepts only the given constant value.\n */\nexport function constant<C extends Scalar>(value: C): Decoder<C> {\n return define((blob, ok, err) =>\n blob === value ? ok(value) : err(`Must be constant ${String(value)}`),\n );\n}\n\n/**\n * Accepts anything, completely ignores it, and always returns the provided\n * value instead.\n *\n * This is useful to manually add extra fields to object decoders.\n */\nexport function always<C extends Scalar>(value: C): Decoder<C>;\nexport function always<T>(value: (() => T) | T): Decoder<T>;\nexport function always<T>(value: (() => T) | T): Decoder<T> {\n return define(\n typeof value === 'function'\n ? (_, ok) => ok((value as () => T)())\n : (_, ok) => ok(value),\n );\n}\n\n/**\n * Alias of always.\n */\nexport const hardcoded = always;\n\n/**\n * Accepts anything and returns it unchanged.\n *\n * Useful for situation in which you don't know or expect a specific type. Of\n * course, the downside is that you won't know the type of the value statically\n * and you'll have to further refine it yourself.\n */\nexport const unknown: Decoder<unknown> = define((blob, ok, _) => ok(blob));\n\n/**\n * Alias of unknown.\n */\nexport const mixed = unknown;\n","import { annotate } from '../annotate';\nimport { define } from '../Decoder';\nimport type { Annotation } from '../annotate';\nimport type { Decoder, DecodeResult } from '../Decoder';\n\n/**\n * Accepts any array, but doesn't validate its items further.\n *\n * \"poja\" means \"plain old JavaScript array\", a play on `pojo()`.\n */\nexport const poja: Decoder<unknown[]> = define((blob, ok, err) => {\n if (!Array.isArray(blob)) {\n return err('Must be an array');\n }\n return ok(blob as unknown[]);\n});\n\n/**\n * Given an array of Result instances, loop over them all and return:\n * - An [index, err] tuple, indicating the (index of the) first Err instance\n * encountered; or\n * - a new Ok with an array of all unwrapped Ok'ed values\n */\nfunction all<T>(\n items: readonly DecodeResult<T>[],\n blobs: readonly unknown[],\n\n // TODO: Make this less ugly\n ok: (value: T[]) => DecodeResult<T[]>,\n err: (ann: Annotation) => DecodeResult<T[]>,\n): DecodeResult<T[]> {\n const results: T[] = [];\n for (let index = 0; index < items.length; ++index) {\n const result = items[index];\n if (result.ok) {\n results.push(result.value);\n } else {\n const ann = result.error;\n\n // Rewrite the annotation to include the index information, and inject it into the original blob\n const clone = blobs.slice();\n clone.splice(\n index,\n 1,\n annotate(ann, ann.text ? `${ann.text} (at index ${index})` : `index ${index}`),\n );\n\n return err(annotate(clone));\n }\n }\n return ok(results);\n}\n\n/**\n * Accepts arrays of whatever the given decoder accepts.\n */\nexport function array<T>(decoder: Decoder<T>): Decoder<T[]> {\n const decodeFn = decoder.decode;\n return poja.then((blobs: readonly unknown[], ok, err) => {\n const results = blobs.map(decodeFn);\n return all(results, blobs, ok, err);\n });\n}\n\nfunction isNonEmpty<T>(arr: readonly T[]): arr is [T, ...T[]] {\n return arr.length > 0;\n}\n\n/**\n * Like `array()`, but will reject arrays with 0 elements.\n */\nexport function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]> {\n return array(decoder).refine(isNonEmpty, 'Must be non-empty array');\n}\n\n/**\n * Similar to `array()`, but returns the result as an [ES6\n * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).\n */\nexport function set<T>(decoder: Decoder<T>): Decoder<Set<T>> {\n return array(decoder).transform((items) => new Set(items));\n}\n\nconst ntuple = (n: number) =>\n poja.refine((arr) => arr.length === n, `Must be a ${n}-tuple`);\n\n/**\n * Accepts a tuple (an array with exactly _n_ items) of values accepted by the\n * _n_ given decoders.\n */\nexport function tuple<A>(a: Decoder<A>): Decoder<[A]>;\nexport function tuple<A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<[A, B]>;\nexport function tuple<A, B, C>(\n a: Decoder<A>,\n b: Decoder<B>,\n c: Decoder<C>,\n): Decoder<[A, B, C]>;\nexport function tuple<A, B, C, D>(\n a: Decoder<A>,\n b: Decoder<B>,\n c: Decoder<C>,\n d: Decoder<D>,\n): Decoder<[A, B, C, D]>;\nexport function tuple<A, B, C, D, E>(\n a: Decoder<A>,\n b: Decoder<B>,\n c: Decoder<C>,\n d: Decoder<D>,\n e: Decoder<E>,\n): Decoder<[A, B, C, D, E]>;\nexport function tuple<A, B, C, D, E, F>(\n a: Decoder<A>,\n b: Decoder<B>,\n c: Decoder<C>,\n d: Decoder<D>,\n e: Decoder<E>,\n f: Decoder<F>,\n): Decoder<[A, B, C, D, E, F]>;\nexport function tuple(...decoders: readonly Decoder<unknown>[]): Decoder<unknown[]> {\n return ntuple(decoders.length).then((blobs, ok, err) => {\n let allOk = true;\n\n const rvs = decoders.map((decoder, i) => {\n const blob = blobs[i];\n const result = decoder.decode(blob);\n if (result.ok) {\n return result.value;\n } else {\n allOk = false;\n return result.error;\n }\n });\n\n if (allOk) {\n return ok(rvs);\n } else {\n // If a decoder error has happened while unwrapping all the\n // results, try to construct a good error message\n return err(annotate(rvs));\n }\n });\n}\n","import { define } from '../Decoder';\nimport type { Decoder } from '../Decoder';\n\n/**\n * Accepts any valid ``number`` value.\n *\n * This also accepts special values like `NaN` and `Infinity`. Unless you\n * want to deliberately accept those, you'll likely want to use the\n * `number` decoder instead.\n */\nexport const anyNumber: Decoder<number> = define((blob, ok, err) =>\n typeof blob === 'number' ? ok(blob) : err('Must be number'),\n);\n\n/**\n * Accepts finite numbers (can be integer or float values). Values `NaN`,\n * or positive and negative `Infinity` will get rejected.\n */\nexport const number: Decoder<number> = anyNumber.refine(\n (n) => Number.isFinite(n),\n 'Number must be finite',\n);\n\n/**\n * Accepts only finite whole numbers.\n */\nexport const integer: Decoder<number> = number.refine(\n (n) => Number.isInteger(n),\n 'Number must be an integer',\n);\n\n/**\n * Accepts only non-negative (zero or positive) finite numbers.\n */\nexport const positiveNumber: Decoder<number> = number\n .refine((n) => n >= 0, 'Number must be positive')\n .transform(Math.abs); // Just here to handle the -0 case\n\n/**\n * Accepts only non-negative (zero or positive) finite whole numbers.\n */\nexport const positiveInteger: Decoder<number> = integer\n .refine((n) => n >= 0, 'Number must be positive')\n .transform(Math.abs); // Just here to handle the -0 case\n","import { define } from '../Decoder';\nimport { number } from './numbers';\nimport type { Decoder } from '../Decoder';\n\n/**\n * Accepts and returns booleans.\n */\nexport const boolean: Decoder<boolean> = define((blob, ok, err) => {\n return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean');\n});\n\n/**\n * Accepts anything and will return its \"truth\" value. Will never reject.\n */\nexport const truthy: Decoder<boolean> = define((blob, ok, _) => ok(!!blob));\n\n/**\n * Accepts numbers, but return their boolean representation.\n */\nexport const numericBoolean: Decoder<boolean> = number.transform((n) => !!n);\n","import { define } from '../Decoder';\nimport { either } from './unions';\nimport { instanceOf } from './utilities';\nimport type { Decoder } from '../Decoder';\n\n/** Match groups in this regex:\n * \\1 - the scheme\n * \\2 - the username/password (optional)\n * \\3 - the host\n * \\4 - the port (optional)\n * \\5 - the path (optional)\n */\nconst url_re =\n /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\\/\\/(?:([-;:&=+$,\\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\\/(?:[-+~%/.,\\w]*)?(?:\\?[-+=&;%@.,/\\w]*)?(?:#[.,!/\\w]*)?)?$/;\n\n/**\n * Accepts and returns strings.\n */\nexport const string: Decoder<string> = define((blob, ok, err) =>\n typeof blob === 'string' ? ok(blob) : err('Must be string'),\n);\n\n/**\n * Like `string`, but will reject the empty string or strings containing only whitespace.\n */\nexport const nonEmptyString: Decoder<string> = regex(/\\S/, 'Must be non-empty string');\n\n/**\n * Accepts and returns strings that match the given regular expression.\n */\nexport function regex(regex: RegExp, msg: string): Decoder<string> {\n return string.refine((s) => regex.test(s), msg);\n}\n\n/**\n * Accepts and returns strings that are syntactically valid email addresses.\n * (This will not mean that the email address actually exist.)\n */\nexport const email: Decoder<string> = regex(\n // The almost perfect email regex, taken from https://emailregex.com/\n /^(([^<>()[\\]\\\\.,;:\\s@\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/,\n 'Must be email',\n);\n\n/**\n * Accepts strings that are valid URLs, returns the value as a URL instance.\n */\nexport const url: Decoder<URL> = either(\n regex(url_re, 'Must be URL').transform((value) => new URL(value)),\n instanceOf(URL),\n);\n\n/**\n * Accepts strings that are valid URLs, but only HTTPS ones. Returns the value\n * as a URL instance.\n */\nexport const httpsUrl: Decoder<URL> = url.refine(\n (value) => value.protocol === 'https:',\n 'Must be an HTTPS URL',\n);\n\n/**\n * Accepts strings that are valid\n * [UUIDs](https://en.wikipedia.org/wiki/universally_unique_identifier)\n * (universally unique identifier).\n */\nexport const uuid: Decoder<string> = regex(\n /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,\n 'Must be uuid',\n);\n\n/**\n * Like `uuid`, but only accepts\n * [UUIDv1](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_%28date-time_and_MAC_address%29)\n * strings.\n */\nexport const uuidv1: Decoder<string> =\n // https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_(date-time_and_MAC_address)\n uuid.refine((value) => value[14] === '1', 'Must be uuidv1');\n\n/**\n * Like `uuid`, but only accepts\n * [UUIDv4](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_%28random%29)\n * strings.\n */\nexport const uuidv4: Decoder<string> =\n // https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)\n uuid.refine((value) => value[14] === '4', 'Must be uuidv4');\n","import { asDate } from '../_utils';\nimport { define } from '../Decoder';\nimport { regex } from './strings';\nimport type { Decoder } from '../Decoder';\n\n// Only matches the shape. This \"over-matches\" some values that still aren't\n// valid dates (like 9999-99-99), but those will be caught by JS Date's\n// internal validations\nconst iso8601_re =\n /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:[.]\\d+)?(?:Z|[+-]\\d{2}:?\\d{2})$/;\n\n/**\n * Accepts and returns `Date` instances.\n */\nexport const date: Decoder<Date> = define((blob, ok, err) => {\n const date = asDate(blob);\n return date !== null ? ok(date) : err('Must be a Date');\n});\n\n/**\n * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,\n * returns them as `Date` instances.\n *\n * This is very useful for working with dates in APIs: serialize them as\n * `.toISOString()` when sending, decode them with `iso8601` when receiving.\n */\nexport const iso8601: Decoder<Date> =\n // Input itself needs to match the ISO8601 regex...\n regex(iso8601_re, 'Must be ISO8601 format').transform(\n // Make sure it is a _valid_ date\n (value: string) => {\n const date = new Date(value);\n if (isNaN(date.getTime())) {\n throw new Error('Must be valid date/time value');\n }\n return date;\n },\n );\n","import { array } from './arrays';\nimport { boolean } from './booleans';\nimport { dict } from './objects';\nimport { either } from './unions';\nimport { lazy } from './utilities';\nimport { null_ } from './basics';\nimport { number } from './numbers';\nimport { string } from './strings';\nimport type { Decoder } from '../Decoder';\n\nexport type JSONValue = null | string | number | boolean | JSONObject | JSONArray;\nexport type JSONObject = { [key: string]: JSONValue | undefined };\nexport type JSONArray = JSONValue[];\n\n/**\n * Accepts objects that contain only valid JSON values.\n */\nexport const jsonObject: Decoder<JSONObject> = lazy(() => dict(json));\n\n/**\n * Accepts arrays that contain only valid JSON values.\n */\nexport const jsonArray: Decoder<JSONArray> = lazy(() => array(json));\n\n/**\n * Accepts any value that's a valid JSON value.\n *\n * In other words: any value returned by `JSON.parse()` should decode without\n * failure.\n *\n * ```typescript\n * type JSONValue =\n * | null\n * | string\n * | number\n * | boolean\n * | { [string]: JSONValue }\n * | JSONValue[]\n * ```\n */\nexport const json: Decoder<JSONValue> = either(\n null_,\n string,\n number,\n boolean,\n jsonObject,\n jsonArray,\n).describe('Must be valid JSON value');\n"]}
@@ -0,0 +1,454 @@
1
+ import { A as Annotation } from './annotate-0PUmWHxH.cjs';
2
+ import { Result } from './result.cjs';
3
+
4
+ type Scalar = string | number | boolean | symbol | undefined | null;
5
+ type DecodeResult<T> = Result<T, Annotation>;
6
+ type AcceptanceFn<T, InputT = unknown> = (blob: InputT, ok: (value: T) => DecodeResult<T>, err: (msg: string | Annotation) => DecodeResult<T>) => DecodeResult<T>;
7
+ type Decoder<T> = {
8
+ /**
9
+ * Verifies untrusted input. Either returns a value, or throws a decoding
10
+ * error.
11
+ */
12
+ verify(blob: unknown, formatterFn?: (ann: Annotation) => string | Error): T;
13
+ /**
14
+ * Verifies untrusted input. Either returns a value, or returns undefined.
15
+ */
16
+ value(blob: unknown): T | undefined;
17
+ /**
18
+ * Verifies untrusted input. Always returns a DecodeResult, which is either
19
+ * an "ok" value or an "error" annotation.
20
+ */
21
+ decode(blob: unknown): DecodeResult<T>;
22
+ /**
23
+ * Build a new decoder from the the current one, with an extra acceptance
24
+ * criterium.
25
+ */
26
+ refine<N extends T>(predicate: (value: T) => value is N, msg: string): Decoder<N>;
27
+ refine(predicate: (value: T) => boolean, msg: string): Decoder<T>;
28
+ /**
29
+ * Build a new decoder from the current one, with an extra rejection
30
+ * criterium.
31
+ */
32
+ reject(rejectFn: (value: T) => string | Annotation | null): Decoder<T>;
33
+ /**
34
+ * Build a new decoder from the current one, modifying its outputted value.
35
+ */
36
+ transform<V>(transformFn: (value: T) => V): Decoder<V>;
37
+ /**
38
+ * Build a new decoder from the current one, with a mutated error message
39
+ * in case of a rejection.
40
+ */
41
+ describe(message: string): Decoder<T>;
42
+ /**
43
+ * Chain together the current decoder with another acceptance function.
44
+ */
45
+ then<V>(next: AcceptanceFn<V, T>): Decoder<V>;
46
+ peek_UNSTABLE<V>(next: AcceptanceFn<V, [unknown, T]>): Decoder<V>;
47
+ };
48
+ /**
49
+ * Helper type to return the "type" of a Decoder.
50
+ *
51
+ * You can use it on types:
52
+ *
53
+ * DecoderType<Decoder<string>> // string
54
+ * DecoderType<Decoder<number[]>> // number[]
55
+ *
56
+ * Or on "values", by using the `typeof` keyword:
57
+ *
58
+ * DecoderType<typeof string> // string
59
+ * DecoderType<typeof truthy> // boolean
60
+ *
61
+ */
62
+ type DecoderType<D extends Decoder<any>> = D extends Decoder<infer T> ? T : never;
63
+ /**
64
+ * Defines a new `Decoder<T>`, by implementing a custom acceptance function.
65
+ * The function receives three arguments:
66
+ *
67
+ * 1. `blob` - the raw/unknown input (aka your external data)
68
+ * 2. `ok` - Call `ok(value)` to accept the input and return ``value``
69
+ * 3. `err` - Call `err(message)` to reject the input with error ``message``
70
+ *
71
+ * The expected return value should be a `DecodeResult<T>`, which can be
72
+ * obtained by returning the result of calling the provided `ok` or `err`
73
+ * helper functions. Please note that `ok()` and `err()` don't perform side
74
+ * effects! You'll need to _return_ those values.
75
+ */
76
+ declare function define<T>(fn: AcceptanceFn<T>): Decoder<T>;
77
+
78
+ type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
79
+ type JSONObject = {
80
+ [key: string]: JSONValue | undefined;
81
+ };
82
+ type JSONArray = JSONValue[];
83
+ /**
84
+ * Accepts objects that contain only valid JSON values.
85
+ */
86
+ declare const jsonObject: Decoder<JSONObject>;
87
+ /**
88
+ * Accepts arrays that contain only valid JSON values.
89
+ */
90
+ declare const jsonArray: Decoder<JSONArray>;
91
+ /**
92
+ * Accepts any value that's a valid JSON value.
93
+ *
94
+ * In other words: any value returned by `JSON.parse()` should decode without
95
+ * failure.
96
+ *
97
+ * ```typescript
98
+ * type JSONValue =
99
+ * | null
100
+ * | string
101
+ * | number
102
+ * | boolean
103
+ * | { [string]: JSONValue }
104
+ * | JSONValue[]
105
+ * ```
106
+ */
107
+ declare const json: Decoder<JSONValue>;
108
+
109
+ /**
110
+ * Accepts and returns only the literal `null` value.
111
+ */
112
+ declare const null_: Decoder<null>;
113
+ /**
114
+ * Accepts and returns only the literal `undefined` value.
115
+ */
116
+ declare const undefined_: Decoder<undefined>;
117
+ /**
118
+ * Accepts whatever the given decoder accepts, or `undefined`.
119
+ *
120
+ * If a default value is explicitly provided, return that instead in the
121
+ * `undefined` case.
122
+ */
123
+ declare function optional<T>(decoder: Decoder<T>): Decoder<T | undefined>;
124
+ declare function optional<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>;
125
+ declare function optional<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>;
126
+ /**
127
+ * Accepts whatever the given decoder accepts, or `null`.
128
+ *
129
+ * If a default value is explicitly provided, return that instead in the `null`
130
+ * case.
131
+ */
132
+ declare function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
133
+ declare function nullable<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>;
134
+ declare function nullable<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>;
135
+ /**
136
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
137
+ *
138
+ * If a default value is explicitly provided, return that instead in the
139
+ * `null`/`undefined` case.
140
+ */
141
+ declare function maybe<T>(decoder: Decoder<T>): Decoder<T | null | undefined>;
142
+ declare function maybe<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>;
143
+ declare function maybe<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>;
144
+ /**
145
+ * Accepts only the given constant value.
146
+ */
147
+ declare function constant<C extends Scalar>(value: C): Decoder<C>;
148
+ /**
149
+ * Accepts anything, completely ignores it, and always returns the provided
150
+ * value instead.
151
+ *
152
+ * This is useful to manually add extra fields to object decoders.
153
+ */
154
+ declare function always<C extends Scalar>(value: C): Decoder<C>;
155
+ declare function always<T>(value: (() => T) | T): Decoder<T>;
156
+ /**
157
+ * Alias of always.
158
+ */
159
+ declare const hardcoded: typeof always;
160
+ /**
161
+ * Accepts anything and returns it unchanged.
162
+ *
163
+ * Useful for situation in which you don't know or expect a specific type. Of
164
+ * course, the downside is that you won't know the type of the value statically
165
+ * and you'll have to further refine it yourself.
166
+ */
167
+ declare const unknown: Decoder<unknown>;
168
+ /**
169
+ * Alias of unknown.
170
+ */
171
+ declare const mixed: Decoder<unknown>;
172
+
173
+ /**
174
+ * Accepts any array, but doesn't validate its items further.
175
+ *
176
+ * "poja" means "plain old JavaScript array", a play on `pojo()`.
177
+ */
178
+ declare const poja: Decoder<unknown[]>;
179
+ /**
180
+ * Accepts arrays of whatever the given decoder accepts.
181
+ */
182
+ declare function array<T>(decoder: Decoder<T>): Decoder<T[]>;
183
+ /**
184
+ * Like `array()`, but will reject arrays with 0 elements.
185
+ */
186
+ declare function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]>;
187
+ /**
188
+ * Similar to `array()`, but returns the result as an [ES6
189
+ * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
190
+ */
191
+ declare function set<T>(decoder: Decoder<T>): Decoder<Set<T>>;
192
+ /**
193
+ * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
194
+ * _n_ given decoders.
195
+ */
196
+ declare function tuple<A>(a: Decoder<A>): Decoder<[A]>;
197
+ declare function tuple<A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<[A, B]>;
198
+ declare function tuple<A, B, C>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>): Decoder<[A, B, C]>;
199
+ declare function tuple<A, B, C, D>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>): Decoder<[A, B, C, D]>;
200
+ declare function tuple<A, B, C, D, E>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>): Decoder<[A, B, C, D, E]>;
201
+ declare function tuple<A, B, C, D, E, F>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>): Decoder<[A, B, C, D, E, F]>;
202
+
203
+ /**
204
+ * Accepts and returns booleans.
205
+ */
206
+ declare const boolean: Decoder<boolean>;
207
+ /**
208
+ * Accepts anything and will return its "truth" value. Will never reject.
209
+ */
210
+ declare const truthy: Decoder<boolean>;
211
+ /**
212
+ * Accepts numbers, but return their boolean representation.
213
+ */
214
+ declare const numericBoolean: Decoder<boolean>;
215
+
216
+ /**
217
+ * Accepts and returns `Date` instances.
218
+ */
219
+ declare const date: Decoder<Date>;
220
+ /**
221
+ * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
222
+ * returns them as `Date` instances.
223
+ *
224
+ * This is very useful for working with dates in APIs: serialize them as
225
+ * `.toISOString()` when sending, decode them with `iso8601` when receiving.
226
+ */
227
+ declare const iso8601: Decoder<Date>;
228
+
229
+ type RequiredKeys<T extends object> = {
230
+ [K in keyof T]: undefined extends T[K] ? never : K;
231
+ }[keyof T];
232
+ /**
233
+ * Transforms an object type, by marking all fields that contain "undefined"
234
+ * with a question mark, i.e. allowing implicit-undefineds when
235
+ * explicit-undefined are also allowed.
236
+ *
237
+ * For example, if:
238
+ *
239
+ * type User = {
240
+ * name: string;
241
+ * age: number | null | undefined;
242
+ * }
243
+ *
244
+ * Then UndefinedToOptional<User> will become equivalent to:
245
+ *
246
+ * {
247
+ * name: string;
248
+ * age?: number | null | undefined;
249
+ * ^
250
+ * Note the question mark
251
+ * }
252
+ */
253
+ type UndefinedToOptional<T extends object> = Resolve<Pick<Required<T>, RequiredKeys<T>> & Partial<T>>;
254
+ type Resolve<T> = T extends (...args: readonly unknown[]) => unknown ? T : {
255
+ [K in keyof T]: T[K];
256
+ };
257
+
258
+ type ObjectDecoderType<T> = UndefinedToOptional<{
259
+ [K in keyof T]: T[K] extends Decoder<infer V> ? V : never;
260
+ }>;
261
+ /**
262
+ * Accepts any "plain old JavaScript object", but doesn't validate its keys or
263
+ * values further.
264
+ */
265
+ declare const pojo: Decoder<Record<string, unknown>>;
266
+ /**
267
+ * Accepts objects with fields matching the given decoders. Extra fields that
268
+ * exist on the input object are ignored and will not be returned.
269
+ */
270
+ declare function object(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
271
+ declare function object<DS extends Record<string, Decoder<any>>>(decodersByKey: DS): Decoder<ObjectDecoderType<DS>>;
272
+ /**
273
+ * Like `object()`, but will reject inputs that contain extra fields that are
274
+ * not specified explicitly.
275
+ */
276
+ declare function exact(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
277
+ declare function exact<O extends Record<string, Decoder<any>>>(decodersByKey: O): Decoder<{
278
+ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];
279
+ }>;
280
+ /**
281
+ * Like `object()`, but will pass through any extra fields on the input object
282
+ * unvalidated that will thus be of `unknown` type statically.
283
+ */
284
+ declare function inexact(decodersByKey: Record<any, never>): Decoder<Record<string, unknown>>;
285
+ declare function inexact<O extends Record<string, Decoder<any>>>(decodersByKey: O): Decoder<{
286
+ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];
287
+ } & Record<string, unknown>>;
288
+ /**
289
+ * Accepts objects where all values match the given decoder, and returns the
290
+ * result as a `Record<string, T>`.
291
+ *
292
+ * The main difference between `object()` and `dict()` is that you'd typically
293
+ * use `object()` if this is a record-like object, where all field names are
294
+ * known and the values are heterogeneous. Whereas with `dict()` the keys are
295
+ * typically dynamic and the values homogeneous, like in a dictionary,
296
+ * a lookup table, or a cache.
297
+ */
298
+ declare function dict<T>(decoder: Decoder<T>): Decoder<Record<string, T>>;
299
+ /**
300
+ * Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6
301
+ * Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
302
+ * instead.
303
+ */
304
+ declare function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
305
+
306
+ type Values<T extends object> = T[keyof T];
307
+ type DecoderTypes<T> = T extends readonly Decoder<infer U>[] ? U : never;
308
+ /**
309
+ * Accepts values accepted by any of the given decoders.
310
+ *
311
+ * The decoders are tried on the input one by one, in the given order. The
312
+ * first one that accepts the input "wins". If all decoders reject the input,
313
+ * the input gets rejected.
314
+ */
315
+ declare function either<T extends readonly Decoder<any>[]>(...decoders: T): Decoder<DecoderTypes<T>>;
316
+ /**
317
+ * Accepts any value that is strictly-equal (using `===`) to one of the
318
+ * specified values.
319
+ */
320
+ declare function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C>;
321
+ /**
322
+ * If you are decoding tagged unions you may want to use the `taggedUnion()`
323
+ * decoder instead of the general purpose `either()` decoder to get better
324
+ * error messages and better performance.
325
+ *
326
+ * This decoder is optimized for [tagged
327
+ * unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
328
+ * objects where one field is used as the discriminator.
329
+ *
330
+ * ```ts
331
+ * const A = object({ tag: constant('A'), foo: string });
332
+ * const B = object({ tag: constant('B'), bar: number });
333
+ *
334
+ * const AorB = taggedUnion('tag', { A, B });
335
+ * // ^^^
336
+ * ```
337
+ *
338
+ * Decoding now works in two steps:
339
+ *
340
+ * 1. Look at the `'tag'` field in the incoming object (this is the field
341
+ * that decides which decoder will be used)
342
+ * 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
343
+ * decoder `B` will be used. Otherwise, this will fail.
344
+ *
345
+ * This is effectively equivalent to `either(A, B)`, but will provide better
346
+ * error messages and is more performant at runtime because it doesn't have to
347
+ * try all decoders one by one.
348
+ */
349
+ declare function taggedUnion<O extends Record<string, Decoder<any>>>(field: string, mapping: O): Decoder<Values<{
350
+ [key in keyof O]: DecoderType<O[key]>;
351
+ }>>;
352
+
353
+ /**
354
+ * Accepts and returns strings.
355
+ */
356
+ declare const string: Decoder<string>;
357
+ /**
358
+ * Like `string`, but will reject the empty string or strings containing only whitespace.
359
+ */
360
+ declare const nonEmptyString: Decoder<string>;
361
+ /**
362
+ * Accepts and returns strings that match the given regular expression.
363
+ */
364
+ declare function regex(regex: RegExp, msg: string): Decoder<string>;
365
+ /**
366
+ * Accepts and returns strings that are syntactically valid email addresses.
367
+ * (This will not mean that the email address actually exist.)
368
+ */
369
+ declare const email: Decoder<string>;
370
+ /**
371
+ * Accepts strings that are valid URLs, returns the value as a URL instance.
372
+ */
373
+ declare const url: Decoder<URL>;
374
+ /**
375
+ * Accepts strings that are valid URLs, but only HTTPS ones. Returns the value
376
+ * as a URL instance.
377
+ */
378
+ declare const httpsUrl: Decoder<URL>;
379
+ /**
380
+ * Accepts strings that are valid
381
+ * [UUIDs](https://en.wikipedia.org/wiki/universally_unique_identifier)
382
+ * (universally unique identifier).
383
+ */
384
+ declare const uuid: Decoder<string>;
385
+ /**
386
+ * Like `uuid`, but only accepts
387
+ * [UUIDv1](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_%28date-time_and_MAC_address%29)
388
+ * strings.
389
+ */
390
+ declare const uuidv1: Decoder<string>;
391
+ /**
392
+ * Like `uuid`, but only accepts
393
+ * [UUIDv4](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_%28random%29)
394
+ * strings.
395
+ */
396
+ declare const uuidv4: Decoder<string>;
397
+
398
+ interface Klass<T> extends Function {
399
+ new (...args: readonly any[]): T;
400
+ }
401
+ type Instance<K> = K extends Klass<infer T> ? T : never;
402
+ /**
403
+ * Accepts any value that is an ``instanceof`` the given class.
404
+ */
405
+ declare function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>>;
406
+ /**
407
+ * Lazily evaluate the given decoder. This is useful to build self-referential
408
+ * types for recursive data structures.
409
+ */
410
+ declare function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
411
+ /**
412
+ * Pre-process the data input before passing it into the decoder. This gives
413
+ * you the ability to arbitrarily customize the input on the fly before passing
414
+ * it to the decoder. Of course, the input value at that point is still of
415
+ * ``unknown`` type, so you will have to deal with that accordingly.
416
+ */
417
+ declare function prep<T>(mapperFn: (blob: unknown) => unknown, decoder: Decoder<T>): Decoder<T>;
418
+ /**
419
+ * Rejects all inputs, and always fails with the given error message. May be
420
+ * useful for explicitly disallowing keys, or for testing purposes.
421
+ */
422
+ declare function never(msg: string): Decoder<never>;
423
+ /**
424
+ * Alias of never().
425
+ */
426
+ declare const fail: typeof never;
427
+
428
+ /**
429
+ * Accepts any valid ``number`` value.
430
+ *
431
+ * This also accepts special values like `NaN` and `Infinity`. Unless you
432
+ * want to deliberately accept those, you'll likely want to use the
433
+ * `number` decoder instead.
434
+ */
435
+ declare const anyNumber: Decoder<number>;
436
+ /**
437
+ * Accepts finite numbers (can be integer or float values). Values `NaN`,
438
+ * or positive and negative `Infinity` will get rejected.
439
+ */
440
+ declare const number: Decoder<number>;
441
+ /**
442
+ * Accepts only finite whole numbers.
443
+ */
444
+ declare const integer: Decoder<number>;
445
+ /**
446
+ * Accepts only non-negative (zero or positive) finite numbers.
447
+ */
448
+ declare const positiveNumber: Decoder<number>;
449
+ /**
450
+ * Accepts only non-negative (zero or positive) finite whole numbers.
451
+ */
452
+ declare const positiveInteger: Decoder<number>;
453
+
454
+ export { type DecodeResult, type Decoder, type DecoderType, type JSONArray, type JSONObject, type JSONValue, type Scalar, always, anyNumber, array, boolean, constant, date, define, dict, either, email, exact, fail, hardcoded, httpsUrl, inexact, instanceOf, integer, iso8601, json, jsonArray, jsonObject, lazy, mapping, maybe, mixed, never, nonEmptyArray, nonEmptyString, null_, nullable, number, numericBoolean, object, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, regex, set, string, taggedUnion, truthy, tuple, undefined_, unknown, url, uuid, uuidv1, uuidv4 };