@rosen-bridge/config 0.4.0 → 1.1.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.
@@ -1,273 +0,0 @@
1
- import { ConfigValidator } from '../config';
2
- import * as types from '../schema/types/fields';
3
- import {
4
- VChoices,
5
- VGreater,
6
- VGreaterEqual,
7
- VLess,
8
- VLessEqual,
9
- VRegex,
10
- VRequired,
11
- } from '../schema/types/validations';
12
-
13
- export const valueValidators: Record<string, any> = {
14
- object: (value: Record<string, any>, field: types.ObjectField) => {
15
- if (typeof value !== 'object') {
16
- throw new Error(`value must be of object type`);
17
- }
18
-
19
- const schemaKeys = new Set(Object.keys(field.children));
20
- for (const key of Object.keys(value)) {
21
- if (!schemaKeys.has(key)) {
22
- throw new Error(`"${key}" key is not found in the schema`);
23
- }
24
- }
25
- },
26
- array: (value: Array<any>, field: types.ArrayField) => {
27
- if (!Array.isArray(value)) {
28
- throw new Error(`value must be of array type`);
29
- }
30
- },
31
- string: (value: string, field: types.StringField) => {
32
- if (typeof value !== 'string') {
33
- throw new Error(`value must be of string type`);
34
- }
35
- },
36
- boolean: (value: boolean, field: types.BooleanField) => {
37
- if (typeof value !== 'boolean') {
38
- throw new Error(`value must be of boolean type`);
39
- }
40
- },
41
- number: (value: number, field: types.NumberField) => {
42
- if (typeof value !== 'number') {
43
- throw new Error(`value must be of number type`);
44
- }
45
- },
46
- bigint: (value: bigint, field: types.BigIntField) => {
47
- if (typeof value !== 'bigint') {
48
- throw new Error(`value must be of bigint type`);
49
- }
50
- },
51
- };
52
-
53
- const required = (
54
- value: any,
55
- validation: VRequired,
56
- config: Record<string, any>,
57
- configValidator: ConfigValidator
58
- ) => {
59
- if (validation.when && !configValidator.isWhenTrue(validation.when, config)) {
60
- return;
61
- }
62
-
63
- if (validation.required && value == undefined) {
64
- throw new Error('value is required but not found in config');
65
- }
66
- };
67
-
68
- export const valueValidations: Record<string, Record<string, any>> = {
69
- boolean: { required },
70
- string: {
71
- required,
72
- regex: (
73
- value: string,
74
- validation: VRegex,
75
- config: Record<string, any>,
76
- configValidator: ConfigValidator
77
- ) => {
78
- if (
79
- value == undefined ||
80
- (validation.when &&
81
- !configValidator.isWhenTrue(validation.when, config))
82
- ) {
83
- return;
84
- }
85
-
86
- const re = new RegExp(validation.regex);
87
- const match = value.match(re);
88
- if (match == null || match[0] !== value) {
89
- throw new Error(`value should match the regex="${validation.regex}"`);
90
- }
91
- },
92
- choices: (
93
- value: string,
94
- validation: VChoices,
95
- config: Record<string, any>,
96
- configValidator: ConfigValidator
97
- ) => {
98
- if (
99
- value == undefined ||
100
- (validation.when &&
101
- !configValidator.isWhenTrue(validation.when, config))
102
- ) {
103
- return;
104
- }
105
-
106
- if (!validation.choices.includes(value)) {
107
- throw new Error(
108
- `value should be one of the choices=[${validation.choices.join(
109
- ', '
110
- )}]`
111
- );
112
- }
113
- },
114
- },
115
- number: {
116
- required,
117
- gt: (
118
- value: number,
119
- validation: VGreater<number>,
120
- config: Record<string, any>,
121
- configValidator: ConfigValidator
122
- ) => {
123
- if (
124
- value == undefined ||
125
- (validation.when &&
126
- !configValidator.isWhenTrue(validation.when, config))
127
- ) {
128
- return;
129
- }
130
-
131
- if (value <= validation.gt) {
132
- throw new Error(`value should be greater than ${validation.gt}`);
133
- }
134
- },
135
- gte: (
136
- value: number,
137
- validation: VGreaterEqual<number>,
138
- config: Record<string, any>,
139
- configValidator: ConfigValidator
140
- ) => {
141
- if (
142
- value == undefined ||
143
- (validation.when &&
144
- !configValidator.isWhenTrue(validation.when, config))
145
- ) {
146
- return;
147
- }
148
-
149
- if (value < validation.gte) {
150
- throw new Error(
151
- `value should be greater than or equal to ${validation.gte}`
152
- );
153
- }
154
- },
155
- lt: (
156
- value: number,
157
- validation: VLess<number>,
158
- config: Record<string, any>,
159
- configValidator: ConfigValidator
160
- ) => {
161
- if (
162
- value == undefined ||
163
- (validation.when &&
164
- !configValidator.isWhenTrue(validation.when, config))
165
- ) {
166
- return;
167
- }
168
-
169
- if (value >= validation.lt) {
170
- throw new Error(`value should be less than ${validation.lt}`);
171
- }
172
- },
173
- lte: (
174
- value: number,
175
- validation: VLessEqual<number>,
176
- config: Record<string, any>,
177
- configValidator: ConfigValidator
178
- ) => {
179
- if (
180
- value == undefined ||
181
- (validation.when &&
182
- !configValidator.isWhenTrue(validation.when, config))
183
- ) {
184
- return;
185
- }
186
-
187
- if (value > validation.lte) {
188
- throw new Error(
189
- `value should be less than or equal to ${validation.lte}`
190
- );
191
- }
192
- },
193
- },
194
- bigint: {
195
- required,
196
- gt: (
197
- value: bigint,
198
- validation: VGreater<bigint>,
199
- config: Record<string, any>,
200
- configValidator: ConfigValidator
201
- ) => {
202
- if (
203
- value == undefined ||
204
- (validation.when &&
205
- !configValidator.isWhenTrue(validation.when, config))
206
- ) {
207
- return;
208
- }
209
-
210
- if (value <= validation.gt) {
211
- throw new Error(`value should be greater than ${validation.gt}`);
212
- }
213
- },
214
- gte: (
215
- value: bigint,
216
- validation: VGreaterEqual<bigint>,
217
- config: Record<string, any>,
218
- configValidator: ConfigValidator
219
- ) => {
220
- if (
221
- value == undefined ||
222
- (validation.when &&
223
- !configValidator.isWhenTrue(validation.when, config))
224
- ) {
225
- return;
226
- }
227
-
228
- if (value < validation.gte) {
229
- throw new Error(
230
- `value should be greater than or equal to ${validation.gte}`
231
- );
232
- }
233
- },
234
- lt: (
235
- value: bigint,
236
- validation: VLess<bigint>,
237
- config: Record<string, any>,
238
- configValidator: ConfigValidator
239
- ) => {
240
- if (
241
- value == undefined ||
242
- (validation.when &&
243
- !configValidator.isWhenTrue(validation.when, config))
244
- ) {
245
- return;
246
- }
247
-
248
- if (value >= validation.lt) {
249
- throw new Error(`value should be less than ${validation.lt}`);
250
- }
251
- },
252
- lte: (
253
- value: bigint,
254
- validation: VLessEqual<bigint>,
255
- config: Record<string, any>,
256
- configValidator: ConfigValidator
257
- ) => {
258
- if (
259
- value == undefined ||
260
- (validation.when &&
261
- !configValidator.isWhenTrue(validation.when, config))
262
- ) {
263
- return;
264
- }
265
-
266
- if (value > validation.lte) {
267
- throw new Error(
268
- `value should be less than or equal to ${validation.lte}`
269
- );
270
- }
271
- },
272
- },
273
- };
package/tests/.gitkeep DELETED
File without changes