@orioro/util 0.2.0 → 0.3.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 (198) hide show
  1. package/dist/PromiseLikeEventEmitter/index.d.ts +10 -0
  2. package/dist/array/arrayChunk.d.ts +1 -0
  3. package/dist/array/index.d.ts +1 -0
  4. package/dist/debug/debugFn/index.d.ts +15 -0
  5. package/dist/debug/debugFn/util.d.ts +1 -0
  6. package/dist/debug/debugFn.d.ts +15 -0
  7. package/dist/debug/deepFreeze.d.ts +5 -0
  8. package/dist/debug/index.d.ts +3 -0
  9. package/dist/debug/wait.d.ts +1 -0
  10. package/dist/index.d.ts +9 -0
  11. package/dist/index.mjs +11 -11
  12. package/dist/interpolate/index.d.ts +14 -0
  13. package/dist/maybeFn.d.ts +1 -0
  14. package/dist/pickPaths/index.d.ts +4 -0
  15. package/dist/promise/batchFn.d.ts +47 -0
  16. package/dist/promise/index.d.ts +3 -0
  17. package/dist/promise/promiseReduce.d.ts +1 -0
  18. package/dist/promise/resolveNestedPromises.d.ts +14 -0
  19. package/dist/promise/types.d.ts +2 -0
  20. package/dist/resolvePaths/index.d.ts +4 -0
  21. package/dist/switchValue.d.ts +9 -0
  22. package/dist/typeOf.d.ts +7 -0
  23. package/dist/validate/async/index.d.ts +18 -0
  24. package/dist/validate/async/validateAsyncFn.d.ts +2 -0
  25. package/dist/validate/async/validators/index.d.ts +2 -0
  26. package/dist/validate/async/validators/logical.d.ts +4 -0
  27. package/dist/validate/async/validators/shape.d.ts +7 -0
  28. package/dist/validate/async/validators/tmpand.d.ts +2 -0
  29. package/dist/validate/async/validators/tmpor.d.ts +2 -0
  30. package/dist/validate/common/ValidationError.d.ts +11 -0
  31. package/dist/validate/common/util/defaultErrorMessage.d.ts +2 -0
  32. package/dist/validate/common/util/index.d.ts +3 -0
  33. package/dist/validate/common/util/parseValidatorInput.d.ts +5 -0
  34. package/dist/validate/common/util/resolveValidationResult.d.ts +6 -0
  35. package/dist/validate/common/validators/index.d.ts +1 -0
  36. package/dist/validate/common/validators/type.d.ts +9 -0
  37. package/dist/validate/index.d.ts +3 -0
  38. package/dist/validate/sync/index.d.ts +18 -0
  39. package/dist/validate/sync/validateSyncFn.d.ts +2 -0
  40. package/dist/validate/sync/validators/index.d.ts +2 -0
  41. package/dist/validate/sync/validators/logical.d.ts +4 -0
  42. package/dist/validate/sync/validators/shape.d.ts +7 -0
  43. package/dist/validate/types/async.d.ts +5 -0
  44. package/dist/validate/types/common.d.ts +27 -0
  45. package/dist/validate/types/index.d.ts +3 -0
  46. package/dist/validate/types/sync.d.ts +5 -0
  47. package/package.json +6 -2
  48. package/babel.config.cjs +0 -13
  49. package/coverage/clover.xml +0 -491
  50. package/coverage/coverage-final.json +0 -29
  51. package/coverage/lcov-report/ValidationError.ts.html +0 -184
  52. package/coverage/lcov-report/base.css +0 -224
  53. package/coverage/lcov-report/block-navigation.js +0 -87
  54. package/coverage/lcov-report/favicon.png +0 -0
  55. package/coverage/lcov-report/index.html +0 -296
  56. package/coverage/lcov-report/prettify.css +0 -1
  57. package/coverage/lcov-report/prettify.js +0 -2
  58. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  59. package/coverage/lcov-report/sorter.js +0 -196
  60. package/coverage/lcov-report/src/debug/deepFreeze.ts.html +0 -157
  61. package/coverage/lcov-report/src/debug/index.html +0 -146
  62. package/coverage/lcov-report/src/debug/index.ts.html +0 -91
  63. package/coverage/lcov-report/src/debug/wait.ts.html +0 -127
  64. package/coverage/lcov-report/src/index.html +0 -131
  65. package/coverage/lcov-report/src/interpolate/index.html +0 -116
  66. package/coverage/lcov-report/src/interpolate/index.ts.html +0 -277
  67. package/coverage/lcov-report/src/maybeFn.ts.html +0 -94
  68. package/coverage/lcov-report/src/promise/index.html +0 -146
  69. package/coverage/lcov-report/src/promise/index.ts.html +0 -91
  70. package/coverage/lcov-report/src/promise/promiseReduce.ts.html +0 -130
  71. package/coverage/lcov-report/src/promise/resolveNestedPromises.ts.html +0 -271
  72. package/coverage/lcov-report/src/switchValue.ts.html +0 -253
  73. package/coverage/lcov-report/src/typeOf.ts.html +0 -328
  74. package/coverage/lcov-report/src/validate/ValidationError.ts.html +0 -184
  75. package/coverage/lcov-report/src/validate/async/index.html +0 -131
  76. package/coverage/lcov-report/src/validate/async/index.ts.html +0 -241
  77. package/coverage/lcov-report/src/validate/async/parseValidator.ts.html +0 -136
  78. package/coverage/lcov-report/src/validate/async/validateAsyncFn.ts.html +0 -208
  79. package/coverage/lcov-report/src/validate/async/validators/and.ts.html +0 -154
  80. package/coverage/lcov-report/src/validate/async/validators/index.html +0 -146
  81. package/coverage/lcov-report/src/validate/async/validators/index.ts.html +0 -91
  82. package/coverage/lcov-report/src/validate/async/validators/logical.ts.html +0 -253
  83. package/coverage/lcov-report/src/validate/async/validators/or.ts.html +0 -151
  84. package/coverage/lcov-report/src/validate/async/validators/shape.ts.html +0 -565
  85. package/coverage/lcov-report/src/validate/common/ValidationError.ts.html +0 -184
  86. package/coverage/lcov-report/src/validate/common/index.html +0 -116
  87. package/coverage/lcov-report/src/validate/common/util/defaultErrorMessage.ts.html +0 -163
  88. package/coverage/lcov-report/src/validate/common/util/index.html +0 -161
  89. package/coverage/lcov-report/src/validate/common/util/index.ts.html +0 -94
  90. package/coverage/lcov-report/src/validate/common/util/parseValidator.ts.html +0 -316
  91. package/coverage/lcov-report/src/validate/common/util/parseValidatorInput.ts.html +0 -316
  92. package/coverage/lcov-report/src/validate/common/util/resolveValidationResult.ts.html +0 -277
  93. package/coverage/lcov-report/src/validate/common/util/validatorParser.ts.html +0 -316
  94. package/coverage/lcov-report/src/validate/common/validators/index.html +0 -131
  95. package/coverage/lcov-report/src/validate/common/validators/index.ts.html +0 -88
  96. package/coverage/lcov-report/src/validate/common/validators/type.ts.html +0 -388
  97. package/coverage/lcov-report/src/validate/fmtValidationResult.ts.html +0 -268
  98. package/coverage/lcov-report/src/validate/index.html +0 -116
  99. package/coverage/lcov-report/src/validate/index.ts.html +0 -94
  100. package/coverage/lcov-report/src/validate/makeValidate.ts.html +0 -634
  101. package/coverage/lcov-report/src/validate/specUtil/commonTests.js.html +0 -1324
  102. package/coverage/lcov-report/src/validate/specUtil/index.html +0 -116
  103. package/coverage/lcov-report/src/validate/sync/index.html +0 -131
  104. package/coverage/lcov-report/src/validate/sync/index.ts.html +0 -244
  105. package/coverage/lcov-report/src/validate/sync/parseValidator.ts.html +0 -136
  106. package/coverage/lcov-report/src/validate/sync/validateSyncFn.ts.html +0 -223
  107. package/coverage/lcov-report/src/validate/sync/validators/and.ts.html +0 -148
  108. package/coverage/lcov-report/src/validate/sync/validators/index.html +0 -146
  109. package/coverage/lcov-report/src/validate/sync/validators/index.ts.html +0 -91
  110. package/coverage/lcov-report/src/validate/sync/validators/logical.ts.html +0 -226
  111. package/coverage/lcov-report/src/validate/sync/validators/or.ts.html +0 -130
  112. package/coverage/lcov-report/src/validate/sync/validators/shape.ts.html +0 -523
  113. package/coverage/lcov-report/src/validate/sync/validators/type.ts.html +0 -154
  114. package/coverage/lcov-report/src/validate/syncValidators/and.ts.html +0 -157
  115. package/coverage/lcov-report/src/validate/syncValidators/index.html +0 -176
  116. package/coverage/lcov-report/src/validate/syncValidators/index.ts.html +0 -97
  117. package/coverage/lcov-report/src/validate/syncValidators/or.ts.html +0 -127
  118. package/coverage/lcov-report/src/validate/syncValidators/shape.ts.html +0 -559
  119. package/coverage/lcov-report/src/validate/syncValidators/string.ts.html +0 -163
  120. package/coverage/lcov-report/src/validate/syncValidators/type.ts.html +0 -154
  121. package/coverage/lcov-report/src/validate/util/defaultErrorMessage.ts.html +0 -169
  122. package/coverage/lcov-report/src/validate/util/index.html +0 -146
  123. package/coverage/lcov-report/src/validate/util/index.ts.html +0 -91
  124. package/coverage/lcov-report/src/validate/util/resolveValidationResult.ts.html +0 -253
  125. package/coverage/lcov-report/src/validate/validate.ts.html +0 -220
  126. package/coverage/lcov-report/src/validate/validateAsync.ts.html +0 -220
  127. package/coverage/lcov-report/src/validate/validators/and.ts.html +0 -157
  128. package/coverage/lcov-report/src/validate/validators/index.html +0 -176
  129. package/coverage/lcov-report/src/validate/validators/index.ts.html +0 -97
  130. package/coverage/lcov-report/src/validate/validators/or.ts.html +0 -127
  131. package/coverage/lcov-report/src/validate/validators/shape.ts.html +0 -541
  132. package/coverage/lcov-report/src/validate/validators/type.ts.html +0 -154
  133. package/coverage/lcov-report/src/validate_/ValidationError.ts.html +0 -184
  134. package/coverage/lcov-report/src/validate_/fmtValidationResult.ts.html +0 -268
  135. package/coverage/lcov-report/src/validate_/index.html +0 -161
  136. package/coverage/lcov-report/src/validate_/makeValidate.ts.html +0 -634
  137. package/coverage/lcov-report/src/validate_/validate.ts.html +0 -220
  138. package/coverage/lcov-report/switchValue.ts.html +0 -253
  139. package/coverage/lcov-report/typeOf.ts.html +0 -331
  140. package/coverage/lcov-report/validate.ts.html +0 -757
  141. package/coverage/lcov.info +0 -1056
  142. package/jest.config.js +0 -6
  143. package/rollup.config.mjs +0 -6
  144. package/src/PromiseLikeEventEmitter/index.ts +0 -35
  145. package/src/array/arrayChunk.ts +0 -7
  146. package/src/array/index.ts +0 -1
  147. package/src/debug/debugFn/index.ts +0 -48
  148. package/src/debug/debugFn/util.ts +0 -27
  149. package/src/debug/deepFreeze.ts +0 -26
  150. package/src/debug/index.ts +0 -3
  151. package/src/debug/wait.ts +0 -14
  152. package/src/index.ts +0 -9
  153. package/src/interpolate/index.spec.ts +0 -20
  154. package/src/interpolate/index.ts +0 -64
  155. package/src/maybeFn.ts +0 -3
  156. package/src/promise/batchFn.spec.ts +0 -92
  157. package/src/promise/batchFn.ts +0 -176
  158. package/src/promise/index.ts +0 -3
  159. package/src/promise/promiseReduce.ts +0 -15
  160. package/src/promise/resolveNestedPromises.spec.ts +0 -205
  161. package/src/promise/resolveNestedPromises.ts +0 -83
  162. package/src/promise/types.ts +0 -2
  163. package/src/resolvePaths/index.spec.ts +0 -42
  164. package/src/resolvePaths/index.ts +0 -21
  165. package/src/switchValue.spec.ts +0 -30
  166. package/src/switchValue.ts +0 -59
  167. package/src/typeOf.spec.ts +0 -47
  168. package/src/typeOf.ts +0 -81
  169. package/src/validate/__snapshots__/index.spec.ts.snap +0 -9
  170. package/src/validate/async/index.spec.ts +0 -236
  171. package/src/validate/async/index.ts +0 -52
  172. package/src/validate/async/validateAsyncFn.ts +0 -41
  173. package/src/validate/async/validators/index.ts +0 -2
  174. package/src/validate/async/validators/logical.ts +0 -56
  175. package/src/validate/async/validators/shape.ts +0 -160
  176. package/src/validate/async/validators/tmpand.ts +0 -24
  177. package/src/validate/async/validators/tmpor.ts +0 -21
  178. package/src/validate/common/ValidationError.ts +0 -33
  179. package/src/validate/common/util/defaultErrorMessage.ts +0 -26
  180. package/src/validate/common/util/index.ts +0 -3
  181. package/src/validate/common/util/parseValidatorInput.ts +0 -77
  182. package/src/validate/common/util/resolveValidationResult.ts +0 -64
  183. package/src/validate/common/validators/index.ts +0 -1
  184. package/src/validate/common/validators/type.ts +0 -101
  185. package/src/validate/index.spec.ts +0 -5
  186. package/src/validate/index.ts +0 -3
  187. package/src/validate/specUtil/commonTests.js +0 -413
  188. package/src/validate/sync/index.spec.ts +0 -81
  189. package/src/validate/sync/index.ts +0 -53
  190. package/src/validate/sync/validateSyncFn.ts +0 -46
  191. package/src/validate/sync/validators/index.ts +0 -2
  192. package/src/validate/sync/validators/logical.ts +0 -47
  193. package/src/validate/sync/validators/shape.ts +0 -146
  194. package/src/validate/types/async.ts +0 -20
  195. package/src/validate/types/common.ts +0 -70
  196. package/src/validate/types/index.ts +0 -3
  197. package/src/validate/types/sync.ts +0 -20
  198. package/tsconfig.json +0 -11
@@ -1,81 +0,0 @@
1
- import { validate } from './'
2
-
3
- import { commonTests } from '../specUtil/commonTests'
4
-
5
- commonTests(
6
- {
7
- describe,
8
- test,
9
- expect,
10
- },
11
- {
12
- validate: validate,
13
- },
14
- )
15
-
16
- describe('error upon attempting async', () => {
17
- test('basic', () => {
18
- const asyncValidator = async (input) =>
19
- typeof input === 'string' && input.length > 5
20
-
21
- expect(() => {
22
- validate(asyncValidator, 'Some text')
23
- }).toThrow('Promises not supported in sync validation method')
24
-
25
- expect(() => {
26
- validate(asyncValidator, 'Some')
27
- }).toThrow('Promises not supported in sync validation method')
28
- })
29
- })
30
-
31
- describe('assertValid', () => {
32
- test('basic', () => {
33
- expect(validate.assertValid('string!', 'test')).toEqual('test')
34
- expect(() => validate.assertValid('string!', null)).toThrow(
35
- "Invalid input: 'null'. Expected type(s) `string!`, but got type `null`",
36
- )
37
- })
38
-
39
- test('shorthand', () => {
40
- const validator = {
41
- param1: 'string!',
42
- param2: {
43
- param21: 'number! | string!',
44
- param22: 'function',
45
- },
46
- }
47
-
48
- const input1 = {
49
- param1: 'Some string',
50
- param2: {
51
- param21: 2,
52
- param22: () => {},
53
- },
54
- }
55
-
56
- expect(validate.assertValid(validator, input1)).toEqual(input1)
57
-
58
- const input2 = {
59
- param1: 'Some string',
60
- param2: {
61
- param21: 2,
62
- param22: 3,
63
- },
64
- }
65
-
66
- expect(() => validate.assertValid(validator, input2)).toThrow(
67
- "param22: Invalid input: '3'. Expected type(s) `function`, but got type `number`",
68
- )
69
-
70
- const input3 = {
71
- param1: 'Some string',
72
- param2: {
73
- param21: null,
74
- },
75
- }
76
-
77
- expect(() => validate.assertValid(validator, input3)).toThrow(
78
- "param21: Invalid input: 'null'. Expected type(s) `number! | string!`, but got type `null`",
79
- )
80
- })
81
- })
@@ -1,53 +0,0 @@
1
- import * as common from '../common/validators'
2
- import * as sync from './validators'
3
-
4
- import { validateSyncFn } from './validateSyncFn'
5
- import { DetailedInvalid, SyncValidatorSystem, Valid } from '../types'
6
- import { ValidationError } from '../common/ValidationError'
7
-
8
- function assertValidSync<InputT = any>(
9
- validator: SyncValidatorSystem['ValidatorInput'],
10
- input: InputT,
11
- ): InputT {
12
- const validationResult = validate(validator, input)
13
-
14
- if (validationResult === true) {
15
- return input
16
- } else {
17
- throw new ValidationError(validationResult)
18
- }
19
- }
20
-
21
- interface Validate {
22
- (
23
- validatorInput: SyncValidatorSystem['ValidatorInput'],
24
- input: any,
25
- ): Valid | DetailedInvalid
26
- type: typeof common.typeValidator
27
-
28
- obj: typeof sync.obj
29
- objOf: typeof sync.objOf
30
- tuple: typeof sync.tuple
31
- arrayOf: typeof sync.arrayOf
32
-
33
- and: typeof sync.and
34
- or: typeof sync.or
35
- not: typeof sync.not
36
-
37
- assertValid: typeof assertValidSync
38
- }
39
-
40
- export const validate: Validate = validateSyncFn as Validate
41
-
42
- validate.type = common.typeValidator
43
-
44
- validate.obj = sync.obj
45
- validate.objOf = sync.objOf
46
- validate.tuple = sync.tuple
47
- validate.arrayOf = sync.arrayOf
48
-
49
- validate.and = sync.and
50
- validate.or = sync.or
51
- validate.not = sync.not
52
-
53
- validate.assertValid = assertValidSync
@@ -1,46 +0,0 @@
1
- import {
2
- DetailedInvalid,
3
- Valid,
4
- SyncValidatorSystem,
5
- ValidatorResult,
6
- } from '../types'
7
-
8
- import { obj as syncObjValidator } from './validators/shape'
9
-
10
- import { parseValidatorInput, resolveValidationResult } from '../common/util'
11
-
12
- export function validateSyncFn(
13
- validatorInput: SyncValidatorSystem['ValidatorInput'],
14
- input: any,
15
- ): Valid | DetailedInvalid {
16
- let result: ValidatorResult
17
-
18
- const [validatorFn, errorMessage] = parseValidatorInput<
19
- SyncValidatorSystem['ValidatorInput'],
20
- SyncValidatorSystem['ValidatorFn'],
21
- SyncValidatorSystem['Validator']
22
- >(
23
- {
24
- objValidator: syncObjValidator,
25
- },
26
- validatorInput,
27
- )
28
-
29
- try {
30
- result = validatorFn(input, {
31
- validateSync: validateSyncFn,
32
- })
33
- } catch (err) {
34
- result = err as Error
35
- }
36
-
37
- if (result instanceof Promise) {
38
- throw new Error('Promises not supported in sync validation method')
39
- }
40
-
41
- return resolveValidationResult({
42
- errorMessage,
43
- input,
44
- result,
45
- })
46
- }
@@ -1,2 +0,0 @@
1
- export * from './logical'
2
- export * from './shape'
@@ -1,47 +0,0 @@
1
- import { DetailedInvalid, Valid, SyncValidatorSystem } from '../../types'
2
-
3
- //
4
- // And operator (serial)
5
- //
6
- export function and(
7
- validators: SyncValidatorSystem['ValidatorInput'][],
8
- ): SyncValidatorSystem['ValidatorFn'] {
9
- return function validateAnd(input, { validateSync }) {
10
- const result = validators.reduce(
11
- (acc, validator) => (acc !== true ? acc : validateSync(validator, input)),
12
- true as Valid | DetailedInvalid,
13
- )
14
-
15
- if (result === true) {
16
- return true
17
- } else {
18
- return result
19
- }
20
- }
21
- }
22
-
23
- //
24
- // Or operator (serial)
25
- //
26
- export function or(
27
- validators: SyncValidatorSystem['ValidatorInput'][],
28
- ): SyncValidatorSystem['ValidatorFn'] {
29
- return function validateOr(input, { validateSync }) {
30
- const someIsValid = validators.some(
31
- (validator) => validateSync(validator, input) === true,
32
- )
33
-
34
- return someIsValid
35
- }
36
- }
37
-
38
- //
39
- // Not
40
- //
41
- export function not(
42
- validator: SyncValidatorSystem['ValidatorInput'],
43
- ): SyncValidatorSystem['ValidatorFn'] {
44
- return function validateNot(input, { validateSync }) {
45
- return validateSync(validator, input) !== true
46
- }
47
- }
@@ -1,146 +0,0 @@
1
- import {
2
- DetailedInvalid,
3
- SyncValidatorSystem,
4
- SyncValidateFn,
5
- } from '../../types'
6
- import { typeValidator } from '../../common/validators/type'
7
- import { getProperty } from 'dot-prop'
8
-
9
- function _shapeGeneralValidator({
10
- input,
11
- baseType,
12
- collectNestedErrors,
13
- validateSync,
14
- }: {
15
- input: any
16
- baseType: 'object' | 'array'
17
- collectNestedErrors: () => DetailedInvalid[]
18
- validateSync: SyncValidateFn
19
- }) {
20
- const baseTypeValidation = validateSync(
21
- typeValidator({
22
- type: baseType,
23
- required: true,
24
- }),
25
- input,
26
- )
27
-
28
- if (baseTypeValidation !== true) {
29
- return baseTypeValidation
30
- }
31
-
32
- const nestedErrors = collectNestedErrors()
33
-
34
- return nestedErrors.length === 0
35
- ? true
36
- : {
37
- input,
38
- nestedErrors,
39
- }
40
- }
41
-
42
- export function obj(objShape: {
43
- [key: string]: SyncValidatorSystem['ValidatorInput']
44
- }): SyncValidatorSystem['ValidatorFn'] {
45
- return function validateObject(input, { validateSync }) {
46
- return _shapeGeneralValidator({
47
- input,
48
- baseType: 'object',
49
- validateSync,
50
- collectNestedErrors: () =>
51
- Object.entries(objShape).reduce((acc, [path, pathValidator]) => {
52
- const pathInput = getProperty(input, path)
53
- const pathResult = validateSync(pathValidator, pathInput)
54
-
55
- return pathResult === true
56
- ? acc
57
- : [
58
- ...acc,
59
- {
60
- ...pathResult,
61
- path,
62
- },
63
- ]
64
- }, [] as DetailedInvalid[]),
65
- })
66
- }
67
- }
68
-
69
- export function objOf(
70
- ofType: SyncValidatorSystem['ValidatorInput'],
71
- ): SyncValidatorSystem['ValidatorFn'] {
72
- return function validateObjOf(input, { validateSync }) {
73
- return _shapeGeneralValidator({
74
- input,
75
- baseType: 'object',
76
- validateSync,
77
- collectNestedErrors: () =>
78
- Object.entries(input).reduce((acc, [key, keyInput]) => {
79
- const indexResult = validateSync(ofType, keyInput)
80
-
81
- return indexResult === true
82
- ? acc
83
- : [
84
- ...acc,
85
- {
86
- ...indexResult,
87
- path: key,
88
- },
89
- ]
90
- }, [] as DetailedInvalid[]),
91
- })
92
- }
93
- }
94
-
95
- export function tuple(
96
- tupleShape: SyncValidatorSystem['ValidatorInput'][],
97
- ): SyncValidatorSystem['ValidatorFn'] {
98
- return function validateTuple(input, { validateSync }) {
99
- return _shapeGeneralValidator({
100
- input,
101
- baseType: 'array',
102
- validateSync,
103
- collectNestedErrors: () =>
104
- tupleShape.reduce((acc, indexValidator, index) => {
105
- const indexInput = input[index]
106
- const indexResult = validateSync(indexValidator, indexInput)
107
-
108
- return indexResult === true
109
- ? acc
110
- : [
111
- ...acc,
112
- {
113
- ...indexResult,
114
- path: index + '',
115
- },
116
- ]
117
- }, [] as DetailedInvalid[]),
118
- })
119
- }
120
- }
121
-
122
- export function arrayOf(
123
- ofType: SyncValidatorSystem['ValidatorInput'],
124
- ): SyncValidatorSystem['ValidatorFn'] {
125
- return function validateArrayOf(input, { validateSync }) {
126
- return _shapeGeneralValidator({
127
- input,
128
- baseType: 'array',
129
- validateSync,
130
- collectNestedErrors: () =>
131
- (input as any[]).reduce((acc, indexInput, index) => {
132
- const indexResult = validateSync(ofType, indexInput)
133
-
134
- return indexResult === true
135
- ? acc
136
- : [
137
- ...acc,
138
- {
139
- ...indexResult,
140
- path: index + '',
141
- },
142
- ]
143
- }, [] as DetailedInvalid[]),
144
- })
145
- }
146
- }
@@ -1,20 +0,0 @@
1
- import {
2
- DetailedInvalid,
3
- Valid,
4
- ValidatorResult,
5
- ValidatorSystem,
6
- } from './common'
7
-
8
- export type AsyncValidatorSystem = ValidatorSystem<
9
- (
10
- input: any,
11
- context: {
12
- validateAsync: AsyncValidateFn
13
- },
14
- ) => ValidatorResult | Promise<ValidatorResult>
15
- >
16
-
17
- export type AsyncValidateFn = (
18
- validatorInput: AsyncValidatorSystem['ValidatorInput'],
19
- input: any,
20
- ) => Promise<Valid | DetailedInvalid>
@@ -1,70 +0,0 @@
1
- export type Valid = true
2
- export type SimpleInvalid = false | undefined | null
3
- export type ErrorMessageInvalid = string
4
- export type ErrorInvalid = Error
5
- export type DetailedInvalid = {
6
- input: any
7
- message?: string
8
- code?: string
9
- error?: Error
10
- path?: string
11
- nestedErrors?: DetailedInvalid[]
12
- expectedTypes?: string
13
- }
14
-
15
- export type ValidatorResult =
16
- | Valid
17
- | SimpleInvalid
18
- | ErrorMessageInvalid
19
- | ErrorInvalid
20
- | DetailedInvalid
21
-
22
- //
23
- // Fucntion that generates an error message
24
- //
25
- export type ValidatorErrorMessageFn = (props: DetailedInvalid) => string
26
-
27
- export type ValidatorErrorMessageInput = string | ValidatorErrorMessageFn
28
-
29
- //
30
- // Validator Fn that can be used both in Sync and Async
31
- // versions
32
- //
33
- export type CommonValidatorFn = (input: any) => ValidatorResult
34
-
35
- type _ValidatorFnInput<ValidatorFn> =
36
- | string
37
- | {
38
- [key: string]:
39
- | _ValidatorFnInput<ValidatorFn>
40
- | [_ValidatorFnInput<ValidatorFn>, ValidatorErrorMessageInput]
41
- }
42
- | ValidatorFn
43
-
44
- //
45
- // Generates all required types for validator system
46
- //
47
- export type ValidatorSystem<ValidatorFn> = {
48
- ValidatorFn: ValidatorFn
49
- //
50
- // The input for the validator fn
51
- //
52
- ValidatorFnInput: _ValidatorFnInput<ValidatorFn>
53
- //
54
- // The final computed validator with:
55
- // - function
56
- // - error message
57
- //
58
- Validator: [ValidatorFn, ValidatorErrorMessageFn]
59
- //
60
- // The validator input, which may be provided in 3 formats:
61
- // - string - indicating a shorthand for type validation
62
- // - function - indicating a full fledged ValidatorFn without custom error message
63
- // - [string, ValidatorErrorMessageInput] - indicating a shorthand validator function for type validation + custom error message
64
- // - [function, ValidatorErrorMessageInput] - indicating a full fledged Validator
65
- //
66
- // prettier-ignore
67
- ValidatorInput:
68
- | _ValidatorFnInput<ValidatorFn>
69
- | [_ValidatorFnInput<ValidatorFn>, ValidatorErrorMessageInput]
70
- }
@@ -1,3 +0,0 @@
1
- export * from './common'
2
- export * from './sync'
3
- export * from './async'
@@ -1,20 +0,0 @@
1
- import {
2
- DetailedInvalid,
3
- Valid,
4
- ValidatorResult,
5
- ValidatorSystem,
6
- } from './common'
7
-
8
- export type SyncValidatorSystem = ValidatorSystem<
9
- (
10
- input: any,
11
- context: {
12
- validateSync: SyncValidateFn
13
- },
14
- ) => ValidatorResult
15
- >
16
-
17
- export type SyncValidateFn = (
18
- validatorInput: SyncValidatorSystem['ValidatorInput'],
19
- input: any,
20
- ) => Valid | DetailedInvalid
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es5",
4
- "lib": ["dom", "esnext"],
5
- "allowJs": true,
6
- "allowSyntheticDefaultImports": true,
7
- "strict": true
8
- },
9
- "include": ["src/**/*.ts"],
10
- "exclude": ["**/*.spec.ts", "node_modules"]
11
- }