@orioro/util 0.0.0 → 0.2.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 (165) hide show
  1. package/README.md +1 -0
  2. package/babel.config.cjs +13 -0
  3. package/coverage/clover.xml +488 -45
  4. package/coverage/coverage-final.json +28 -2
  5. package/coverage/lcov-report/ValidationError.ts.html +184 -0
  6. package/coverage/lcov-report/base.css +19 -7
  7. package/coverage/lcov-report/block-navigation.js +87 -0
  8. package/coverage/lcov-report/favicon.png +0 -0
  9. package/coverage/lcov-report/index.html +248 -58
  10. package/coverage/lcov-report/prettify.js +1 -0
  11. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  12. package/coverage/lcov-report/sorter.js +52 -14
  13. package/coverage/lcov-report/src/debug/deepFreeze.ts.html +157 -0
  14. package/coverage/lcov-report/src/debug/index.html +146 -0
  15. package/coverage/lcov-report/src/debug/index.ts.html +91 -0
  16. package/coverage/lcov-report/src/debug/wait.ts.html +127 -0
  17. package/coverage/lcov-report/src/index.html +131 -0
  18. package/coverage/lcov-report/src/interpolate/index.html +116 -0
  19. package/coverage/lcov-report/src/interpolate/index.ts.html +277 -0
  20. package/coverage/lcov-report/src/maybeFn.ts.html +94 -0
  21. package/coverage/lcov-report/src/promise/index.html +146 -0
  22. package/coverage/lcov-report/src/promise/index.ts.html +91 -0
  23. package/coverage/lcov-report/src/promise/promiseReduce.ts.html +130 -0
  24. package/coverage/lcov-report/src/promise/resolveNestedPromises.ts.html +271 -0
  25. package/coverage/lcov-report/src/switchValue.ts.html +253 -0
  26. package/coverage/lcov-report/src/typeOf.ts.html +328 -0
  27. package/coverage/lcov-report/src/validate/ValidationError.ts.html +184 -0
  28. package/coverage/lcov-report/src/validate/async/index.html +131 -0
  29. package/coverage/lcov-report/src/validate/async/index.ts.html +241 -0
  30. package/coverage/lcov-report/src/validate/async/parseValidator.ts.html +136 -0
  31. package/coverage/lcov-report/src/validate/async/validateAsyncFn.ts.html +208 -0
  32. package/coverage/lcov-report/src/validate/async/validators/and.ts.html +154 -0
  33. package/coverage/lcov-report/src/validate/async/validators/index.html +146 -0
  34. package/coverage/lcov-report/src/validate/async/validators/index.ts.html +91 -0
  35. package/coverage/lcov-report/src/validate/async/validators/logical.ts.html +253 -0
  36. package/coverage/lcov-report/src/validate/async/validators/or.ts.html +151 -0
  37. package/coverage/lcov-report/src/validate/async/validators/shape.ts.html +565 -0
  38. package/coverage/lcov-report/src/validate/common/ValidationError.ts.html +184 -0
  39. package/coverage/lcov-report/src/validate/common/index.html +116 -0
  40. package/coverage/lcov-report/src/validate/common/util/defaultErrorMessage.ts.html +163 -0
  41. package/coverage/lcov-report/src/validate/common/util/index.html +161 -0
  42. package/coverage/lcov-report/src/validate/common/util/index.ts.html +94 -0
  43. package/coverage/lcov-report/src/validate/common/util/parseValidator.ts.html +316 -0
  44. package/coverage/lcov-report/src/validate/common/util/parseValidatorInput.ts.html +316 -0
  45. package/coverage/lcov-report/src/validate/common/util/resolveValidationResult.ts.html +277 -0
  46. package/coverage/lcov-report/src/validate/common/util/validatorParser.ts.html +316 -0
  47. package/coverage/lcov-report/src/validate/common/validators/index.html +131 -0
  48. package/coverage/lcov-report/src/validate/common/validators/index.ts.html +88 -0
  49. package/coverage/lcov-report/src/validate/common/validators/type.ts.html +388 -0
  50. package/coverage/lcov-report/src/validate/fmtValidationResult.ts.html +268 -0
  51. package/coverage/lcov-report/src/validate/index.html +116 -0
  52. package/coverage/lcov-report/src/validate/index.ts.html +94 -0
  53. package/coverage/lcov-report/src/validate/makeValidate.ts.html +634 -0
  54. package/coverage/lcov-report/src/validate/specUtil/commonTests.js.html +1324 -0
  55. package/coverage/lcov-report/src/validate/specUtil/index.html +116 -0
  56. package/coverage/lcov-report/src/validate/sync/index.html +131 -0
  57. package/coverage/lcov-report/src/validate/sync/index.ts.html +244 -0
  58. package/coverage/lcov-report/src/validate/sync/parseValidator.ts.html +136 -0
  59. package/coverage/lcov-report/src/validate/sync/validateSyncFn.ts.html +223 -0
  60. package/coverage/lcov-report/src/validate/sync/validators/and.ts.html +148 -0
  61. package/coverage/lcov-report/src/validate/sync/validators/index.html +146 -0
  62. package/coverage/lcov-report/src/validate/sync/validators/index.ts.html +91 -0
  63. package/coverage/lcov-report/src/validate/sync/validators/logical.ts.html +226 -0
  64. package/coverage/lcov-report/src/validate/sync/validators/or.ts.html +130 -0
  65. package/coverage/lcov-report/src/validate/sync/validators/shape.ts.html +523 -0
  66. package/coverage/lcov-report/src/validate/sync/validators/type.ts.html +154 -0
  67. package/coverage/lcov-report/src/validate/syncValidators/and.ts.html +157 -0
  68. package/coverage/lcov-report/src/validate/syncValidators/index.html +176 -0
  69. package/coverage/lcov-report/src/validate/syncValidators/index.ts.html +97 -0
  70. package/coverage/lcov-report/src/validate/syncValidators/or.ts.html +127 -0
  71. package/coverage/lcov-report/src/validate/syncValidators/shape.ts.html +559 -0
  72. package/coverage/lcov-report/src/validate/syncValidators/string.ts.html +163 -0
  73. package/coverage/lcov-report/src/validate/syncValidators/type.ts.html +154 -0
  74. package/coverage/lcov-report/src/validate/util/defaultErrorMessage.ts.html +169 -0
  75. package/coverage/lcov-report/src/validate/util/index.html +146 -0
  76. package/coverage/lcov-report/src/validate/util/index.ts.html +91 -0
  77. package/coverage/lcov-report/src/validate/util/resolveValidationResult.ts.html +253 -0
  78. package/coverage/lcov-report/src/validate/validate.ts.html +220 -0
  79. package/coverage/lcov-report/src/validate/validateAsync.ts.html +220 -0
  80. package/coverage/lcov-report/src/validate/validators/and.ts.html +157 -0
  81. package/coverage/lcov-report/src/validate/validators/index.html +176 -0
  82. package/coverage/lcov-report/src/validate/validators/index.ts.html +97 -0
  83. package/coverage/lcov-report/src/validate/validators/or.ts.html +127 -0
  84. package/coverage/lcov-report/src/validate/validators/shape.ts.html +541 -0
  85. package/coverage/lcov-report/src/validate/validators/type.ts.html +154 -0
  86. package/coverage/lcov-report/src/validate_/ValidationError.ts.html +184 -0
  87. package/coverage/lcov-report/src/validate_/fmtValidationResult.ts.html +268 -0
  88. package/coverage/lcov-report/src/validate_/index.html +161 -0
  89. package/coverage/lcov-report/src/validate_/makeValidate.ts.html +634 -0
  90. package/coverage/lcov-report/src/validate_/validate.ts.html +220 -0
  91. package/coverage/lcov-report/switchValue.ts.html +253 -0
  92. package/coverage/lcov-report/typeOf.ts.html +331 -0
  93. package/coverage/lcov-report/validate.ts.html +757 -0
  94. package/coverage/lcov.info +1045 -74
  95. package/dist/index.mjs +1437 -73
  96. package/jest.config.js +6 -0
  97. package/package.json +27 -27
  98. package/rollup.config.mjs +6 -0
  99. package/src/PromiseLikeEventEmitter/index.ts +35 -0
  100. package/src/array/arrayChunk.ts +7 -0
  101. package/src/array/index.ts +1 -0
  102. package/src/debug/debugFn/index.ts +48 -0
  103. package/src/debug/debugFn/util.ts +27 -0
  104. package/src/debug/deepFreeze.ts +26 -0
  105. package/src/debug/index.ts +3 -0
  106. package/src/debug/wait.ts +14 -0
  107. package/src/index.ts +9 -0
  108. package/src/interpolate/index.spec.ts +20 -0
  109. package/src/interpolate/index.ts +64 -0
  110. package/src/maybeFn.ts +3 -0
  111. package/src/promise/batchFn.spec.ts +92 -0
  112. package/src/promise/batchFn.ts +176 -0
  113. package/src/promise/index.ts +3 -0
  114. package/src/promise/promiseReduce.ts +15 -0
  115. package/src/promise/resolveNestedPromises.spec.ts +205 -0
  116. package/src/promise/resolveNestedPromises.ts +83 -0
  117. package/src/promise/types.ts +2 -0
  118. package/src/resolvePaths/index.spec.ts +42 -0
  119. package/src/resolvePaths/index.ts +21 -0
  120. package/src/switchValue.spec.ts +30 -0
  121. package/src/switchValue.ts +59 -0
  122. package/src/typeOf.spec.ts +47 -0
  123. package/src/typeOf.ts +81 -0
  124. package/src/validate/__snapshots__/index.spec.ts.snap +9 -0
  125. package/src/validate/async/index.spec.ts +236 -0
  126. package/src/validate/async/index.ts +52 -0
  127. package/src/validate/async/validateAsyncFn.ts +41 -0
  128. package/src/validate/async/validators/index.ts +2 -0
  129. package/src/validate/async/validators/logical.ts +56 -0
  130. package/src/validate/async/validators/shape.ts +160 -0
  131. package/src/validate/async/validators/tmpand.ts +24 -0
  132. package/src/validate/async/validators/tmpor.ts +21 -0
  133. package/src/validate/common/ValidationError.ts +33 -0
  134. package/src/validate/common/util/defaultErrorMessage.ts +26 -0
  135. package/src/validate/common/util/index.ts +3 -0
  136. package/src/validate/common/util/parseValidatorInput.ts +77 -0
  137. package/src/validate/common/util/resolveValidationResult.ts +64 -0
  138. package/src/validate/common/validators/index.ts +1 -0
  139. package/src/validate/common/validators/type.ts +101 -0
  140. package/src/validate/index.spec.ts +5 -0
  141. package/src/validate/index.ts +3 -0
  142. package/src/validate/specUtil/commonTests.js +413 -0
  143. package/src/validate/sync/index.spec.ts +81 -0
  144. package/src/validate/sync/index.ts +53 -0
  145. package/src/validate/sync/validateSyncFn.ts +46 -0
  146. package/src/validate/sync/validators/index.ts +2 -0
  147. package/src/validate/sync/validators/logical.ts +47 -0
  148. package/src/validate/sync/validators/shape.ts +146 -0
  149. package/src/validate/types/async.ts +20 -0
  150. package/src/validate/types/common.ts +70 -0
  151. package/src/validate/types/index.ts +3 -0
  152. package/src/validate/types/sync.ts +20 -0
  153. package/tsconfig.json +11 -0
  154. package/array/index.js +0 -11
  155. package/coverage/lcov-report/array/index.html +0 -93
  156. package/coverage/lcov-report/array/index.js.html +0 -98
  157. package/coverage/lcov-report/coverage/coverage-final.json.html +0 -95
  158. package/coverage/lcov-report/coverage/index.html +0 -93
  159. package/coverage/lcov-report/coverage/lcov-report/index.html +0 -106
  160. package/coverage/lcov-report/coverage/lcov-report/prettify.js.html +0 -68
  161. package/coverage/lcov-report/coverage/lcov-report/sorter.js.html +0 -539
  162. package/coverage/lcov-report/fn/index.html +0 -93
  163. package/coverage/lcov-report/fn/index.js.html +0 -215
  164. package/dist/index.js +0 -116
  165. package/fn/index.js +0 -50
@@ -0,0 +1,81 @@
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
+ })
@@ -0,0 +1,53 @@
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
@@ -0,0 +1,46 @@
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
+ }
@@ -0,0 +1,2 @@
1
+ export * from './logical'
2
+ export * from './shape'
@@ -0,0 +1,47 @@
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
+ }
@@ -0,0 +1,146 @@
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
+ }
@@ -0,0 +1,20 @@
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>
@@ -0,0 +1,70 @@
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
+ }
@@ -0,0 +1,3 @@
1
+ export * from './common'
2
+ export * from './sync'
3
+ export * from './async'
@@ -0,0 +1,20 @@
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 ADDED
@@ -0,0 +1,11 @@
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
+ }
package/array/index.js DELETED
@@ -1,11 +0,0 @@
1
- const arrayReduceAsync = (arr, fn, initial) => {
2
- return arr.reduce((previousPromise, item, index) => {
3
- return previousPromise.then(acc => {
4
- return Promise.resolve(fn(acc, item, index, arr))
5
- })
6
- }, Promise.resolve(initial))
7
- }
8
-
9
- module.exports = {
10
- arrayReduceAsync
11
- }
@@ -1,93 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <title>Code coverage report for array</title>
5
- <meta charset="utf-8" />
6
- <link rel="stylesheet" href="../prettify.css" />
7
- <link rel="stylesheet" href="../base.css" />
8
- <meta name="viewport" content="width=device-width, initial-scale=1">
9
- <style type='text/css'>
10
- .coverage-summary .sorter {
11
- background-image: url(../sort-arrow-sprite.png);
12
- }
13
- </style>
14
- </head>
15
- <body>
16
- <div class='wrapper'>
17
- <div class='pad1'>
18
- <h1>
19
- <a href="../index.html">All files</a> array
20
- </h1>
21
- <div class='clearfix'>
22
- <div class='fl pad1y space-right2'>
23
- <span class="strong">100% </span>
24
- <span class="quiet">Statements</span>
25
- <span class='fraction'>5/5</span>
26
- </div>
27
- <div class='fl pad1y space-right2'>
28
- <span class="strong">100% </span>
29
- <span class="quiet">Branches</span>
30
- <span class='fraction'>0/0</span>
31
- </div>
32
- <div class='fl pad1y space-right2'>
33
- <span class="strong">100% </span>
34
- <span class="quiet">Functions</span>
35
- <span class='fraction'>3/3</span>
36
- </div>
37
- <div class='fl pad1y space-right2'>
38
- <span class="strong">100% </span>
39
- <span class="quiet">Lines</span>
40
- <span class='fraction'>5/5</span>
41
- </div>
42
- </div>
43
- </div>
44
- <div class='status-line high'></div>
45
- <div class="pad1">
46
- <table class="coverage-summary">
47
- <thead>
48
- <tr>
49
- <th data-col="file" data-fmt="html" data-html="true" class="file">File</th>
50
- <th data-col="pic" data-type="number" data-fmt="html" data-html="true" class="pic"></th>
51
- <th data-col="statements" data-type="number" data-fmt="pct" class="pct">Statements</th>
52
- <th data-col="statements_raw" data-type="number" data-fmt="html" class="abs"></th>
53
- <th data-col="branches" data-type="number" data-fmt="pct" class="pct">Branches</th>
54
- <th data-col="branches_raw" data-type="number" data-fmt="html" class="abs"></th>
55
- <th data-col="functions" data-type="number" data-fmt="pct" class="pct">Functions</th>
56
- <th data-col="functions_raw" data-type="number" data-fmt="html" class="abs"></th>
57
- <th data-col="lines" data-type="number" data-fmt="pct" class="pct">Lines</th>
58
- <th data-col="lines_raw" data-type="number" data-fmt="html" class="abs"></th>
59
- </tr>
60
- </thead>
61
- <tbody><tr>
62
- <td class="file high" data-value="index.js"><a href="index.js.html">index.js</a></td>
63
- <td data-value="100" class="pic high"><div class="chart"><div class="cover-fill cover-full" style="width: 100%;"></div><div class="cover-empty" style="width:0%;"></div></div></td>
64
- <td data-value="100" class="pct high">100%</td>
65
- <td data-value="5" class="abs high">5/5</td>
66
- <td data-value="100" class="pct high">100%</td>
67
- <td data-value="0" class="abs high">0/0</td>
68
- <td data-value="100" class="pct high">100%</td>
69
- <td data-value="3" class="abs high">3/3</td>
70
- <td data-value="100" class="pct high">100%</td>
71
- <td data-value="5" class="abs high">5/5</td>
72
- </tr>
73
-
74
- </tbody>
75
- </table>
76
- </div><div class='push'></div><!-- for sticky footer -->
77
- </div><!-- /wrapper -->
78
- <div class='footer quiet pad2 space-top1 center small'>
79
- Code coverage
80
- generated by <a href="https://istanbul.js.org/" target="_blank">istanbul</a> at Thu Jun 13 2019 20:54:46 GMT-0300 (-03)
81
- </div>
82
- </div>
83
- <script src="../prettify.js"></script>
84
- <script>
85
- window.onload = function () {
86
- if (typeof prettyPrint === 'function') {
87
- prettyPrint();
88
- }
89
- };
90
- </script>
91
- <script src="../sorter.js"></script>
92
- </body>
93
- </html>