@cleverbrush/schema 0.0.10 → 0.0.11

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.
package/dist/index.d.ts CHANGED
@@ -56,6 +56,7 @@ export declare type StringSchemaDefinition<TObj> = Omit<SchemaDefintion<TObj>, '
56
56
  };
57
57
  export declare type ArraySchemaDefinition<TObj> = Omit<SchemaDefintion<TObj>, 'type'> & {
58
58
  type: 'array';
59
+ preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
59
60
  ofType?: Schema<any>;
60
61
  minLength?: number;
61
62
  maxLength?: number;
@@ -14,6 +14,17 @@ const validateArray = async (obj, schema, validator) => {
14
14
  valid: false,
15
15
  errors: ['expected type array']
16
16
  };
17
+ if (schema.preprocessor) {
18
+ const preprocessor = typeof schema.preprocessor === 'function'
19
+ ? schema.preprocessor
20
+ : validator.preprocessors.get(schema.preprocessor);
21
+ if (typeof preprocessor !== 'function') {
22
+ throw new Error(`unknown preprocessor '${schema.preprocessor}'`);
23
+ }
24
+ for (let i = 0; i < obj.length; i++) {
25
+ obj[i] = await Promise.resolve(preprocessor(obj[i]));
26
+ }
27
+ }
17
28
  if (typeof schema.minLength === 'number' && obj.length < schema.minLength) {
18
29
  return {
19
30
  valid: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleverbrush/schema",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "keywords": [
5
5
  "object schema validator",
6
6
  "schema",
@@ -19,7 +19,7 @@
19
19
  "build": "tsc"
20
20
  },
21
21
  "dependencies": {
22
- "@cleverbrush/deep": "0.0.10"
22
+ "@cleverbrush/deep": "0.0.11"
23
23
  },
24
24
  "types": "./dist/index.d.ts"
25
25
  }
package/src/index.ts CHANGED
@@ -96,6 +96,7 @@ export type ArraySchemaDefinition<TObj> = Omit<
96
96
  'type'
97
97
  > & {
98
98
  type: 'array';
99
+ preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
99
100
  ofType?: Schema<any>;
100
101
  minLength?: number;
101
102
  maxLength?: number;
@@ -1299,3 +1299,67 @@ test('Preprocessors - 2', async () => {
1299
1299
  });
1300
1300
  }).rejects.toBeInstanceOf(Error);
1301
1301
  });
1302
+
1303
+ test('Preprocessors - 3', async () => {
1304
+ const validator = new SchemaValidator().addSchemaType('Date', {
1305
+ validators: [
1306
+ (value) =>
1307
+ value instanceof Date && !Number.isNaN(value)
1308
+ ? {
1309
+ valid: true
1310
+ }
1311
+ : {
1312
+ valid: false,
1313
+ errors: ['should be a valid Date object']
1314
+ }
1315
+ ]
1316
+ });
1317
+
1318
+ validator.addPreprocessor('StringToDate', (value: unknown): Date => {
1319
+ const time = Date.parse(value.toString());
1320
+ if (Number.isNaN(time)) return undefined;
1321
+ return new Date(time);
1322
+ });
1323
+
1324
+ let obj = [new Date().toJSON()];
1325
+
1326
+ let result = await validator.validate(
1327
+ {
1328
+ preprocessor: 'StringToDate',
1329
+ type: 'array',
1330
+ ofType: 'Date'
1331
+ },
1332
+ obj
1333
+ );
1334
+ expect(result).toHaveProperty('valid', true);
1335
+
1336
+ obj = [new Date().toJSON()];
1337
+ result = await validator.validate(
1338
+ {
1339
+ preprocessor: (value: unknown): Date => {
1340
+ const time = Date.parse(value.toString());
1341
+ if (Number.isNaN(time)) return undefined;
1342
+ return new Date(time);
1343
+ },
1344
+ type: 'array',
1345
+ ofType: 'Date'
1346
+ },
1347
+ obj
1348
+ );
1349
+ expect(result).toHaveProperty('valid', true);
1350
+
1351
+ obj = ['sdfsdf12', new Date().toJSON()];
1352
+ result = await validator.validate(
1353
+ {
1354
+ preprocessor: (value: unknown): Date => {
1355
+ const time = Date.parse(value.toString());
1356
+ if (Number.isNaN(time)) return undefined;
1357
+ return new Date(time);
1358
+ },
1359
+ type: 'array',
1360
+ ofType: 'Date'
1361
+ },
1362
+ obj
1363
+ );
1364
+ expect(result).toHaveProperty('valid', false);
1365
+ });
@@ -24,6 +24,19 @@ export const validateArray = async (
24
24
  errors: ['expected type array']
25
25
  };
26
26
 
27
+ if (schema.preprocessor) {
28
+ const preprocessor =
29
+ typeof schema.preprocessor === 'function'
30
+ ? schema.preprocessor
31
+ : validator.preprocessors.get(schema.preprocessor);
32
+ if (typeof preprocessor !== 'function') {
33
+ throw new Error(`unknown preprocessor '${schema.preprocessor}'`);
34
+ }
35
+ for (let i = 0; i < obj.length; i++) {
36
+ obj[i] = await Promise.resolve(preprocessor(obj[i]));
37
+ }
38
+ }
39
+
27
40
  if (typeof schema.minLength === 'number' && obj.length < schema.minLength) {
28
41
  return {
29
42
  valid: false,