@modulify/validator 0.2.1 → 0.3.1

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 (88) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/README.md +12 -2
  3. package/dist/assert.cjs +99 -63
  4. package/dist/assert.d.cts +37 -0
  5. package/dist/assert.d.mts +37 -0
  6. package/dist/assert.d.ts +24 -3
  7. package/dist/assert.mjs +94 -64
  8. package/dist/assertions.cjs +283 -178
  9. package/dist/assertions.d.cts +56 -0
  10. package/dist/assertions.d.mts +56 -0
  11. package/dist/assertions.d.ts +43 -45
  12. package/dist/assertions.mjs +274 -201
  13. package/dist/checkers.cjs +23 -0
  14. package/dist/checkers.d.cts +8 -0
  15. package/dist/checkers.d.mts +8 -0
  16. package/dist/checkers.d.ts +1 -1
  17. package/dist/checkers.mjs +16 -0
  18. package/dist/combinators.cjs +305 -304
  19. package/dist/combinators.d.cts +16 -0
  20. package/dist/combinators.d.mts +16 -0
  21. package/dist/combinators.d.ts +15 -16
  22. package/dist/combinators.mjs +305 -314
  23. package/dist/constraints.cjs +23 -0
  24. package/dist/constraints.d.cts +4 -0
  25. package/dist/constraints.d.mts +4 -0
  26. package/dist/constraints.d.ts +2 -2
  27. package/dist/constraints.mjs +21 -0
  28. package/dist/extractors.cjs +6 -0
  29. package/dist/extractors.d.cts +2 -0
  30. package/dist/extractors.d.mts +2 -0
  31. package/dist/extractors.mjs +5 -0
  32. package/dist/index.cjs +140 -219
  33. package/dist/index.d.cts +12 -0
  34. package/dist/index.d.mts +12 -0
  35. package/dist/index.d.ts +9 -9
  36. package/dist/index.mjs +93 -222
  37. package/dist/json-schema.cjs +364 -489
  38. package/dist/json-schema.d.cts +14 -0
  39. package/dist/json-schema.d.mts +14 -0
  40. package/dist/json-schema.d.ts +3 -3
  41. package/dist/json-schema.mjs +365 -492
  42. package/dist/metadata.cjs +98 -7
  43. package/dist/metadata.d.cts +8 -0
  44. package/dist/metadata.d.mts +8 -0
  45. package/dist/metadata.d.ts +2 -2
  46. package/dist/metadata.mjs +93 -7
  47. package/dist/predicates.cjs +98 -41
  48. package/dist/predicates.d.cts +77 -0
  49. package/dist/predicates.d.mts +77 -0
  50. package/dist/predicates.d.ts +25 -4
  51. package/dist/predicates.mjs +92 -66
  52. package/dist/types/index.d.cts +984 -0
  53. package/dist/types/index.d.mts +984 -0
  54. package/dist/types/index.d.ts +984 -0
  55. package/dist/types/json-schema.d.cts +75 -0
  56. package/dist/types/json-schema.d.mts +75 -0
  57. package/dist/types/json-schema.d.ts +75 -0
  58. package/dist/violations.cjs +81 -0
  59. package/dist/violations.d.cts +29 -0
  60. package/dist/violations.d.mts +29 -0
  61. package/dist/violations.d.ts +1 -1
  62. package/dist/violations.mjs +80 -0
  63. package/docs/RELEASING.md +66 -0
  64. package/docs/en/00-index.md +2 -0
  65. package/docs/en/01-shape-api.md +35 -10
  66. package/docs/en/02-metadata-and-introspection.md +2 -1
  67. package/docs/en/03-violations.md +1 -1
  68. package/docs/en/04-json-schema-export.md +5 -0
  69. package/docs/en/05-public-api.md +35 -3
  70. package/docs/en/06-common-recipes.md +2 -1
  71. package/docs/en/07-ai-reference.md +5 -2
  72. package/docs/en/08-violation-code-types.md +28 -2
  73. package/docs/en/09-migration.md +75 -0
  74. package/docs/ru/00-index.md +2 -0
  75. package/docs/ru/01-shape-api.md +35 -10
  76. package/docs/ru/02-metadata-and-introspection.md +2 -1
  77. package/docs/ru/03-violations.md +1 -1
  78. package/docs/ru/04-json-schema-export.md +5 -0
  79. package/docs/ru/05-public-api.md +35 -3
  80. package/docs/ru/06-common-recipes.md +2 -1
  81. package/docs/ru/07-ai-reference.md +5 -2
  82. package/docs/ru/08-violation-code-types.md +28 -2
  83. package/docs/ru/09-migration.md +76 -0
  84. package/docs/ru/README.md +10 -2
  85. package/package.json +63 -37
  86. package/types/index.d.ts +275 -115
  87. package/dist/metadata.cjs.js +0 -130
  88. package/dist/metadata.es.js +0 -131
@@ -1,58 +1,56 @@
1
- import { AssertionConstraint, Assertion } from '../types';
2
- import { assert } from './assert';
3
- export { assert };
4
- type LengthAssertionConstraint = AssertionConstraint<string | unknown[], number, [exact: number], 'length.exact'> | AssertionConstraint<string | unknown[], number, [max: number], 'length.max'> | AssertionConstraint<string | unknown[], number, [min: number], 'length.min'> | AssertionConstraint<string | unknown[], number, [range: [number, number]], 'length.range'>;
5
- type SizeAssertionConstraint = AssertionConstraint<Map<unknown, unknown> | Set<unknown>, number, [exact: number], 'size.exact'> | AssertionConstraint<Map<unknown, unknown> | Set<unknown>, number, [max: number], 'size.max'> | AssertionConstraint<Map<unknown, unknown> | Set<unknown>, number, [min: number], 'size.min'> | AssertionConstraint<Map<unknown, unknown> | Set<unknown>, number, [range: [number, number]], 'size.range'>;
6
- type ValueAssertionConstraint = AssertionConstraint<number, number, [exact: number], 'number.exact'> | AssertionConstraint<number, number, [max: number], 'number.max'> | AssertionConstraint<number, number, [min: number], 'number.min'> | AssertionConstraint<number, number, [range: [number, number]], 'number.range'>;
7
- export declare const isBoolean: Assertion<boolean, [], "type.boolean", undefined, "isBoolean">;
8
- export declare const isBigInt: Assertion<bigint, [], "type.bigint", undefined, "isBigInt">;
9
- export declare const isBlob: Assertion<Blob, [], "type.blob", undefined, "isBlob">;
10
- export declare const isDate: Assertion<Date, [], "type.date", undefined, "isDate">;
11
- export declare const isDefined: Assertion<{}, [], "value.defined", undefined, "isDefined">;
12
- export declare const isEmail: Assertion<string, [], "string.email", undefined, "isEmail">;
13
- export declare const isFile: Assertion<File, [], "type.file", undefined, "isFile">;
14
- export declare const isFunction: Assertion<(...args: never[]) => unknown, [], "type.function", undefined, "isFunction">;
15
- export declare const isMap: Assertion<Map<unknown, unknown>, [], "type.map", undefined, "isMap">;
16
- export declare const isNaN: Assertion<number, [], "number.nan", undefined, "isNaN">;
17
- export declare const isNull: Assertion<null, [], "type.null", undefined, "isNull">;
18
- export declare const isNumber: Assertion<number, [], "type.number", undefined, "isNumber">;
19
- export declare const isSet: Assertion<Set<unknown>, [], "type.set", undefined, "isSet">;
20
- export declare const isString: Assertion<string, [], "type.string", undefined, "isString">;
21
- export declare const isSymbol: Assertion<symbol, [], "type.symbol", undefined, "isSymbol">;
22
- export declare const hasLength: ({ exact, max, min, range, bail, }?: {
1
+ import { AssertionConstraint, Guard, Refinement, Extractor, Checker } from './types/index.js';
2
+ import { assert, refine } from './assert.js';
3
+ export { assert, refine, };
4
+ type Defined = {} | null;
5
+ type EmptyOptions = Record<never, never>;
6
+ type BoundedAssertionOptions = {
23
7
  exact?: number | null;
24
8
  max?: number | null;
25
9
  min?: number | null;
26
- range?: [number, number] | null;
27
- bail?: boolean;
28
- }) => Assertion<string | unknown[], LengthAssertionConstraint[], "length.unsupported-type", undefined, "hasLength">;
29
- export declare const hasSize: ({ exact, max, min, range, bail, }?: {
30
- exact?: number | null;
31
- max?: number | null;
32
- min?: number | null;
33
- range?: [number, number] | null;
34
- bail?: boolean;
35
- }) => Assertion<Map<unknown, unknown> | Set<unknown>, SizeAssertionConstraint[], "size.unsupported-type", undefined, "hasSize">;
10
+ range?: readonly [number, number] | null;
11
+ bail?: boolean;
12
+ };
13
+ type OptionNumber<O, K extends PropertyKey> = K extends keyof O ? Extract<O[K], number> : never;
14
+ type OptionRange<O, K extends PropertyKey> = K extends keyof O ? Extract<O[K], readonly [number, number] | [number, number]> extends infer R ? R extends readonly [infer Min extends number, infer Max extends number] ? readonly [Min, Max] : never : never : never;
15
+ type IncludeConstraint<Value, C extends AssertionConstraint> = [Value] extends [never] ? [] : [C];
16
+ export declare const isBoolean: Guard<boolean, [], "type.boolean", [], "isBoolean">;
17
+ export declare const isBigInt: Guard<bigint, [], "type.bigint", [], "isBigInt">;
18
+ export declare const isBlob: Guard<Blob, [], "type.blob", [], "isBlob">;
19
+ export declare const isDate: Guard<Date, [], "type.date", [], "isDate">;
20
+ export declare const isDefined: Guard<Defined, [], "value.defined", [], "isDefined">;
21
+ export declare const isEmail: Guard<string, [], "string.email", [], "isEmail">;
22
+ export declare const isError: Guard<Error, [], "type.error", [], "isError">;
23
+ export declare const isFiniteNumber: Guard<number, [], "number.finite", [], "isFiniteNumber">;
24
+ export declare const isFile: Guard<File, [], "type.file", [], "isFile">;
25
+ export declare const isFunction: Guard<(...args: never[]) => unknown, [], "type.function", [], "isFunction">;
26
+ export declare const isInteger: Guard<number, [], "number.integer", [], "isInteger">;
27
+ export declare const isMap: Guard<Map<unknown, unknown>, [], "type.map", [], "isMap">;
28
+ export declare const isNaN: Guard<number, [], "number.nan", [], "isNaN">;
29
+ export declare const isNull: Guard<null, [], "type.null", [], "isNull">;
30
+ export declare const isNumber: Guard<number, [], "type.number", [], "isNumber">;
31
+ export declare const isPromiseLike: Guard<PromiseLike<unknown>, [], "type.promise-like", [], "isPromiseLike">;
32
+ export declare const isRegExp: Guard<RegExp, [], "type.regexp", [], "isRegExp">;
33
+ export declare const isSafeInteger: Guard<number, [], "number.safe-integer", [], "isSafeInteger">;
34
+ export declare const isSet: Guard<Set<unknown>, [], "type.set", [], "isSet">;
35
+ export declare const isString: Guard<string, [], "type.string", [], "isString">;
36
+ export declare const isSymbol: Guard<symbol, [], "type.symbol", [], "isSymbol">;
37
+ export declare const isValidDate: Guard<Date, [], "date.valid", [], "isValidDate">;
38
+ export declare const hasLength: <const O extends BoundedAssertionOptions = EmptyOptions>(options?: O) => Refinement<[...IncludeConstraint<OptionNumber<O, "exact">, readonly [ Extractor<string | unknown[], number>, Checker<number, [exact: OptionNumber<O, "exact">]>, "length.exact", exact: OptionNumber<O, "exact">]>, ...IncludeConstraint<OptionNumber<O, "max">, readonly [ Extractor<string | unknown[], number>, Checker<number, [max: OptionNumber<O, "max">]>, "length.max", max: OptionNumber<O, "max">]>, ...IncludeConstraint<OptionNumber<O, "min">, readonly [ Extractor<string | unknown[], number>, Checker<number, [min: OptionNumber<O, "min">]>, "length.min", min: OptionNumber<O, "min">]>, ...IncludeConstraint<OptionRange<O, "range">, readonly [ Extractor<string | unknown[], number>, Checker<number, [range: OptionRange<O, "range">]>, "length.range", range: OptionRange<O, "range">]>] extends infer T ? T extends [...IncludeConstraint<OptionNumber<O, "exact">, readonly [ Extractor<string | unknown[], number>, Checker<number, [exact: OptionNumber<O, "exact">]>, "length.exact", exact: OptionNumber<O, "exact">]>, ...IncludeConstraint<OptionNumber<O, "max">, readonly [ Extractor<string | unknown[], number>, Checker<number, [max: OptionNumber<O, "max">]>, "length.max", max: OptionNumber<O, "max">]>, ...IncludeConstraint<OptionNumber<O, "min">, readonly [ Extractor<string | unknown[], number>, Checker<number, [min: OptionNumber<O, "min">]>, "length.min", min: OptionNumber<O, "min">]>, ...IncludeConstraint<OptionRange<O, "range">, readonly [ Extractor<string | unknown[], number>, Checker<number, [range: OptionRange<O, "range">]>, "length.range", range: OptionRange<O, "range">]>] ? T extends readonly [] ? unknown : (T[number] extends infer T_1 ? T_1 extends T[number] ? T_1 extends unknown ? (value: Parameters<T_1[0]>[0]) => void : never : never : never) extends (value: infer Input) => void ? Input : never : never : never, [...IncludeConstraint<OptionNumber<O, "exact">, readonly [ Extractor<string | unknown[], number>, Checker<number, [exact: OptionNumber<O, "exact">]>, "length.exact", exact: OptionNumber<O, "exact">]>, ...IncludeConstraint<OptionNumber<O, "max">, readonly [ Extractor<string | unknown[], number>, Checker<number, [max: OptionNumber<O, "max">]>, "length.max", max: OptionNumber<O, "max">]>, ...IncludeConstraint<OptionNumber<O, "min">, readonly [ Extractor<string | unknown[], number>, Checker<number, [min: OptionNumber<O, "min">]>, "length.min", min: OptionNumber<O, "min">]>, ...IncludeConstraint<OptionRange<O, "range">, readonly [ Extractor<string | unknown[], number>, Checker<number, [range: OptionRange<O, "range">]>, "length.range", range: OptionRange<O, "range">]>], "length.unsupported-type", [], "hasLength">;
39
+ export declare const hasSize: <const O extends BoundedAssertionOptions = EmptyOptions>(options?: O) => Refinement<[...IncludeConstraint<OptionNumber<O, "exact">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [exact: OptionNumber<O, "exact">]>, "size.exact", exact: OptionNumber<O, "exact">]>, ...IncludeConstraint<OptionNumber<O, "max">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [max: OptionNumber<O, "max">]>, "size.max", max: OptionNumber<O, "max">]>, ...IncludeConstraint<OptionNumber<O, "min">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [min: OptionNumber<O, "min">]>, "size.min", min: OptionNumber<O, "min">]>, ...IncludeConstraint<OptionRange<O, "range">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [range: OptionRange<O, "range">]>, "size.range", range: OptionRange<O, "range">]>] extends infer T ? T extends [...IncludeConstraint<OptionNumber<O, "exact">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [exact: OptionNumber<O, "exact">]>, "size.exact", exact: OptionNumber<O, "exact">]>, ...IncludeConstraint<OptionNumber<O, "max">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [max: OptionNumber<O, "max">]>, "size.max", max: OptionNumber<O, "max">]>, ...IncludeConstraint<OptionNumber<O, "min">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [min: OptionNumber<O, "min">]>, "size.min", min: OptionNumber<O, "min">]>, ...IncludeConstraint<OptionRange<O, "range">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [range: OptionRange<O, "range">]>, "size.range", range: OptionRange<O, "range">]>] ? T extends readonly [] ? unknown : (T[number] extends infer T_1 ? T_1 extends T[number] ? T_1 extends unknown ? (value: Parameters<T_1[0]>[0]) => void : never : never : never) extends (value: infer Input) => void ? Input : never : never : never, [...IncludeConstraint<OptionNumber<O, "exact">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [exact: OptionNumber<O, "exact">]>, "size.exact", exact: OptionNumber<O, "exact">]>, ...IncludeConstraint<OptionNumber<O, "max">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [max: OptionNumber<O, "max">]>, "size.max", max: OptionNumber<O, "max">]>, ...IncludeConstraint<OptionNumber<O, "min">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [min: OptionNumber<O, "min">]>, "size.min", min: OptionNumber<O, "min">]>, ...IncludeConstraint<OptionRange<O, "range">, readonly [ Extractor<Map<unknown, unknown> | Set<unknown>, number>, Checker<number, [range: OptionRange<O, "range">]>, "size.range", range: OptionRange<O, "range">]>], "size.unsupported-type", [], "hasSize">;
36
40
  export declare const hasPattern: (pattern: RegExp, { bail, }?: {
37
41
  bail?: boolean;
38
- }) => Assertion<string, readonly [readonly [(value: string) => string, (value: string, pattern: RegExp) => boolean, "string.pattern", RegExp]], "string.unsupported-type", undefined, "hasPattern">;
42
+ }) => Refinement<string, readonly [readonly [(value: string) => string, (value: string, pattern: RegExp) => boolean, "string.pattern", RegExp]], "string.unsupported-type", [], "hasPattern">;
39
43
  export declare const startsWith: (prefix: string, { bail, }?: {
40
44
  bail?: boolean;
41
- }) => Assertion<string, readonly [readonly [(value: string) => string, (value: string, prefix: string) => boolean, "string.starts-with", string]], "string.unsupported-type", undefined, "startsWith">;
45
+ }) => Refinement<string, readonly [readonly [(value: string) => string, (value: string, prefix: string) => boolean, "string.starts-with", string]], "string.unsupported-type", [], "startsWith">;
42
46
  export declare const endsWith: (suffix: string, { bail, }?: {
43
47
  bail?: boolean;
44
- }) => Assertion<string, readonly [readonly [(value: string) => string, (value: string, suffix: string) => boolean, "string.ends-with", string]], "string.unsupported-type", undefined, "endsWith">;
45
- export declare const hasValue: ({ exact, max, min, range, bail, }?: {
46
- exact?: number | null;
47
- max?: number | null;
48
- min?: number | null;
49
- range?: [number, number] | null;
50
- bail?: boolean;
51
- }) => Assertion<number, ValueAssertionConstraint[], "number.unsupported-type", undefined, "hasValue">;
52
- export declare const multipleOf: (step: number, { bail, }?: {
48
+ }) => Refinement<string, readonly [readonly [(value: string) => string, (value: string, suffix: string) => boolean, "string.ends-with", string]], "string.unsupported-type", [], "endsWith">;
49
+ export declare const hasValue: <const O extends BoundedAssertionOptions = EmptyOptions>(options?: O) => Refinement<[...IncludeConstraint<OptionNumber<O, "exact">, readonly [ Extractor<number, number>, Checker<number, [exact: OptionNumber<O, "exact">]>, "number.exact", exact: OptionNumber<O, "exact">]>, ...IncludeConstraint<OptionNumber<O, "max">, readonly [ Extractor<number, number>, Checker<number, [max: OptionNumber<O, "max">]>, "number.max", max: OptionNumber<O, "max">]>, ...IncludeConstraint<OptionNumber<O, "min">, readonly [ Extractor<number, number>, Checker<number, [min: OptionNumber<O, "min">]>, "number.min", min: OptionNumber<O, "min">]>, ...IncludeConstraint<OptionRange<O, "range">, readonly [ Extractor<number, number>, Checker<number, [range: OptionRange<O, "range">]>, "number.range", range: OptionRange<O, "range">]>] extends infer T ? T extends [...IncludeConstraint<OptionNumber<O, "exact">, readonly [ Extractor<number, number>, Checker<number, [exact: OptionNumber<O, "exact">]>, "number.exact", exact: OptionNumber<O, "exact">]>, ...IncludeConstraint<OptionNumber<O, "max">, readonly [ Extractor<number, number>, Checker<number, [max: OptionNumber<O, "max">]>, "number.max", max: OptionNumber<O, "max">]>, ...IncludeConstraint<OptionNumber<O, "min">, readonly [ Extractor<number, number>, Checker<number, [min: OptionNumber<O, "min">]>, "number.min", min: OptionNumber<O, "min">]>, ...IncludeConstraint<OptionRange<O, "range">, readonly [ Extractor<number, number>, Checker<number, [range: OptionRange<O, "range">]>, "number.range", range: OptionRange<O, "range">]>] ? T extends readonly [] ? unknown : (T[number] extends infer T_1 ? T_1 extends T[number] ? T_1 extends unknown ? (value: Parameters<T_1[0]>[0]) => void : never : never : never) extends (value: infer Input) => void ? Input : never : never : never, [...IncludeConstraint<OptionNumber<O, "exact">, readonly [ Extractor<number, number>, Checker<number, [exact: OptionNumber<O, "exact">]>, "number.exact", exact: OptionNumber<O, "exact">]>, ...IncludeConstraint<OptionNumber<O, "max">, readonly [ Extractor<number, number>, Checker<number, [max: OptionNumber<O, "max">]>, "number.max", max: OptionNumber<O, "max">]>, ...IncludeConstraint<OptionNumber<O, "min">, readonly [ Extractor<number, number>, Checker<number, [min: OptionNumber<O, "min">]>, "number.min", min: OptionNumber<O, "min">]>, ...IncludeConstraint<OptionRange<O, "range">, readonly [ Extractor<number, number>, Checker<number, [range: OptionRange<O, "range">]>, "number.range", range: OptionRange<O, "range">]>], "number.unsupported-type", [], "hasValue">;
50
+ export declare const multipleOf: <const Step extends number>(step: Step, { bail, }?: {
53
51
  bail?: boolean;
54
- }) => Assertion<number, readonly [readonly [(value: number) => number, (value: number, step: number) => boolean, "number.multiple-of", number]], "number.unsupported-type", undefined, "multipleOf">;
52
+ }) => Refinement<number, readonly [readonly [(value: number) => number, (value: number, step: number) => boolean, "number.multiple-of", Step]], "number.unsupported-type", [], "multipleOf">;
55
53
  export declare const oneOf: <Actual = unknown>(values: Actual[] | Record<string, Actual>, { equalTo, bail, }?: {
56
54
  equalTo?: (a: Actual, b: unknown) => boolean;
57
55
  bail?: boolean;
58
- }) => Assertion<Actual, [], "value.one-of", readonly [Actual[]], "oneOf">;
56
+ }) => Guard<Actual, [], "value.one-of", readonly [Actual[]], "oneOf">;
@@ -1,207 +1,280 @@
1
- import { assert } from "./assert.mjs";
2
- import { isString as isString$1, isArray, isMap as isMap$1, isSet as isSet$1, isNumber as isNumber$1, isBigInt as isBigInt$1, isBlob as isBlob$1, isBoolean as isBoolean$1, isDate as isDate$1, isEmail as isEmail$1, isFile as isFile$1, isFunction as isFunction$1, isNaN as isNaN$1, isNull as isNull$1, isSymbol as isSymbol$1 } from "./predicates.mjs";
3
- const isEqual = (value, exact) => value === exact;
4
- const isGte = (value, min) => value >= min;
5
- const isLte = (value, max) => value <= max;
6
- const inRange = (value, [min, max]) => value >= min && value <= max;
7
- const startsWith$1 = (value, prefix) => value.startsWith(prefix);
8
- const endsWith$1 = (value, suffix) => value.endsWith(suffix);
9
- const matchesPattern = (value, pattern) => new RegExp(pattern.source, pattern.flags).test(value);
10
- const isMultipleOf = (value, step) => {
11
- if (step === 0) {
12
- return false;
13
- }
14
- const quotient = value / step;
15
- const nearestInteger = Math.round(quotient);
16
- const delta = Math.abs(quotient - nearestInteger);
17
- return Number.isFinite(quotient) && delta <= Number.EPSILON * Math.max(1, Math.abs(quotient)) * 16;
1
+ import { assert, createRefinement, refine } from "./assert.mjs";
2
+ import { endsWith as endsWith$1, inRange, isEqual, isGte, isLte, isMultipleOf, matchesPattern, startsWith as startsWith$1 } from "./checkers.mjs";
3
+ import { length, size } from "./extractors.mjs";
4
+ import { isArray, isBigInt as isBigInt$1, isBlob as isBlob$1, isBoolean as isBoolean$1, isDate as isDate$1, isEmail as isEmail$1, isError as isError$1, isFile as isFile$1, isFiniteNumber as isFiniteNumber$1, isFunction as isFunction$1, isInteger as isInteger$1, isMap as isMap$1, isNaN as isNaN$1, isNull as isNull$1, isNumber as isNumber$1, isPromiseLike as isPromiseLike$1, isRegExp as isRegExp$1, isSafeInteger as isSafeInteger$1, isSet as isSet$1, isString as isString$1, isSymbol as isSymbol$1, isValidDate as isValidDate$1 } from "./predicates.mjs";
5
+ //#region src/assertions.ts
6
+ var buildLengthConstraints = (options) => {
7
+ const { exact = null, max = null, min = null, range = null } = options;
8
+ const constraints = [];
9
+ if (exact !== null) constraints.push([
10
+ length,
11
+ isEqual,
12
+ "length.exact",
13
+ exact
14
+ ]);
15
+ if (max !== null) constraints.push([
16
+ length,
17
+ isLte,
18
+ "length.max",
19
+ max
20
+ ]);
21
+ if (min !== null) constraints.push([
22
+ length,
23
+ isGte,
24
+ "length.min",
25
+ min
26
+ ]);
27
+ if (range !== null) constraints.push([
28
+ length,
29
+ inRange,
30
+ "length.range",
31
+ range
32
+ ]);
33
+ return constraints;
18
34
  };
19
- const length = (value) => value.length;
20
- const size = (value) => value.size;
21
- const isBoolean = assert(isBoolean$1, { name: "isBoolean", bail: true, code: "type.boolean" });
22
- const isBigInt = assert(isBigInt$1, { name: "isBigInt", bail: true, code: "type.bigint" });
23
- const isBlob = assert(isBlob$1, { name: "isBlob", bail: true, code: "type.blob" });
24
- const isDate = assert(isDate$1, { name: "isDate", bail: true, code: "type.date" });
25
- const isDefined = assert((value) => value !== void 0, {
26
- name: "isDefined",
27
- bail: true,
28
- code: "value.defined"
29
- });
30
- const isEmail = assert(isEmail$1, { name: "isEmail", bail: true, code: "string.email" });
31
- const isFile = assert(isFile$1, { name: "isFile", bail: true, code: "type.file" });
32
- const isFunction = assert(isFunction$1, { name: "isFunction", bail: true, code: "type.function" });
33
- const isMap = assert(isMap$1, { name: "isMap", bail: true, code: "type.map" });
34
- const isNaN = assert(isNaN$1, { name: "isNaN", bail: true, code: "number.nan" });
35
- const isNull = assert(isNull$1, { name: "isNull", bail: true, code: "type.null" });
36
- const isNumber = assert(isNumber$1, { name: "isNumber", bail: true, code: "type.number" });
37
- const isSet = assert(isSet$1, { name: "isSet", bail: true, code: "type.set" });
38
- const isString = assert(isString$1, { name: "isString", bail: true, code: "type.string" });
39
- const isSymbol = assert(isSymbol$1, { name: "isSymbol", bail: true, code: "type.symbol" });
40
- const hasLength = ({
41
- exact = null,
42
- max = null,
43
- min = null,
44
- range = null,
45
- bail = false
46
- } = {}) => {
47
- const constraints = [];
48
- if (exact !== null) constraints.push([length, isEqual, "length.exact", exact]);
49
- if (max !== null) constraints.push([length, isLte, "length.max", max]);
50
- if (min !== null) constraints.push([length, isGte, "length.min", min]);
51
- if (range !== null) constraints.push([length, inRange, "length.range", range]);
52
- return assert(
53
- (value) => isArray(value) || isString$1(value),
54
- {
55
- name: "hasLength",
56
- bail,
57
- code: "length.unsupported-type"
58
- },
59
- constraints
60
- );
35
+ var buildSizeConstraints = (options) => {
36
+ const { exact = null, max = null, min = null, range = null } = options;
37
+ const constraints = [];
38
+ if (exact !== null) constraints.push([
39
+ size,
40
+ isEqual,
41
+ "size.exact",
42
+ exact
43
+ ]);
44
+ if (max !== null) constraints.push([
45
+ size,
46
+ isLte,
47
+ "size.max",
48
+ max
49
+ ]);
50
+ if (min !== null) constraints.push([
51
+ size,
52
+ isGte,
53
+ "size.min",
54
+ min
55
+ ]);
56
+ if (range !== null) constraints.push([
57
+ size,
58
+ inRange,
59
+ "size.range",
60
+ range
61
+ ]);
62
+ return constraints;
61
63
  };
62
- const hasSize = ({
63
- exact = null,
64
- max = null,
65
- min = null,
66
- range = null,
67
- bail = false
68
- } = {}) => {
69
- const constraints = [];
70
- if (exact !== null) constraints.push([size, isEqual, "size.exact", exact]);
71
- if (max !== null) constraints.push([size, isLte, "size.max", max]);
72
- if (min !== null) constraints.push([size, isGte, "size.min", min]);
73
- if (range !== null) constraints.push([size, inRange, "size.range", range]);
74
- return assert(
75
- (value) => isMap$1(value) || isSet$1(value),
76
- {
77
- name: "hasSize",
78
- bail,
79
- code: "size.unsupported-type"
80
- },
81
- constraints
82
- );
64
+ var buildValueConstraints = (options) => {
65
+ const { exact = null, max = null, min = null, range = null } = options;
66
+ const constraints = [];
67
+ if (exact !== null) constraints.push([
68
+ (value) => value,
69
+ isEqual,
70
+ "number.exact",
71
+ exact
72
+ ]);
73
+ if (max !== null) constraints.push([
74
+ (value) => value,
75
+ isLte,
76
+ "number.max",
77
+ max
78
+ ]);
79
+ if (min !== null) constraints.push([
80
+ (value) => value,
81
+ isGte,
82
+ "number.min",
83
+ min
84
+ ]);
85
+ if (range !== null) constraints.push([
86
+ (value) => value,
87
+ inRange,
88
+ "number.range",
89
+ range
90
+ ]);
91
+ return constraints;
83
92
  };
84
- const hasPattern = (pattern, {
85
- bail = false
86
- } = {}) => assert(
87
- isString$1,
88
- {
89
- name: "hasPattern",
90
- bail,
91
- code: "string.unsupported-type"
92
- },
93
- [[
94
- (value) => value,
95
- matchesPattern,
96
- "string.pattern",
97
- pattern
98
- ]]
99
- );
100
- const startsWith = (prefix, {
101
- bail = false
102
- } = {}) => assert(
103
- isString$1,
104
- {
105
- name: "startsWith",
106
- bail,
107
- code: "string.unsupported-type"
108
- },
109
- [[
110
- (value) => value,
111
- startsWith$1,
112
- "string.starts-with",
113
- prefix
114
- ]]
115
- );
116
- const endsWith = (suffix, {
117
- bail = false
118
- } = {}) => assert(
119
- isString$1,
120
- {
121
- name: "endsWith",
122
- bail,
123
- code: "string.unsupported-type"
124
- },
125
- [[
126
- (value) => value,
127
- endsWith$1,
128
- "string.ends-with",
129
- suffix
130
- ]]
131
- );
132
- const hasValue = ({
133
- exact = null,
134
- max = null,
135
- min = null,
136
- range = null,
137
- bail = false
138
- } = {}) => {
139
- const constraints = [];
140
- if (exact !== null) constraints.push([(value) => value, isEqual, "number.exact", exact]);
141
- if (max !== null) constraints.push([(value) => value, isLte, "number.max", max]);
142
- if (min !== null) constraints.push([(value) => value, isGte, "number.min", min]);
143
- if (range !== null) constraints.push([(value) => value, inRange, "number.range", range]);
144
- return assert(
145
- isNumber$1,
146
- {
147
- name: "hasValue",
148
- bail,
149
- code: "number.unsupported-type"
150
- },
151
- constraints
152
- );
93
+ var isBoolean = /* @__PURE__ */ assert(isBoolean$1, {
94
+ name: "isBoolean",
95
+ bail: true,
96
+ code: "type.boolean"
97
+ });
98
+ var isBigInt = /* @__PURE__ */ assert(isBigInt$1, {
99
+ name: "isBigInt",
100
+ bail: true,
101
+ code: "type.bigint"
102
+ });
103
+ var isBlob = /* @__PURE__ */ assert(isBlob$1, {
104
+ name: "isBlob",
105
+ bail: true,
106
+ code: "type.blob"
107
+ });
108
+ var isDate = /* @__PURE__ */ assert(isDate$1, {
109
+ name: "isDate",
110
+ bail: true,
111
+ code: "type.date"
112
+ });
113
+ var isDefined = /* @__PURE__ */ assert((value) => value !== void 0, {
114
+ name: "isDefined",
115
+ bail: true,
116
+ code: "value.defined"
117
+ });
118
+ var isEmail = /* @__PURE__ */ assert(isEmail$1, {
119
+ name: "isEmail",
120
+ bail: true,
121
+ code: "string.email"
122
+ });
123
+ var isError = /* @__PURE__ */ assert(isError$1, {
124
+ name: "isError",
125
+ bail: true,
126
+ code: "type.error"
127
+ });
128
+ var isFiniteNumber = /* @__PURE__ */ assert(isFiniteNumber$1, {
129
+ name: "isFiniteNumber",
130
+ bail: true,
131
+ code: "number.finite"
132
+ });
133
+ var isFile = /* @__PURE__ */ assert(isFile$1, {
134
+ name: "isFile",
135
+ bail: true,
136
+ code: "type.file"
137
+ });
138
+ var isFunction = /* @__PURE__ */ assert(isFunction$1, {
139
+ name: "isFunction",
140
+ bail: true,
141
+ code: "type.function"
142
+ });
143
+ var isInteger = /* @__PURE__ */ assert(isInteger$1, {
144
+ name: "isInteger",
145
+ bail: true,
146
+ code: "number.integer"
147
+ });
148
+ var isMap = /* @__PURE__ */ assert(isMap$1, {
149
+ name: "isMap",
150
+ bail: true,
151
+ code: "type.map"
152
+ });
153
+ var isNaN = /* @__PURE__ */ assert(isNaN$1, {
154
+ name: "isNaN",
155
+ bail: true,
156
+ code: "number.nan"
157
+ });
158
+ var isNull = /* @__PURE__ */ assert(isNull$1, {
159
+ name: "isNull",
160
+ bail: true,
161
+ code: "type.null"
162
+ });
163
+ var isNumber = /* @__PURE__ */ assert(isNumber$1, {
164
+ name: "isNumber",
165
+ bail: true,
166
+ code: "type.number"
167
+ });
168
+ var isPromiseLike = /* @__PURE__ */ assert(isPromiseLike$1, {
169
+ name: "isPromiseLike",
170
+ bail: true,
171
+ code: "type.promise-like"
172
+ });
173
+ var isRegExp = /* @__PURE__ */ assert(isRegExp$1, {
174
+ name: "isRegExp",
175
+ bail: true,
176
+ code: "type.regexp"
177
+ });
178
+ var isSafeInteger = /* @__PURE__ */ assert(isSafeInteger$1, {
179
+ name: "isSafeInteger",
180
+ bail: true,
181
+ code: "number.safe-integer"
182
+ });
183
+ var isSet = /* @__PURE__ */ assert(isSet$1, {
184
+ name: "isSet",
185
+ bail: true,
186
+ code: "type.set"
187
+ });
188
+ var isString = /* @__PURE__ */ assert(isString$1, {
189
+ name: "isString",
190
+ bail: true,
191
+ code: "type.string"
192
+ });
193
+ var isSymbol = /* @__PURE__ */ assert(isSymbol$1, {
194
+ name: "isSymbol",
195
+ bail: true,
196
+ code: "type.symbol"
197
+ });
198
+ var isValidDate = /* @__PURE__ */ assert(isValidDate$1, {
199
+ name: "isValidDate",
200
+ bail: true,
201
+ code: "date.valid"
202
+ });
203
+ var hasLength = (options = {}) => {
204
+ const { bail = false } = options;
205
+ const constraints = buildLengthConstraints(options);
206
+ return /* @__PURE__ */ createRefinement({
207
+ name: "hasLength",
208
+ bail,
209
+ code: "length.unsupported-type"
210
+ }, constraints);
211
+ };
212
+ var hasSize = (options = {}) => {
213
+ const { bail = false } = options;
214
+ const constraints = buildSizeConstraints(options);
215
+ return /* @__PURE__ */ createRefinement({
216
+ name: "hasSize",
217
+ bail,
218
+ code: "size.unsupported-type"
219
+ }, constraints);
153
220
  };
154
- const multipleOf = (step, {
155
- bail = false
156
- } = {}) => assert(
157
- isNumber$1,
158
- {
159
- name: "multipleOf",
160
- bail,
161
- code: "number.unsupported-type"
162
- },
163
- [[
164
- (value) => value,
165
- isMultipleOf,
166
- "number.multiple-of",
167
- step
168
- ]]
169
- );
170
- const oneOf = (values, {
171
- equalTo = (a, b) => a === b,
172
- bail = false
173
- } = {}) => {
174
- const haystack = isArray(values) ? values : Object.values(values);
175
- return assert((value) => haystack.some((item) => equalTo(item, value)), {
176
- name: "oneOf",
177
- bail,
178
- code: "value.one-of",
179
- args: [haystack]
180
- });
221
+ var hasPattern = (pattern, { bail = false } = {}) => /* @__PURE__ */ createRefinement({
222
+ name: "hasPattern",
223
+ bail,
224
+ code: "string.unsupported-type"
225
+ }, [[
226
+ (value) => value,
227
+ matchesPattern,
228
+ "string.pattern",
229
+ pattern
230
+ ]]);
231
+ var startsWith = (prefix, { bail = false } = {}) => /* @__PURE__ */ createRefinement({
232
+ name: "startsWith",
233
+ bail,
234
+ code: "string.unsupported-type"
235
+ }, [[
236
+ (value) => value,
237
+ startsWith$1,
238
+ "string.starts-with",
239
+ prefix
240
+ ]]);
241
+ var endsWith = (suffix, { bail = false } = {}) => /* @__PURE__ */ createRefinement({
242
+ name: "endsWith",
243
+ bail,
244
+ code: "string.unsupported-type"
245
+ }, [[
246
+ (value) => value,
247
+ endsWith$1,
248
+ "string.ends-with",
249
+ suffix
250
+ ]]);
251
+ var hasValue = (options = {}) => {
252
+ const { bail = false } = options;
253
+ const constraints = buildValueConstraints(options);
254
+ return /* @__PURE__ */ createRefinement({
255
+ name: "hasValue",
256
+ bail,
257
+ code: "number.unsupported-type"
258
+ }, constraints);
181
259
  };
182
- export {
183
- assert,
184
- endsWith,
185
- hasLength,
186
- hasPattern,
187
- hasSize,
188
- hasValue,
189
- isBigInt,
190
- isBlob,
191
- isBoolean,
192
- isDate,
193
- isDefined,
194
- isEmail,
195
- isFile,
196
- isFunction,
197
- isMap,
198
- isNaN,
199
- isNull,
200
- isNumber,
201
- isSet,
202
- isString,
203
- isSymbol,
204
- multipleOf,
205
- oneOf,
206
- startsWith
260
+ var multipleOf = (step, { bail = false } = {}) => /* @__PURE__ */ createRefinement({
261
+ name: "multipleOf",
262
+ bail,
263
+ code: "number.unsupported-type"
264
+ }, [[
265
+ (value) => value,
266
+ isMultipleOf,
267
+ "number.multiple-of",
268
+ step
269
+ ]]);
270
+ var oneOf = (values, { equalTo = (a, b) => a === b, bail = false } = {}) => {
271
+ const haystack = isArray(values) ? values : Object.values(values);
272
+ return /* @__PURE__ */ assert((value) => haystack.some((item) => equalTo(item, value)), {
273
+ name: "oneOf",
274
+ bail,
275
+ code: "value.one-of",
276
+ args: [haystack]
277
+ });
207
278
  };
279
+ //#endregion
280
+ export { assert, endsWith, hasLength, hasPattern, hasSize, hasValue, isBigInt, isBlob, isBoolean, isDate, isDefined, isEmail, isError, isFile, isFiniteNumber, isFunction, isInteger, isMap, isNaN, isNull, isNumber, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isValidDate, multipleOf, oneOf, refine, startsWith };
@@ -0,0 +1,23 @@
1
+ //#region src/checkers.ts
2
+ var isEqual = (value, exact) => value === exact;
3
+ var isGte = (value, min) => value >= min;
4
+ var isLte = (value, max) => value <= max;
5
+ var inRange = (value, [min, max]) => value >= min && value <= max;
6
+ var startsWith = (value, prefix) => value.startsWith(prefix);
7
+ var endsWith = (value, suffix) => value.endsWith(suffix);
8
+ var matchesPattern = (value, pattern) => new RegExp(pattern.source, pattern.flags).test(value);
9
+ var isMultipleOf = (value, step) => {
10
+ if (step === 0) return false;
11
+ const quotient = value / step;
12
+ const delta = Math.abs(quotient - Math.round(quotient));
13
+ return Number.isFinite(quotient) && delta <= Number.EPSILON * Math.max(1, Math.abs(quotient)) * 16;
14
+ };
15
+ //#endregion
16
+ exports.endsWith = endsWith;
17
+ exports.inRange = inRange;
18
+ exports.isEqual = isEqual;
19
+ exports.isGte = isGte;
20
+ exports.isLte = isLte;
21
+ exports.isMultipleOf = isMultipleOf;
22
+ exports.matchesPattern = matchesPattern;
23
+ exports.startsWith = startsWith;
@@ -0,0 +1,8 @@
1
+ export declare const isEqual: (value: number, exact: number) => boolean;
2
+ export declare const isGte: (value: number, min: number) => boolean;
3
+ export declare const isLte: (value: number, max: number) => boolean;
4
+ export declare const inRange: (value: number, [min, max]: readonly [number, number]) => boolean;
5
+ export declare const startsWith: (value: string, prefix: string) => boolean;
6
+ export declare const endsWith: (value: string, suffix: string) => boolean;
7
+ export declare const matchesPattern: (value: string, pattern: RegExp) => boolean;
8
+ export declare const isMultipleOf: (value: number, step: number) => boolean;