@palmares/schemas 0.1.24 → 0.1.26

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 (305) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/src/adapter/fields/array.cjs +96 -0
  3. package/dist/src/adapter/fields/array.d.ts.map +1 -0
  4. package/dist/src/adapter/fields/array.js +70 -0
  5. package/dist/src/adapter/fields/boolean.cjs +102 -0
  6. package/dist/src/adapter/fields/boolean.d.ts.map +1 -0
  7. package/dist/src/adapter/fields/boolean.js +76 -0
  8. package/dist/src/adapter/fields/datetime.cjs +102 -0
  9. package/dist/src/adapter/fields/datetime.d.ts.map +1 -0
  10. package/dist/src/adapter/fields/datetime.js +76 -0
  11. package/dist/src/adapter/fields/index.cjs +87 -0
  12. package/dist/src/adapter/fields/index.d.ts.map +1 -0
  13. package/dist/src/adapter/fields/index.js +61 -0
  14. package/dist/src/adapter/fields/number.cjs +102 -0
  15. package/dist/src/adapter/fields/number.d.ts.map +1 -0
  16. package/dist/src/adapter/fields/number.js +76 -0
  17. package/dist/src/adapter/fields/object.cjs +108 -0
  18. package/dist/src/adapter/fields/object.d.ts.map +1 -0
  19. package/dist/src/adapter/fields/object.js +82 -0
  20. package/dist/src/adapter/fields/string.cjs +102 -0
  21. package/dist/src/adapter/fields/string.d.ts.map +1 -0
  22. package/dist/src/adapter/fields/string.js +76 -0
  23. package/dist/src/adapter/fields/union.cjs +102 -0
  24. package/dist/src/adapter/fields/union.d.ts.map +1 -0
  25. package/dist/src/adapter/fields/union.js +76 -0
  26. package/dist/src/adapter/index.cjs +65 -0
  27. package/dist/src/adapter/index.d.ts.map +1 -0
  28. package/dist/src/adapter/index.js +40 -0
  29. package/dist/src/adapter/types.cjs +18 -0
  30. package/dist/src/adapter/types.d.ts.map +1 -0
  31. package/dist/src/adapter/types.js +0 -0
  32. package/dist/src/compile.cjs +59 -0
  33. package/dist/src/compile.d.ts.map +1 -0
  34. package/dist/src/compile.js +36 -0
  35. package/dist/src/conf.cjs +53 -0
  36. package/dist/src/conf.d.ts.map +1 -0
  37. package/dist/src/conf.js +27 -0
  38. package/dist/src/constants.cjs +40 -0
  39. package/dist/src/constants.d.ts.map +1 -0
  40. package/dist/src/constants.js +14 -0
  41. package/dist/src/domain.cjs +63 -0
  42. package/dist/src/domain.d.ts.map +1 -0
  43. package/dist/src/domain.js +40 -0
  44. package/dist/src/exceptions.cjs +58 -0
  45. package/dist/src/exceptions.d.ts.map +1 -0
  46. package/dist/src/exceptions.js +33 -0
  47. package/dist/src/index.cjs +6520 -0
  48. package/dist/src/index.d.ts.map +1 -0
  49. package/dist/src/index.js +6456 -0
  50. package/dist/src/middleware.cjs +58 -0
  51. package/dist/src/middleware.d.ts.map +1 -0
  52. package/dist/src/middleware.js +35 -0
  53. package/dist/src/model.cjs +6003 -0
  54. package/dist/src/model.d.ts.map +1 -0
  55. package/dist/src/model.js +5978 -0
  56. package/dist/src/parsers/convert-from-number.cjs +34 -0
  57. package/dist/src/parsers/convert-from-number.d.ts.map +1 -0
  58. package/dist/src/parsers/convert-from-number.js +11 -0
  59. package/dist/src/parsers/convert-from-string.cjs +40 -0
  60. package/dist/src/parsers/convert-from-string.d.ts.map +1 -0
  61. package/dist/src/parsers/convert-from-string.js +17 -0
  62. package/dist/src/parsers/index.cjs +50 -0
  63. package/dist/src/parsers/index.d.ts.map +1 -0
  64. package/dist/src/parsers/index.js +24 -0
  65. package/dist/src/schema/array.cjs +1863 -0
  66. package/dist/src/schema/array.d.ts.map +1 -0
  67. package/dist/src/schema/array.js +1837 -0
  68. package/dist/src/schema/boolean.cjs +1800 -0
  69. package/dist/src/schema/boolean.d.ts.map +1 -0
  70. package/dist/src/schema/boolean.js +1774 -0
  71. package/dist/src/schema/datetime.cjs +1789 -0
  72. package/dist/src/schema/datetime.d.ts.map +1 -0
  73. package/dist/src/schema/datetime.js +1763 -0
  74. package/dist/src/schema/index.cjs +2026 -0
  75. package/dist/src/schema/index.d.ts.map +1 -0
  76. package/dist/src/schema/index.js +2001 -0
  77. package/dist/src/schema/number.cjs +2026 -0
  78. package/dist/src/schema/number.d.ts.map +1 -0
  79. package/dist/src/schema/number.js +2000 -0
  80. package/dist/src/schema/object.cjs +1795 -0
  81. package/dist/src/schema/object.d.ts.map +1 -0
  82. package/dist/src/schema/object.js +1769 -0
  83. package/dist/src/schema/schema.cjs +844 -0
  84. package/dist/src/schema/schema.d.ts.map +1 -0
  85. package/dist/src/schema/schema.js +818 -0
  86. package/dist/src/schema/string.cjs +2126 -0
  87. package/dist/src/schema/string.d.ts.map +1 -0
  88. package/dist/src/schema/string.js +2100 -0
  89. package/dist/src/schema/types.cjs +18 -0
  90. package/dist/src/schema/types.d.ts.map +1 -0
  91. package/dist/src/schema/types.js +0 -0
  92. package/dist/src/schema/union.cjs +1737 -0
  93. package/dist/src/schema/union.d.ts.map +1 -0
  94. package/dist/src/schema/union.js +1711 -0
  95. package/dist/src/types.cjs +18 -0
  96. package/dist/src/types.d.ts.map +1 -0
  97. package/dist/src/types.js +0 -0
  98. package/dist/src/utils.cjs +477 -0
  99. package/dist/src/utils.d.ts.map +1 -0
  100. package/dist/src/utils.js +445 -0
  101. package/dist/src/validators/array.cjs +176 -0
  102. package/dist/src/validators/array.d.ts.map +1 -0
  103. package/dist/src/validators/array.js +150 -0
  104. package/dist/src/validators/boolean.cjs +71 -0
  105. package/dist/src/validators/boolean.d.ts.map +1 -0
  106. package/dist/src/validators/boolean.js +47 -0
  107. package/dist/src/validators/datetime.cjs +129 -0
  108. package/dist/src/validators/datetime.d.ts.map +1 -0
  109. package/dist/src/validators/datetime.js +103 -0
  110. package/dist/src/validators/number.cjs +194 -0
  111. package/dist/src/validators/number.d.ts.map +1 -0
  112. package/dist/src/validators/number.js +166 -0
  113. package/dist/src/validators/object.cjs +69 -0
  114. package/dist/src/validators/object.d.ts.map +1 -0
  115. package/dist/src/validators/object.js +46 -0
  116. package/dist/src/validators/schema.cjs +158 -0
  117. package/dist/src/validators/schema.d.ts.map +1 -0
  118. package/dist/src/validators/schema.js +132 -0
  119. package/dist/src/validators/string.cjs +255 -0
  120. package/dist/src/validators/string.d.ts.map +1 -0
  121. package/dist/src/validators/string.js +224 -0
  122. package/dist/src/validators/types.cjs +18 -0
  123. package/dist/src/validators/types.d.ts.map +1 -0
  124. package/dist/src/validators/types.js +0 -0
  125. package/dist/src/validators/union.cjs +69 -0
  126. package/dist/src/validators/union.d.ts.map +1 -0
  127. package/dist/src/validators/union.js +46 -0
  128. package/dist/src/validators/utils.cjs +165 -0
  129. package/dist/src/validators/utils.d.ts.map +1 -0
  130. package/dist/src/validators/utils.js +142 -0
  131. package/dist/tsconfig.types.tsbuildinfo +1 -0
  132. package/package.json +15 -23
  133. package/dist/cjs/src/adapter/fields/array.js +0 -152
  134. package/dist/cjs/src/adapter/fields/boolean.js +0 -162
  135. package/dist/cjs/src/adapter/fields/datetime.js +0 -162
  136. package/dist/cjs/src/adapter/fields/index.js +0 -311
  137. package/dist/cjs/src/adapter/fields/number.js +0 -162
  138. package/dist/cjs/src/adapter/fields/object.js +0 -171
  139. package/dist/cjs/src/adapter/fields/string.js +0 -162
  140. package/dist/cjs/src/adapter/fields/union.js +0 -162
  141. package/dist/cjs/src/adapter/index.js +0 -201
  142. package/dist/cjs/src/adapter/types.js +0 -4
  143. package/dist/cjs/src/compile.js +0 -262
  144. package/dist/cjs/src/conf.js +0 -26
  145. package/dist/cjs/src/constants.js +0 -36
  146. package/dist/cjs/src/domain.js +0 -156
  147. package/dist/cjs/src/exceptions.js +0 -168
  148. package/dist/cjs/src/index.js +0 -317
  149. package/dist/cjs/src/middleware.js +0 -213
  150. package/dist/cjs/src/model.js +0 -628
  151. package/dist/cjs/src/parsers/convert-from-number.js +0 -15
  152. package/dist/cjs/src/parsers/convert-from-string.js +0 -19
  153. package/dist/cjs/src/parsers/index.js +0 -20
  154. package/dist/cjs/src/schema/array.js +0 -1014
  155. package/dist/cjs/src/schema/boolean.js +0 -883
  156. package/dist/cjs/src/schema/datetime.js +0 -840
  157. package/dist/cjs/src/schema/index.js +0 -12
  158. package/dist/cjs/src/schema/number.js +0 -976
  159. package/dist/cjs/src/schema/object.js +0 -1052
  160. package/dist/cjs/src/schema/schema.js +0 -1840
  161. package/dist/cjs/src/schema/string.js +0 -1038
  162. package/dist/cjs/src/schema/types.js +0 -4
  163. package/dist/cjs/src/schema/union.js +0 -1070
  164. package/dist/cjs/src/types.js +0 -4
  165. package/dist/cjs/src/utils.js +0 -647
  166. package/dist/cjs/src/validators/array.js +0 -462
  167. package/dist/cjs/src/validators/boolean.js +0 -201
  168. package/dist/cjs/src/validators/datetime.js +0 -291
  169. package/dist/cjs/src/validators/number.js +0 -375
  170. package/dist/cjs/src/validators/object.js +0 -291
  171. package/dist/cjs/src/validators/schema.js +0 -322
  172. package/dist/cjs/src/validators/string.js +0 -467
  173. package/dist/cjs/src/validators/types.js +0 -4
  174. package/dist/cjs/src/validators/union.js +0 -233
  175. package/dist/cjs/src/validators/utils.js +0 -499
  176. package/dist/cjs/tsconfig.types.tsbuildinfo +0 -1
  177. package/dist/cjs/types/adapter/fields/array.d.ts.map +0 -1
  178. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +0 -1
  179. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +0 -1
  180. package/dist/cjs/types/adapter/fields/index.d.ts.map +0 -1
  181. package/dist/cjs/types/adapter/fields/number.d.ts.map +0 -1
  182. package/dist/cjs/types/adapter/fields/object.d.ts.map +0 -1
  183. package/dist/cjs/types/adapter/fields/string.d.ts.map +0 -1
  184. package/dist/cjs/types/adapter/fields/union.d.ts.map +0 -1
  185. package/dist/cjs/types/adapter/index.d.ts.map +0 -1
  186. package/dist/cjs/types/adapter/types.d.ts.map +0 -1
  187. package/dist/cjs/types/compile.d.ts.map +0 -1
  188. package/dist/cjs/types/conf.d.ts.map +0 -1
  189. package/dist/cjs/types/constants.d.ts.map +0 -1
  190. package/dist/cjs/types/domain.d.ts.map +0 -1
  191. package/dist/cjs/types/exceptions.d.ts.map +0 -1
  192. package/dist/cjs/types/index.d.ts.map +0 -1
  193. package/dist/cjs/types/middleware.d.ts.map +0 -1
  194. package/dist/cjs/types/model.d.ts.map +0 -1
  195. package/dist/cjs/types/parsers/convert-from-number.d.ts.map +0 -1
  196. package/dist/cjs/types/parsers/convert-from-string.d.ts.map +0 -1
  197. package/dist/cjs/types/parsers/index.d.ts.map +0 -1
  198. package/dist/cjs/types/schema/array.d.ts.map +0 -1
  199. package/dist/cjs/types/schema/boolean.d.ts.map +0 -1
  200. package/dist/cjs/types/schema/datetime.d.ts.map +0 -1
  201. package/dist/cjs/types/schema/index.d.ts.map +0 -1
  202. package/dist/cjs/types/schema/number.d.ts.map +0 -1
  203. package/dist/cjs/types/schema/object.d.ts.map +0 -1
  204. package/dist/cjs/types/schema/schema.d.ts.map +0 -1
  205. package/dist/cjs/types/schema/string.d.ts.map +0 -1
  206. package/dist/cjs/types/schema/types.d.ts.map +0 -1
  207. package/dist/cjs/types/schema/union.d.ts.map +0 -1
  208. package/dist/cjs/types/types.d.ts.map +0 -1
  209. package/dist/cjs/types/utils.d.ts.map +0 -1
  210. package/dist/cjs/types/validators/array.d.ts.map +0 -1
  211. package/dist/cjs/types/validators/boolean.d.ts.map +0 -1
  212. package/dist/cjs/types/validators/datetime.d.ts.map +0 -1
  213. package/dist/cjs/types/validators/number.d.ts.map +0 -1
  214. package/dist/cjs/types/validators/object.d.ts.map +0 -1
  215. package/dist/cjs/types/validators/schema.d.ts.map +0 -1
  216. package/dist/cjs/types/validators/string.d.ts.map +0 -1
  217. package/dist/cjs/types/validators/types.d.ts.map +0 -1
  218. package/dist/cjs/types/validators/union.d.ts.map +0 -1
  219. package/dist/cjs/types/validators/utils.d.ts.map +0 -1
  220. package/dist/esm/src/adapter/fields/array.js +0 -13
  221. package/dist/esm/src/adapter/fields/boolean.js +0 -20
  222. package/dist/esm/src/adapter/fields/datetime.js +0 -20
  223. package/dist/esm/src/adapter/fields/index.js +0 -37
  224. package/dist/esm/src/adapter/fields/number.js +0 -20
  225. package/dist/esm/src/adapter/fields/object.js +0 -26
  226. package/dist/esm/src/adapter/fields/string.js +0 -20
  227. package/dist/esm/src/adapter/fields/union.js +0 -20
  228. package/dist/esm/src/adapter/index.js +0 -21
  229. package/dist/esm/src/adapter/types.js +0 -1
  230. package/dist/esm/src/compile.js +0 -10
  231. package/dist/esm/src/conf.js +0 -18
  232. package/dist/esm/src/constants.js +0 -4
  233. package/dist/esm/src/domain.js +0 -11
  234. package/dist/esm/src/exceptions.js +0 -15
  235. package/dist/esm/src/index.js +0 -126
  236. package/dist/esm/src/middleware.js +0 -29
  237. package/dist/esm/src/model.js +0 -264
  238. package/dist/esm/src/parsers/convert-from-number.js +0 -8
  239. package/dist/esm/src/parsers/convert-from-string.js +0 -14
  240. package/dist/esm/src/parsers/index.js +0 -2
  241. package/dist/esm/src/schema/array.js +0 -487
  242. package/dist/esm/src/schema/boolean.js +0 -520
  243. package/dist/esm/src/schema/datetime.js +0 -477
  244. package/dist/esm/src/schema/index.js +0 -3
  245. package/dist/esm/src/schema/number.js +0 -610
  246. package/dist/esm/src/schema/object.js +0 -521
  247. package/dist/esm/src/schema/schema.js +0 -767
  248. package/dist/esm/src/schema/string.js +0 -668
  249. package/dist/esm/src/schema/types.js +0 -1
  250. package/dist/esm/src/schema/union.js +0 -468
  251. package/dist/esm/src/types.js +0 -14
  252. package/dist/esm/src/utils.js +0 -207
  253. package/dist/esm/src/validators/array.js +0 -140
  254. package/dist/esm/src/validators/boolean.js +0 -37
  255. package/dist/esm/src/validators/datetime.js +0 -89
  256. package/dist/esm/src/validators/number.js +0 -148
  257. package/dist/esm/src/validators/object.js +0 -39
  258. package/dist/esm/src/validators/schema.js +0 -118
  259. package/dist/esm/src/validators/string.js +0 -202
  260. package/dist/esm/src/validators/types.js +0 -1
  261. package/dist/esm/src/validators/union.js +0 -39
  262. package/dist/esm/src/validators/utils.js +0 -147
  263. /package/dist/{cjs/types → src}/adapter/fields/array.d.ts +0 -0
  264. /package/dist/{cjs/types → src}/adapter/fields/boolean.d.ts +0 -0
  265. /package/dist/{cjs/types → src}/adapter/fields/datetime.d.ts +0 -0
  266. /package/dist/{cjs/types → src}/adapter/fields/index.d.ts +0 -0
  267. /package/dist/{cjs/types → src}/adapter/fields/number.d.ts +0 -0
  268. /package/dist/{cjs/types → src}/adapter/fields/object.d.ts +0 -0
  269. /package/dist/{cjs/types → src}/adapter/fields/string.d.ts +0 -0
  270. /package/dist/{cjs/types → src}/adapter/fields/union.d.ts +0 -0
  271. /package/dist/{cjs/types → src}/adapter/index.d.ts +0 -0
  272. /package/dist/{cjs/types → src}/adapter/types.d.ts +0 -0
  273. /package/dist/{cjs/types → src}/compile.d.ts +0 -0
  274. /package/dist/{cjs/types → src}/conf.d.ts +0 -0
  275. /package/dist/{cjs/types → src}/constants.d.ts +0 -0
  276. /package/dist/{cjs/types → src}/domain.d.ts +0 -0
  277. /package/dist/{cjs/types → src}/exceptions.d.ts +0 -0
  278. /package/dist/{cjs/types → src}/index.d.ts +0 -0
  279. /package/dist/{cjs/types → src}/middleware.d.ts +0 -0
  280. /package/dist/{cjs/types → src}/model.d.ts +0 -0
  281. /package/dist/{cjs/types → src}/parsers/convert-from-number.d.ts +0 -0
  282. /package/dist/{cjs/types → src}/parsers/convert-from-string.d.ts +0 -0
  283. /package/dist/{cjs/types → src}/parsers/index.d.ts +0 -0
  284. /package/dist/{cjs/types → src}/schema/array.d.ts +0 -0
  285. /package/dist/{cjs/types → src}/schema/boolean.d.ts +0 -0
  286. /package/dist/{cjs/types → src}/schema/datetime.d.ts +0 -0
  287. /package/dist/{cjs/types → src}/schema/index.d.ts +0 -0
  288. /package/dist/{cjs/types → src}/schema/number.d.ts +0 -0
  289. /package/dist/{cjs/types → src}/schema/object.d.ts +0 -0
  290. /package/dist/{cjs/types → src}/schema/schema.d.ts +0 -0
  291. /package/dist/{cjs/types → src}/schema/string.d.ts +0 -0
  292. /package/dist/{cjs/types → src}/schema/types.d.ts +0 -0
  293. /package/dist/{cjs/types → src}/schema/union.d.ts +0 -0
  294. /package/dist/{cjs/types → src}/types.d.ts +0 -0
  295. /package/dist/{cjs/types → src}/utils.d.ts +0 -0
  296. /package/dist/{cjs/types → src}/validators/array.d.ts +0 -0
  297. /package/dist/{cjs/types → src}/validators/boolean.d.ts +0 -0
  298. /package/dist/{cjs/types → src}/validators/datetime.d.ts +0 -0
  299. /package/dist/{cjs/types → src}/validators/number.d.ts +0 -0
  300. /package/dist/{cjs/types → src}/validators/object.d.ts +0 -0
  301. /package/dist/{cjs/types → src}/validators/schema.d.ts +0 -0
  302. /package/dist/{cjs/types → src}/validators/string.d.ts +0 -0
  303. /package/dist/{cjs/types → src}/validators/types.d.ts +0 -0
  304. /package/dist/{cjs/types → src}/validators/union.d.ts +0 -0
  305. /package/dist/{cjs/types → src}/validators/utils.d.ts +0 -0
@@ -1,668 +0,0 @@
1
- import { Schema } from './schema';
2
- import { defaultTransform, defaultTransformToAdapter } from '../utils';
3
- import { is, nullable, optional } from '../validators/schema';
4
- import { email, endsWith, includes, maxLength, minLength, regex, startsWith, stringValidation, uuid } from '../validators/string';
5
- export class StringSchema extends Schema {
6
- fieldType = 'string';
7
- __is;
8
- __email;
9
- __uuid;
10
- __minLength;
11
- __maxLength;
12
- __regex;
13
- __endsWith;
14
- __startsWith;
15
- __includes;
16
- __type = {
17
- message: 'Invalid type',
18
- check: (value)=>{
19
- return typeof value === 'string';
20
- }
21
- };
22
- async __transformToAdapter(options) {
23
- return defaultTransformToAdapter(async (adapter)=>{
24
- return defaultTransform('string', this, adapter, adapter.string, ()=>({
25
- type: this.__type,
26
- is: this.__is,
27
- email: this.__email,
28
- uuid: this.__uuid,
29
- minLength: this.__minLength,
30
- maxLength: this.__maxLength,
31
- regex: this.__regex,
32
- endsWith: this.__endsWith,
33
- startsWith: this.__startsWith,
34
- includes: this.__includes,
35
- nullable: this.__nullable,
36
- optional: this.__optional,
37
- parsers: {
38
- nullable: this.__nullable.allow,
39
- optional: this.__optional.allow
40
- }
41
- }), {
42
- maxLength,
43
- minLength,
44
- endsWith,
45
- startsWith,
46
- email,
47
- uuid,
48
- is,
49
- regex,
50
- includes,
51
- nullable,
52
- optional
53
- }, {
54
- validatorsIfFallbackOrNotSupported: stringValidation(),
55
- shouldAddStringVersion: options.shouldAddStringVersion,
56
- // eslint-disable-next-line ts/require-await
57
- fallbackIfNotSupported: async ()=>{
58
- return [];
59
- }
60
- });
61
- }, this, this.__transformedSchemas, options, 'number');
62
- }
63
- /**
64
- * This let's you refine the schema with custom validations. This is useful when you want to validate something that
65
- * is not supported by default by the schema adapter.
66
- *
67
- * @example
68
- * ```typescript
69
- * import * as p from '@palmares/schemas';
70
- *
71
- * const numberSchema = p.number().refine((value) => {
72
- * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
73
- * });
74
- *
75
- * const { errors, parsed } = await numberSchema.parse(-1);
76
- *
77
- * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
78
- * console.log(errors);
79
- * ```
80
- *
81
- * @param refinementCallback - The callback that will be called to validate the value.
82
- * @param options - Options for the refinement.
83
- * @param options.isAsync - Whether the callback is async or not. Defaults to true.
84
- */ refine(refinementCallback) {
85
- return super.refine(refinementCallback);
86
- }
87
- /**
88
- * Allows the value to be either undefined or null.
89
- *
90
- * @example
91
- * ```typescript
92
- * import * as p from '@palmares/schemas';
93
- *
94
- * const numberSchema = p.number().optional();
95
- *
96
- * const { errors, parsed } = await numberSchema.parse(undefined);
97
- *
98
- * console.log(parsed); // undefined
99
- *
100
- * const { errors, parsed } = await numberSchema.parse(null);
101
- *
102
- * console.log(parsed); // null
103
- *
104
- * const { errors, parsed } = await numberSchema.parse(1);
105
- *
106
- * console.log(parsed); // 1
107
- * ```
108
- *
109
- * @returns - The schema we are working with.
110
- */ optional() {
111
- return super.optional();
112
- }
113
- /**
114
- * Just adds a message when the value is undefined. It's just a syntax sugar for
115
- *
116
- * ```typescript
117
- * p.string().optional({ message: 'This value cannot be null', allow: false })
118
- * ```
119
- *
120
- * @param options - The options of nonOptional function
121
- * @param options.message - A custom message if the value is undefined.
122
- *
123
- * @returns - The schema.
124
- */ nonOptional(options) {
125
- return super.optional({
126
- message: options?.message,
127
- allow: false
128
- });
129
- }
130
- /**
131
- * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
132
- * is NULL by setting the { message: 'Your custom message', allow: false } on the options.
133
- *
134
- * @example
135
- * ```typescript
136
- * import * as p from '@palmares/schemas';
137
- *
138
- * const numberSchema = p.number().nullable();
139
- *
140
- * const { errors, parsed } = await numberSchema.parse(null);
141
- *
142
- * console.log(parsed); // null
143
- *
144
- * const { errors, parsed } = await numberSchema.parse(undefined);
145
- *
146
- * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
147
- * ```
148
- *
149
- * @param options - The options for the nullable function.
150
- * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
151
- * @param options.allow - Whether the value can be null or not. Defaults to true.
152
- *
153
- * @returns The schema.
154
- */ nullable(options) {
155
- return super.nullable(options);
156
- }
157
- /**
158
- * Just adds a message when the value is null. It's just a syntax sugar for
159
- *
160
- * ```typescript
161
- * p.string().nullable({ message: 'This value cannot be null', allow: false })
162
- * ```
163
- *
164
- * @param options - The options of nonNullable function
165
- * @param options.message - A custom message if the value is null.
166
- *
167
- * @returns - The schema.
168
- */ nonNullable(options) {
169
- return super.nullable({
170
- message: options?.message || '',
171
- allow: false
172
- });
173
- }
174
- /**
175
- * This method will remove the value from the representation of the schema. If the value is undefined it will keep
176
- * that way otherwise it will set the value to undefined after it's validated.
177
- * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
178
- * function. This will remove the value from the representation of the schema.
179
- *
180
- * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
181
- * function. But if you want to remove the value from the internal representation, you can pass the argument
182
- * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
183
- * the argument `toRepresentation` as true as well.
184
- *
185
- * @example
186
- * ```typescript
187
- * import * as p from '@palmares/schemas';
188
- *
189
- * const userSchema = p.object({
190
- * id: p.number().optional(),
191
- * name: p.string(),
192
- * password: p.string().omit()
193
- * });
194
- *
195
- * const user = await userSchema.data({
196
- * id: 1,
197
- * name: 'John Doe',
198
- * password: '123456'
199
- * });
200
- *
201
- * console.log(user); // { id: 1, name: 'John Doe' }
202
- * ```
203
- *
204
- *
205
- * @param args - By default, the value will be removed just from the representation, in other words, when you call
206
- * the {@link data} function. But if you want to remove the value from the internal representation, you can pass the
207
- * argument `toInternal` as true. Then if you still want to remove the value from the representation, you will need
208
- * to pass the argument `toRepresentation` as true as well.
209
- *
210
- * @returns The schema.
211
- */ omit(args) {
212
- return super.omit(args);
213
- }
214
- /**
215
- * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
216
- * source like a database. You should always return the schema after you save the value, that way we will always have
217
- * the correct type of the schema after the save operation.
218
- *
219
- * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
220
- *
221
- * @example
222
- * ```typescript
223
- * import * as p from '@palmares/schemas';
224
- *
225
- * import { User } from './models';
226
- *
227
- * const userSchema = p.object({
228
- * id: p.number().optional(),
229
- * name: p.string(),
230
- * email: p.string().email(),
231
- * }).onSave(async (value) => {
232
- * // Create or update the user on the database using palmares models or any other library of your choice.
233
- * if (value.id)
234
- * await User.default.set(value, { search: { id: value.id } });
235
- * else
236
- * await User.default.set(value);
237
- *
238
- * return value;
239
- * });
240
- *
241
- *
242
- * // Then, on your controller, do something like this:
243
- * const { isValid, save, errors } = await userSchema.validate(req.body);
244
- * if (isValid) {
245
- * const savedValue = await save();
246
- * return Response.json(savedValue, { status: 201 });
247
- * }
248
- *
249
- * return Response.json({ errors }, { status: 400 });
250
- * ```
251
- *
252
- * @param callback - The callback that will be called to save the value on an external source.
253
- *
254
- * @returns The schema.
255
- */ onSave(callback) {
256
- return super.onSave(callback);
257
- }
258
- /**
259
- * This function is used to add a default value to the schema. If the value is either undefined or null, the default
260
- * value will be used.
261
- *
262
- * @example
263
- * ```typescript
264
- * import * as p from '@palmares/schemas';
265
- *
266
- * const numberSchema = p.number().default(0);
267
- *
268
- * const { errors, parsed } = await numberSchema.parse(undefined);
269
- *
270
- * console.log(parsed); // 0
271
- * ```
272
- */ default(defaultValueOrFunction) {
273
- return super.default(defaultValueOrFunction);
274
- }
275
- /**
276
- * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
277
- * this function to let you customize the custom schema your own way. Our API does not support passthrough?
278
- * No problem, you can use this function to customize the zod schema.
279
- *
280
- * @example
281
- * ```typescript
282
- * import * as p from '@palmares/schemas';
283
- *
284
- * const numberSchema = p.number().extends((schema) => {
285
- * return schema.nonnegative();
286
- * });
287
- *
288
- * const { errors, parsed } = await numberSchema.parse(-1);
289
- * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
290
- * console.log(errors);
291
- * ```
292
- *
293
- * @param callback - The callback that will be called to customize the schema.
294
- * @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
295
- * compile the underlying schema to a string so you can save it for future runs.
296
- *
297
- * @returns The schema.
298
- */ extends(callback, toStringCallback) {
299
- return super.extends(callback, toStringCallback);
300
- }
301
- /**
302
- * This function is used to transform the value to the representation of the schema. When using the {@link data}
303
- * function. With this function you have full control to add data cleaning for example, transforming the data and
304
- * whatever. Another use case is when you want to return deeply nested recursive data.
305
- * The schema maps to itself.
306
- *
307
- * @example
308
- * ```typescript
309
- * import * as p from '@palmares/schemas';
310
- *
311
- * const recursiveSchema = p.object({
312
- * id: p.number().optional(),
313
- * name: p.string(),
314
- * }).toRepresentation(async (value) => {
315
- * return {
316
- * id: value.id,
317
- * name: value.name,
318
- * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
319
- * }
320
- * });
321
- *
322
- * const data = await recursiveSchema.data({
323
- * id: 1,
324
- * name: 'John Doe',
325
- * });
326
- * ```
327
- *
328
- * @example
329
- * ```
330
- * import * as p from '@palmares/schemas';
331
- *
332
- * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
333
- * switch (value) {
334
- * case 'red': return { r: 255, g: 0, b: 0 };
335
- * case 'green': return { r: 0, g: 255, b: 0 };
336
- * case 'blue': return { r: 0, g: 0, b: 255 };
337
- * default: return { r: 0, g: 0, b: 0 };
338
- * }
339
- * });
340
- * ```
341
- * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
342
- *
343
- * @returns The schema with a new return type
344
- */ toRepresentation(toRepresentationCallback) {
345
- return super.toRepresentation(toRepresentationCallback);
346
- }
347
- /**
348
- * This function is used to transform the value to the internal representation of the schema. This is useful when
349
- * you want to transform the value to a type that the schema adapter can understand. For example, you might want
350
- * to transform a string to a date. This is the function you use.
351
- *
352
- * @example
353
- * ```typescript
354
- * import * as p from '@palmares/schemas';
355
- *
356
- * const dateSchema = p.string().toInternal((value) => {
357
- * return new Date(value);
358
- * });
359
- *
360
- * const date = await dateSchema.parse('2021-01-01');
361
- *
362
- * console.log(date); // Date object
363
- *
364
- * const rgbToColorSchema = p.object({
365
- * r: p.number().min(0).max(255),
366
- * g: p.number().min(0).max(255),
367
- * b: p.number().min(0).max(255),
368
- * }).toInternal(async (value) => {
369
- * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
370
- * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
371
- * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
372
- * return `rgb(${value.r}, ${value.g}, ${value.b})`;
373
- * });
374
- * ```
375
- *
376
- * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
377
- *
378
- * @returns The schema with a new return type.
379
- */ toInternal(toInternalCallback) {
380
- return super.toInternal(toInternalCallback);
381
- }
382
- /**
383
- * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
384
- * you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a
385
- * type that the schema adapter can understand.
386
- *
387
- * @example
388
- * ```typescript
389
- * import * as p from '@palmares/schemas';
390
- * import * as z from 'zod';
391
- *
392
- * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
393
- * return new Map(value); // Before validating we transform the value to a map.
394
- * });
395
- *
396
- * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
397
- * ```
398
- *
399
- * @param toValidateCallback - The callback that will be called to validate the value.
400
- *
401
- * @returns The schema with a new return type.
402
- */ toValidate(toValidateCallback) {
403
- return super.toValidate(toValidateCallback);
404
- }
405
- /**
406
- * Defines a list of strings that are allowed, it's useful when you want to restrict the values that are allowed.
407
- * Like a selector or a Choice field.
408
- *
409
- * @example
410
- * ```typescript
411
- * import * as p from '@palmares/schema';
412
- *
413
- * const schema = p.string().is(['Argentina', 'Brazil', 'Chile']);
414
- *
415
- * schema.parse('Argentina'); // { errors: [], parsed: 'Argentina' }
416
- * // { errors: [{
417
- * // code: 'invalid_value',
418
- * // message: 'The value should be equal to Argentina, Brazil, Chile',
419
- * // path: [] }], parsed: 'Uruguay' }
420
- * schema.parse('Uruguay');
421
- * ```
422
- *
423
- * @param value - The list of numbers that are allowed
424
- *
425
- * @returns - The schema instance
426
- */ is(value, options) {
427
- this.__is = {
428
- value,
429
- message: typeof options?.message === 'string' ? options.message : `The value should be equal to ${value.join(', ')}`
430
- };
431
- return this;
432
- }
433
- /**
434
- * Validates if the string ends with a specific value.
435
- *
436
- * @example
437
- * ```typescript
438
- * import * as p from '@palmares/schema';
439
- *
440
- * const schema = p.string().endsWith('.com');
441
- *
442
- * schema.parse('example.com'); // { errors: [], parsed: 'example.com' }
443
- *
444
- * // { errors: [{ code: 'endsWith', message: 'The value should end with .com', path: [] }], parsed: 'example.org' }
445
- * schema.parse('example.org');
446
- * ```
447
- *
448
- * @param value - The value that the string should end with.
449
- * @param options - The options for the endsWith function.
450
- * @param options.message - The message to be shown when the value does not end with the value.
451
- *
452
- * @returns - The schema instance.
453
- */ endsWith(value, options) {
454
- this.__endsWith = {
455
- value,
456
- message: options?.message || `The value should end with ${value}`
457
- };
458
- return this;
459
- }
460
- /**
461
- * Validates if the string starts with a specific value.
462
- *
463
- * @example
464
- * ```typescript
465
- * import * as p from '@palmares/schema';
466
- *
467
- * const schema = p.string().startsWith('https://');
468
- *
469
- * schema.parse('https://example.com'); // { errors: [], parsed: 'https://example.com' }
470
- * // {
471
- * // errors: [{ code: 'startsWith', message: 'The value should start with https://', path: [] }],
472
- * // parsed: 'http://example.com'
473
- * // }
474
- * schema.parse('http://example.com');
475
- * ```
476
- *
477
- * @param value - The value that the string should start with.
478
- * @param options - The options for the startsWith function.
479
- * @param options.message - The message to be shown when the value does not start with the value.
480
- *
481
- * @returns - The schema instance.
482
- */ startsWith(value, options) {
483
- this.__startsWith = {
484
- value,
485
- message: options?.message || `The value should start with ${value}`
486
- };
487
- return this;
488
- }
489
- /**
490
- * Checks if the string includes a specific substring.
491
- *
492
- * @example
493
- * ```typescript
494
- * import * as p from '@palmares/schema';
495
- *
496
- * const schema = p.string().includes('for babies');
497
- *
498
- * schema.parse('Computer graphics for babies'); // { errors: [], parsed: 'Computer graphics for babies' }
499
- * // {
500
- * // errors: [{
501
- * // code: 'includes',
502
- * // message: 'The string value should include the following substring 'for babies',
503
- * // path: []
504
- * // }], parsed: 'example.org' }
505
- * schema.parse('Learn javascript as you were 5');
506
- * ```
507
- *
508
- * @param value - The value that the string should include.
509
- * @param options - The options for the includes function.
510
- * @param options.message - The message to be shown when the value does not include the value.
511
- *
512
- * @returns - The schema instance.
513
- */ includes(value, options) {
514
- this.__includes = {
515
- value,
516
- message: options?.message || `The string value should include the following substring '${value}'`
517
- };
518
- return this;
519
- }
520
- /**
521
- * Validates if the string matches a specific regex.
522
- *
523
- * @example
524
- * ```typescript
525
- * import * as p from '@palmares/schema';
526
- *
527
- * const schema = p.string().regex(/^[a-z]+$/);
528
- *
529
- * schema.parse('abc'); // { errors: [], parsed: 'abc' }
530
- * // {
531
- * // errors: [{
532
- * // code: 'regex',
533
- * // message: 'The value should match the following regex /^[a-z]+$/',
534
- * // path: []
535
- * // }],
536
- * // parsed: '123'
537
- * // }
538
- * schema.parse('123');
539
- * ```
540
- *
541
- * @param value - The regex that the string should match.
542
- * @param options - The options for the regex function.
543
- * @param options.message - The message to be shown when the value does not match the regex.
544
- *
545
- * @returns - The schema instance.
546
- */ regex(value, options) {
547
- this.__regex = {
548
- value,
549
- message: options?.message || `The value should match the following regex '${value.toString()}'`
550
- };
551
- return this;
552
- }
553
- /**
554
- * Validates if the string has a maximum length. Use { inclusive: true } to allow the value to have the same length
555
- * as the maximum length.
556
- *
557
- * @example
558
- * ```typescript
559
- * import * as p from '@palmares/schema';
560
- *
561
- * const schema = p.string().maxLength(5);
562
- *
563
- * schema.parse('12345'); // { errors: [], parsed: '12345' }
564
- * // {
565
- * // errors: [{ code: 'maxLength', message: 'The value should have a maximum length of 5', path: [] }],
566
- * // parsed: '123
567
- * // }
568
- * schema.parse('123456');
569
- * ```
570
- *
571
- * @param value - The maximum length that the string should have.
572
- * @param options - The options for the maxLength function.
573
- * @param options.message - The message to be shown when the value has a length greater than the maximum length.
574
- * @param options.inclusive - Whether the value can have the same length as the maximum length. Defaults to false.
575
- *
576
- * @returns - The schema instance.
577
- */ maxLength(value, options) {
578
- this.__maxLength = {
579
- value,
580
- message: options?.message || `The value should have a maximum length of ${value}`
581
- };
582
- return this;
583
- }
584
- /**
585
- * Validates if the string has a minimum length. Use { inclusive: true } to allow the value to have the same length
586
- * as the minimum length.
587
- *
588
- * @example
589
- * ```typescript
590
- * import * as p from '@palmares/schema';
591
- *
592
- * const schema = p.string().minLength(5);
593
- *
594
- * schema.parse('12345'); // { errors: [], parsed: '12345' }
595
- * // {
596
- * // errors: [{ code: 'minLength', message: 'The value should have a minimum length of 5', path: [] }],
597
- * // parsed: '1234'
598
- * // }
599
- * schema.parse('1234');
600
- * ```
601
- *
602
- * @param value - The minimum length that the string should have.
603
- * @param options - The options for the minLength function.
604
- * @param options.message - The message to be shown when the value has a length less than the minimum length.
605
- * @param options.inclusive - Whether the value can have the same length as the minimum length. Defaults to false.
606
- *
607
- * @returns - The schema instance.
608
- */ minLength(value, options) {
609
- this.__minLength = {
610
- value,
611
- message: options?.message || `The value should have a minimum length of ${value}`
612
- };
613
- return this;
614
- }
615
- /**
616
- * Validates if the string is a valid UUID.
617
- *
618
- * @example
619
- * ```typescript
620
- * import * as p from '@palmares/schema';
621
- *
622
- * const schema = p.string().uuid();
623
- *
624
- * // { errors: [], parsed: '550e8400-e29b-41d4-a716-446655440000' }
625
- * schema.parse('550e8400-e29b-41d4-a716-446655440000');
626
- * ```
627
- *
628
- * @param options - The options for the uuid function.
629
- * @param options.message - The message to be shown when the value is not a valid UUID. Defaults to
630
- * 'The value should be a valid UUID'.
631
- *
632
- * @returns - The schema instance.
633
- */ uuid(options) {
634
- this.__uuid = {
635
- message: options?.message || 'The value should be a valid UUID'
636
- };
637
- return this;
638
- }
639
- /**
640
- * Validates if the string is a valid email or not
641
- *
642
- * @example
643
- * ```typescript
644
- *
645
- * import * as p from '@palmares/schema';
646
- *
647
- * const schema = p.string().email();
648
- *
649
- * schema.parse('john.doe@example.com'); // { errors: [], parsed: 'john.doe@example.com' }
650
- * ```
651
- *
652
- * @param options - The options for the email function.
653
- * @param options.message - The message to be shown when the value is not a valid email.
654
- * Defaults to 'The value should be a valid email'.
655
- *
656
- * @returns - The schema instance.
657
- */ email(options) {
658
- this.__email = {
659
- message: options?.message || 'The value should be a valid email'
660
- };
661
- return this;
662
- }
663
- static new() {
664
- const returnValue = new StringSchema();
665
- return returnValue;
666
- }
667
- }
668
- export const string = StringSchema.new;
@@ -1 +0,0 @@
1
- export { };