arktype 1.1.0-dev-2 → 2.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/LICENSE +7 -0
  2. package/out/ark.d.ts +24 -0
  3. package/out/ark.js +8 -0
  4. package/out/config.d.ts +1 -0
  5. package/out/config.js +2 -0
  6. package/out/generic.d.ts +54 -0
  7. package/out/generic.js +39 -0
  8. package/out/index.d.ts +6 -0
  9. package/out/index.js +5 -0
  10. package/out/match.d.ts +71 -0
  11. package/out/match.js +50 -0
  12. package/out/module.d.ts +11 -0
  13. package/out/module.js +2 -0
  14. package/{dist → out}/parser/definition.d.ts +16 -16
  15. package/{dist → out}/parser/definition.js +12 -12
  16. package/out/parser/objectLiteral.d.ts +79 -0
  17. package/out/parser/objectLiteral.js +100 -0
  18. package/out/parser/semantic/bounds.d.ts +15 -0
  19. package/{dist → out}/parser/semantic/bounds.js +0 -1
  20. package/out/parser/semantic/default.d.ts +8 -0
  21. package/out/parser/semantic/default.js +1 -0
  22. package/out/parser/semantic/divisor.d.ts +7 -0
  23. package/out/parser/semantic/divisor.js +1 -0
  24. package/out/parser/semantic/infer.d.ts +25 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +4 -0
  27. package/out/parser/semantic/utils.js +1 -0
  28. package/out/parser/semantic/validate.d.ts +30 -0
  29. package/out/parser/semantic/validate.js +2 -0
  30. package/out/parser/string/reduce/dynamic.d.ts +43 -0
  31. package/{dist → out}/parser/string/reduce/dynamic.js +50 -33
  32. package/out/parser/string/reduce/shared.d.ts +42 -0
  33. package/{dist → out}/parser/string/reduce/shared.js +22 -2
  34. package/{dist → out}/parser/string/reduce/static.d.ts +29 -27
  35. package/out/parser/string/reduce/static.js +1 -0
  36. package/out/parser/string/shift/operand/date.d.ts +9 -0
  37. package/{dist → out}/parser/string/shift/operand/date.js +5 -8
  38. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +5 -16
  39. package/{dist → out}/parser/string/shift/operand/enclosed.js +15 -15
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +28 -0
  41. package/out/parser/string/shift/operand/genericArgs.js +16 -0
  42. package/out/parser/string/shift/operand/operand.d.ts +9 -0
  43. package/out/parser/string/shift/operand/operand.js +12 -0
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +30 -0
  45. package/out/parser/string/shift/operand/unenclosed.js +53 -0
  46. package/out/parser/string/shift/operator/bounds.d.ts +27 -0
  47. package/out/parser/string/shift/operator/bounds.js +94 -0
  48. package/out/parser/string/shift/operator/default.d.ts +24 -0
  49. package/out/parser/string/shift/operator/default.js +23 -0
  50. package/{dist → out}/parser/string/shift/operator/divisor.d.ts +2 -4
  51. package/{dist → out}/parser/string/shift/operator/divisor.js +2 -4
  52. package/{dist → out}/parser/string/shift/operator/operator.d.ts +4 -4
  53. package/out/parser/string/shift/operator/operator.js +22 -0
  54. package/{dist → out}/parser/string/shift/scanner.d.ts +13 -17
  55. package/{dist → out}/parser/string/shift/scanner.js +27 -24
  56. package/out/parser/string/string.d.ts +28 -0
  57. package/out/parser/string/string.js +24 -0
  58. package/out/parser/tuple.d.ts +130 -0
  59. package/out/parser/tuple.js +157 -0
  60. package/out/scope.d.ts +84 -0
  61. package/out/scope.js +131 -0
  62. package/out/type.d.ts +79 -0
  63. package/out/type.js +37 -0
  64. package/package.json +37 -41
  65. package/dist/main.d.ts +0 -11
  66. package/dist/main.d.ts.map +0 -1
  67. package/dist/main.js +0 -8
  68. package/dist/main.js.map +0 -1
  69. package/dist/match.d.ts +0 -46
  70. package/dist/match.d.ts.map +0 -1
  71. package/dist/match.js +0 -23
  72. package/dist/match.js.map +0 -1
  73. package/dist/parser/definition.d.ts.map +0 -1
  74. package/dist/parser/definition.js.map +0 -1
  75. package/dist/parser/generic.d.ts +0 -44
  76. package/dist/parser/generic.d.ts.map +0 -1
  77. package/dist/parser/generic.js +0 -46
  78. package/dist/parser/generic.js.map +0 -1
  79. package/dist/parser/objectLiteral.d.ts +0 -24
  80. package/dist/parser/objectLiteral.d.ts.map +0 -1
  81. package/dist/parser/objectLiteral.js +0 -38
  82. package/dist/parser/objectLiteral.js.map +0 -1
  83. package/dist/parser/semantic/bounds.d.ts +0 -13
  84. package/dist/parser/semantic/bounds.d.ts.map +0 -1
  85. package/dist/parser/semantic/bounds.js.map +0 -1
  86. package/dist/parser/semantic/divisor.d.ts +0 -9
  87. package/dist/parser/semantic/divisor.d.ts.map +0 -1
  88. package/dist/parser/semantic/divisor.js +0 -2
  89. package/dist/parser/semantic/divisor.js.map +0 -1
  90. package/dist/parser/semantic/intersections.d.ts +0 -15
  91. package/dist/parser/semantic/intersections.d.ts.map +0 -1
  92. package/dist/parser/semantic/intersections.js +0 -2
  93. package/dist/parser/semantic/intersections.js.map +0 -1
  94. package/dist/parser/semantic/semantic.d.ts +0 -22
  95. package/dist/parser/semantic/semantic.d.ts.map +0 -1
  96. package/dist/parser/semantic/semantic.js +0 -2
  97. package/dist/parser/semantic/semantic.js.map +0 -1
  98. package/dist/parser/semantic/utils.d.ts +0 -8
  99. package/dist/parser/semantic/utils.d.ts.map +0 -1
  100. package/dist/parser/semantic/utils.js +0 -2
  101. package/dist/parser/semantic/utils.js.map +0 -1
  102. package/dist/parser/semantic/validate.d.ts +0 -24
  103. package/dist/parser/semantic/validate.d.ts.map +0 -1
  104. package/dist/parser/semantic/validate.js +0 -2
  105. package/dist/parser/semantic/validate.js.map +0 -1
  106. package/dist/parser/shared.d.ts +0 -48
  107. package/dist/parser/shared.d.ts.map +0 -1
  108. package/dist/parser/shared.js +0 -41
  109. package/dist/parser/shared.js.map +0 -1
  110. package/dist/parser/string/reduce/dynamic.d.ts +0 -40
  111. package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
  112. package/dist/parser/string/reduce/dynamic.js.map +0 -1
  113. package/dist/parser/string/reduce/shared.d.ts +0 -29
  114. package/dist/parser/string/reduce/shared.d.ts.map +0 -1
  115. package/dist/parser/string/reduce/shared.js.map +0 -1
  116. package/dist/parser/string/reduce/static.d.ts.map +0 -1
  117. package/dist/parser/string/reduce/static.js +0 -2
  118. package/dist/parser/string/reduce/static.js.map +0 -1
  119. package/dist/parser/string/shift/operand/date.d.ts +0 -11
  120. package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
  121. package/dist/parser/string/shift/operand/date.js.map +0 -1
  122. package/dist/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  123. package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
  124. package/dist/parser/string/shift/operand/operand.d.ts +0 -9
  125. package/dist/parser/string/shift/operand/operand.d.ts.map +0 -1
  126. package/dist/parser/string/shift/operand/operand.js +0 -17
  127. package/dist/parser/string/shift/operand/operand.js.map +0 -1
  128. package/dist/parser/string/shift/operand/unenclosed.d.ts +0 -36
  129. package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  130. package/dist/parser/string/shift/operand/unenclosed.js +0 -68
  131. package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
  132. package/dist/parser/string/shift/operator/bounds.d.ts +0 -55
  133. package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
  134. package/dist/parser/string/shift/operator/bounds.js +0 -95
  135. package/dist/parser/string/shift/operator/bounds.js.map +0 -1
  136. package/dist/parser/string/shift/operator/divisor.d.ts.map +0 -1
  137. package/dist/parser/string/shift/operator/divisor.js.map +0 -1
  138. package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
  139. package/dist/parser/string/shift/operator/operator.js +0 -29
  140. package/dist/parser/string/shift/operator/operator.js.map +0 -1
  141. package/dist/parser/string/shift/scanner.d.ts.map +0 -1
  142. package/dist/parser/string/shift/scanner.js.map +0 -1
  143. package/dist/parser/string/string.d.ts +0 -26
  144. package/dist/parser/string/string.d.ts.map +0 -1
  145. package/dist/parser/string/string.js +0 -31
  146. package/dist/parser/string/string.js.map +0 -1
  147. package/dist/parser/tuple.d.ts +0 -73
  148. package/dist/parser/tuple.d.ts.map +0 -1
  149. package/dist/parser/tuple.js +0 -134
  150. package/dist/parser/tuple.js.map +0 -1
  151. package/dist/scope.d.ts +0 -159
  152. package/dist/scope.d.ts.map +0 -1
  153. package/dist/scope.js +0 -248
  154. package/dist/scope.js.map +0 -1
  155. package/dist/scopes/ark.d.ts +0 -38
  156. package/dist/scopes/ark.d.ts.map +0 -1
  157. package/dist/scopes/ark.js +0 -24
  158. package/dist/scopes/ark.js.map +0 -1
  159. package/dist/scopes/jsObjects.d.ts +0 -19
  160. package/dist/scopes/jsObjects.d.ts.map +0 -1
  161. package/dist/scopes/jsObjects.js +0 -15
  162. package/dist/scopes/jsObjects.js.map +0 -1
  163. package/dist/scopes/parsing.d.ts +0 -18
  164. package/dist/scopes/parsing.d.ts.map +0 -1
  165. package/dist/scopes/parsing.js +0 -61
  166. package/dist/scopes/parsing.js.map +0 -1
  167. package/dist/scopes/tsGenerics.d.ts +0 -17
  168. package/dist/scopes/tsGenerics.d.ts.map +0 -1
  169. package/dist/scopes/tsGenerics.js +0 -13
  170. package/dist/scopes/tsGenerics.js.map +0 -1
  171. package/dist/scopes/tsKeywords.d.ts +0 -24
  172. package/dist/scopes/tsKeywords.d.ts.map +0 -1
  173. package/dist/scopes/tsKeywords.js +0 -20
  174. package/dist/scopes/tsKeywords.js.map +0 -1
  175. package/dist/scopes/utils/creditCard.d.ts +0 -3
  176. package/dist/scopes/utils/creditCard.d.ts.map +0 -1
  177. package/dist/scopes/utils/creditCard.js +0 -36
  178. package/dist/scopes/utils/creditCard.js.map +0 -1
  179. package/dist/scopes/utils/date.d.ts +0 -24
  180. package/dist/scopes/utils/date.d.ts.map +0 -1
  181. package/dist/scopes/utils/date.js +0 -53
  182. package/dist/scopes/utils/date.js.map +0 -1
  183. package/dist/scopes/validation.d.ts +0 -20
  184. package/dist/scopes/validation.d.ts.map +0 -1
  185. package/dist/scopes/validation.js +0 -57
  186. package/dist/scopes/validation.js.map +0 -1
  187. package/dist/type.d.ts +0 -101
  188. package/dist/type.d.ts.map +0 -1
  189. package/dist/type.js +0 -138
  190. package/dist/type.js.map +0 -1
  191. package/main.ts +0 -20
  192. package/match.ts +0 -137
  193. package/parser/definition.ts +0 -156
  194. package/parser/generic.ts +0 -208
  195. package/parser/objectLiteral.ts +0 -120
  196. package/parser/semantic/bounds.ts +0 -63
  197. package/parser/semantic/divisor.ts +0 -13
  198. package/parser/semantic/intersections.ts +0 -47
  199. package/parser/semantic/semantic.ts +0 -107
  200. package/parser/semantic/utils.ts +0 -26
  201. package/parser/semantic/validate.ts +0 -105
  202. package/parser/shared.ts +0 -141
  203. package/parser/string/reduce/dynamic.ts +0 -181
  204. package/parser/string/reduce/shared.ts +0 -73
  205. package/parser/string/reduce/static.ts +0 -300
  206. package/parser/string/shift/operand/date.ts +0 -57
  207. package/parser/string/shift/operand/enclosed.ts +0 -119
  208. package/parser/string/shift/operand/operand.ts +0 -48
  209. package/parser/string/shift/operand/unenclosed.ts +0 -282
  210. package/parser/string/shift/operator/bounds.ts +0 -229
  211. package/parser/string/shift/operator/divisor.ts +0 -36
  212. package/parser/string/shift/operator/operator.ts +0 -82
  213. package/parser/string/shift/scanner.ts +0 -209
  214. package/parser/string/string.ts +0 -93
  215. package/parser/tuple.ts +0 -475
  216. package/scope.ts +0 -573
  217. package/scopes/ark.ts +0 -64
  218. package/scopes/jsObjects.ts +0 -29
  219. package/scopes/parsing.ts +0 -81
  220. package/scopes/tsGenerics.ts +0 -28
  221. package/scopes/tsKeywords.ts +0 -39
  222. package/scopes/utils/creditCard.ts +0 -37
  223. package/scopes/utils/date.ts +0 -108
  224. package/scopes/validation.ts +0 -81
  225. package/type.ts +0 -403
package/scopes/parsing.ts DELETED
@@ -1,81 +0,0 @@
1
- import { node, type Out } from "@arktype/schema"
2
- import {
3
- wellFormedIntegerMatcher,
4
- wellFormedNumberMatcher
5
- } from "@arktype/util"
6
- import { Scope } from "../scope.js"
7
- import type { RootScope } from "./ark.js"
8
- import { parsedDate } from "./utils/date.js"
9
-
10
- const number = node({
11
- in: {
12
- basis: "string",
13
- pattern: wellFormedNumberMatcher,
14
- description: "a well-formed numeric string"
15
- },
16
- morph: (s: string) => parseFloat(s)
17
- })
18
-
19
- const integer = node({
20
- in: {
21
- basis: "string",
22
- pattern: wellFormedIntegerMatcher
23
- },
24
- morph: (s: string) => {
25
- // if (!isWellFormedInteger(s)) {
26
- // return problems.mustBe("a well-formed integer string")
27
- // }
28
- // const parsed = parseInt(s)
29
- // return Number.isSafeInteger(parsed)
30
- // ? parsed
31
- // : problems.mustBe(
32
- // "an integer in the range Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER"
33
- // )
34
- return parseInt(s)
35
- }
36
- })
37
-
38
- const url = node({
39
- in: {
40
- basis: "string",
41
- description: "a valid URL"
42
- },
43
- morph: (s: string) => {
44
- return new URL(s)
45
- // try {
46
- // return new URL(s)
47
- // } catch {
48
- // return state.mustBe("a valid URL", s, state.basePath)
49
- // }
50
- }
51
- })
52
-
53
- const json = node({
54
- in: {
55
- basis: "string",
56
- description: "a JSON-parsable string"
57
- },
58
- morph: (s: string): unknown => JSON.parse(s)
59
- })
60
-
61
- const date = parsedDate
62
-
63
- export interface InferredParsing {
64
- url: (In: string) => Out<URL>
65
- number: (In: string) => Out<number>
66
- integer: (In: string) => Out<number>
67
- date: (In: string) => Out<Date>
68
- json: (In: string) => Out<unknown>
69
- }
70
-
71
- export const parsing: RootScope<InferredParsing> = Scope.root({
72
- url,
73
- number,
74
- integer,
75
- date,
76
- json
77
- })
78
-
79
- export const parsingModule = parsing.export()
80
-
81
- export type ParsingModule = typeof parsingModule
@@ -1,28 +0,0 @@
1
- import { Scope } from "../scope.js"
2
- import type { Generic } from "../type.js"
3
- import type { RootScope } from "./ark.js"
4
-
5
- export interface InferredTsGenerics {
6
- Record: Generic<
7
- ["K", "V"],
8
- {
9
- "[K]": "V"
10
- },
11
- // as long as the generics in the root scope don't reference one
12
- // another, they shouldn't need a bound local scope
13
- {}
14
- >
15
- }
16
-
17
- export const tsGenerics: RootScope<InferredTsGenerics> = Scope.root({
18
- // "Record<K, V>": {
19
- // // Remove this once we support constraints on generic parameters:
20
- // // https://github.com/arktypeio/arktype/issues/796
21
- // /** @ts-expect-error */
22
- // "[K]": "V"
23
- // }
24
- // unfortunately TS won't let us assign this directly, so we need to be
25
- // careful to keep the inferred types in sync
26
- }) as never // satisfies RootScope<{ [k in keyof InferredTsGenerics]: any }> as never
27
-
28
- export const tsGenericsModule = tsGenerics.export()
@@ -1,39 +0,0 @@
1
- import { node } from "@arktype/schema"
2
- import { Scope } from "../scope.js"
3
- import type { RootScope, type } from "./ark.js"
4
-
5
- export interface InferredTsKeywords {
6
- any: any
7
- bigint: bigint
8
- boolean: boolean
9
- false: false
10
- never: never
11
- null: null
12
- number: number
13
- object: object
14
- string: string
15
- symbol: symbol
16
- true: true
17
- unknown: unknown
18
- void: void
19
- undefined: undefined
20
- }
21
-
22
- export const tsKeywords: RootScope<InferredTsKeywords> = Scope.root({
23
- any: "unknown" as type.cast<any>,
24
- bigint: node("bigint"),
25
- boolean: "true|false",
26
- false: node.units(false),
27
- never: node(),
28
- null: node.units(null),
29
- number: node("number"),
30
- object: node("object"),
31
- string: node("string"),
32
- symbol: node("symbol"),
33
- true: node.units(true),
34
- unknown: node({}),
35
- void: "undefined" as type.cast<void>,
36
- undefined: node.units(undefined)
37
- })
38
-
39
- export const tsKeywordsModule = tsKeywords.export()
@@ -1,37 +0,0 @@
1
- import { node } from "@arktype/schema"
2
-
3
- // https://github.com/validatorjs/validator.js/blob/master/src/lib/isLuhnNumber.js
4
- export const isLuhnValid = (creditCardInput: string) => {
5
- const sanitized = creditCardInput.replace(/[- ]+/g, "")
6
- let sum = 0
7
- let digit
8
- let tmpNum
9
- let shouldDouble
10
- for (let i = sanitized.length - 1; i >= 0; i--) {
11
- digit = sanitized.substring(i, i + 1)
12
- tmpNum = parseInt(digit, 10)
13
- if (shouldDouble) {
14
- tmpNum *= 2
15
- if (tmpNum >= 10) {
16
- sum += (tmpNum % 10) + 1
17
- } else {
18
- sum += tmpNum
19
- }
20
- } else {
21
- sum += tmpNum
22
- }
23
- shouldDouble = !shouldDouble
24
- }
25
- return !!(sum % 10 === 0 ? sanitized : false)
26
- }
27
-
28
- // https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js
29
- const creditCardMatcher =
30
- /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/
31
-
32
- export const creditCard = node({
33
- basis: "string",
34
- pattern: creditCardMatcher,
35
- predicate: isLuhnValid,
36
- description: "a valid credit card number"
37
- })
@@ -1,108 +0,0 @@
1
- import { node } from "@arktype/schema"
2
- import { throwInternalError } from "@arktype/util"
3
-
4
- type DayDelimiter = "." | "/" | "-"
5
-
6
- const dayDelimiterMatcher = /^[./-]$/
7
-
8
- type DayPart = DayPatterns[PartKey]
9
-
10
- type PartKey = keyof DayPatterns
11
-
12
- type DayPatterns = {
13
- y: "yy" | "yyyy"
14
- m: "mm" | "m"
15
- d: "dd" | "d"
16
- }
17
-
18
- type fragment<part extends DayPart, delimiter extends DayDelimiter> =
19
- | `${delimiter}${part}`
20
- | ""
21
-
22
- export type DayPattern<delimiter extends DayDelimiter = DayDelimiter> = {
23
- [d in delimiter]: {
24
- [k1 in keyof DayPatterns]: {
25
- [k2 in Exclude<keyof DayPatterns, k1>]: `${DayPatterns[k1]}${fragment<
26
- DayPatterns[k2],
27
- d
28
- >}${fragment<DayPatterns[Exclude<keyof DayPatterns, k1 | k2>], d>}`
29
- }[Exclude<keyof DayPatterns, k1>]
30
- }[keyof DayPatterns]
31
- }[delimiter]
32
-
33
- export type DateFormat = "iso8601" | DayPattern
34
-
35
- export type DateOptions = {
36
- format?: DateFormat
37
- }
38
-
39
- // ISO 8601 date/time modernized from https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js
40
- // Based on https://tc39.es/ecma262/#sec-date-time-string-format, the T
41
- // delimiter for date/time is mandatory. Regex from validator.js strict matcher:
42
- const iso8601Matcher =
43
- /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/
44
-
45
- type ParsedDayParts = {
46
- y?: string
47
- m?: string
48
- d?: string
49
- }
50
-
51
- const isValidDateInstance = (date: Date) => !isNaN(date as any)
52
-
53
- const writeFormattedMustBe = (format: DateFormat) =>
54
- `a ${format}-formatted date`
55
-
56
- export const tryParseDatePattern = (
57
- data: string,
58
- opts?: DateOptions
59
- ): Date | string => {
60
- if (!opts?.format) {
61
- const result = new Date(data)
62
- return isValidDateInstance(result) ? result : "a valid date"
63
- }
64
- if (opts.format === "iso8601") {
65
- return iso8601Matcher.test(data)
66
- ? new Date(data)
67
- : writeFormattedMustBe("iso8601")
68
- }
69
- const dataParts = data.split(dayDelimiterMatcher)
70
- // will be the first delimiter matched, if there is one
71
- const delimiter: string | undefined = data[dataParts[0].length]
72
- const formatParts = delimiter ? opts.format.split(delimiter) : [opts.format]
73
-
74
- if (dataParts.length !== formatParts.length) {
75
- return writeFormattedMustBe(opts.format)
76
- }
77
-
78
- const parsedParts: ParsedDayParts = {}
79
- for (let i = 0; i < formatParts.length; i++) {
80
- if (
81
- dataParts[i].length !== formatParts[i].length &&
82
- // if format is "m" or "d", data is allowed to be 1 or 2 characters
83
- !(formatParts[i].length === 1 && dataParts[i].length === 2)
84
- ) {
85
- return writeFormattedMustBe(opts.format)
86
- }
87
- parsedParts[formatParts[i][0] as PartKey] = dataParts[i]
88
- }
89
-
90
- const date = new Date(`${parsedParts.m}/${parsedParts.d}/${parsedParts.y}`)
91
-
92
- if (`${date.getDate()}` === parsedParts.d) {
93
- return date
94
- }
95
- return writeFormattedMustBe(opts.format)
96
- }
97
-
98
- export const parsedDate = node({
99
- in: "string",
100
- morph: (s: string) => {
101
- const result = tryParseDatePattern(s)
102
- return typeof result === "string"
103
- ? // TODO: Fix
104
- throwInternalError("Unsupported")
105
- : //state.mustBe(result, s, state.basePath)
106
- result
107
- }
108
- })
@@ -1,81 +0,0 @@
1
- import { node } from "@arktype/schema"
2
- import { Scope } from "../scope.js"
3
- import type { RootScope } from "./ark.js"
4
- import { creditCard } from "./utils/creditCard.js"
5
-
6
- // Non-trivial expressions should have an explanation or attribution
7
-
8
- const url = node({
9
- basis: "string",
10
- predicate: (s: string) => {
11
- try {
12
- new URL(s)
13
- } catch {
14
- return false
15
- }
16
- return true
17
- },
18
- description: "a valid URL"
19
- })
20
-
21
- // https://www.regular-expressions.info/email.html
22
- const emailMatcher = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/
23
-
24
- const email = node({
25
- basis: "string",
26
- pattern: emailMatcher,
27
- description: "a valid email"
28
- })
29
-
30
- const uuidMatcher =
31
- /^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/
32
-
33
- // https://github.com/validatorjs/validator.js/blob/master/src/lib/isUUID.js
34
- const uuid = node({
35
- basis: "string",
36
- pattern: uuidMatcher,
37
- description: "a valid UUID"
38
- })
39
-
40
- const semverMatcher =
41
- /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
42
-
43
- // https://semver.org/
44
- const semver = node({
45
- basis: "string",
46
- pattern: semverMatcher,
47
- description: "a valid semantic version (see https://semver.org/)"
48
- })
49
-
50
- export interface InferredValidation {
51
- alpha: string
52
- alphanumeric: string
53
- lowercase: string
54
- uppercase: string
55
- creditCard: string
56
- email: string
57
- uuid: string
58
- url: string
59
- semver: string
60
- integer: number
61
- }
62
-
63
- export const validation: RootScope<InferredValidation> = Scope.root({
64
- // Character sets
65
- alpha: [/^[A-Za-z]*$/, "@", "only letters"],
66
- alphanumeric: [/^[A-Za-z\d]*$/, "@", "only letters and digits"],
67
- lowercase: [/^[a-z]*$/, "@", "only lowercase letters"],
68
- uppercase: [/^[A-Z]*$/, "@", "only uppercase letters"],
69
- creditCard,
70
- email,
71
- uuid,
72
- url,
73
- semver,
74
- integer: node({
75
- basis: "number",
76
- divisor: 1,
77
- description: "an integer"
78
- })
79
- })
80
-
81
- export const validationModule = validation.export()