@cleverbrush/schema 0.0.11 → 0.0.12

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
@@ -24,7 +24,7 @@ export declare type ObjectSchemaDefinition<T> = Omit<SchemaDefintion<T>, 'type'>
24
24
  [S in keyof T]: Schema<PropType<T, S>>;
25
25
  }>;
26
26
  preprocessors?: Partial<{
27
- [S in keyof T]: ((value: unknown) => PropType<T, S> | Promise<PropType<T, S>>) | string;
27
+ [S in keyof T | '*']: S extends keyof T ? ((value: unknown) => PropType<T, S> | Promise<PropType<T, S>>) | string : (value: T) => void | Promise<void>;
28
28
  }>;
29
29
  };
30
30
  export declare type AliasSchemaDefinition<T> = Omit<SchemaDefintion<T>, 'type'> & {
@@ -13,5 +13,5 @@ export default class SchemaValidator<T = Record<string, never>> implements ISche
13
13
  };
14
14
  private validateDefaultType;
15
15
  private checkValidators;
16
- validate(schema: keyof T | DefaultSchemaType | Schema<any>, obj: any): Promise<ValidationResult>;
16
+ validate<K>(schema: keyof T | DefaultSchemaType | Schema<K>, obj: any): Promise<ValidationResult>;
17
17
  }
@@ -20,7 +20,7 @@ const validateObject = async (obj, schema, validator) => {
20
20
  if (typeof schema.preprocessors === 'object' && schema.preprocessors) {
21
21
  await Promise.all(Object.keys(schema.preprocessors).map(async (key) => {
22
22
  if (typeof schema.preprocessors[key] === 'function') {
23
- obj[key] = await Promise.resolve(schema.preprocessors[key](obj[key]));
23
+ obj[key] = await Promise.resolve(schema.preprocessors[key](key === '*' ? obj : obj[key]));
24
24
  }
25
25
  else {
26
26
  const preprocessor = validator.preprocessors.get(schema.preprocessors[key]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleverbrush/schema",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
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.11"
22
+ "@cleverbrush/deep": "0.0.12"
23
23
  },
24
24
  "types": "./dist/index.d.ts"
25
25
  }
package/src/index.ts CHANGED
@@ -40,9 +40,13 @@ export type ObjectSchemaDefinition<T> = Omit<SchemaDefintion<T>, 'type'> & {
40
40
  [S in keyof T]: Schema<PropType<T, S>>;
41
41
  }>;
42
42
  preprocessors?: Partial<{
43
- [S in keyof T]:
44
- | ((value: unknown) => PropType<T, S> | Promise<PropType<T, S>>)
45
- | string;
43
+ [S in keyof T | '*']: S extends keyof T
44
+ ?
45
+ | ((
46
+ value: unknown
47
+ ) => PropType<T, S> | Promise<PropType<T, S>>)
48
+ | string
49
+ : (value: T) => void | Promise<void>;
46
50
  }>;
47
51
  };
48
52
 
@@ -4,6 +4,7 @@ import { ValidationResult } from '../dist/index.js';
4
4
  import {
5
5
  NumberSchemaDefinition,
6
6
  ObjectSchemaDefinitionParam,
7
+ ObjectSchemaDefinition,
7
8
  Schema
8
9
  } from './index';
9
10
  import SchemaValidator from './schemaValidator';
@@ -829,7 +830,7 @@ test('Validate - object - 1', async () => {
829
830
  b: 'number'
830
831
  },
831
832
  validators: [
832
- (value) => {
833
+ (value: { a: number; b: number }) => {
833
834
  if (value.a + value.b === 5) {
834
835
  return {
835
836
  valid: true
@@ -1363,3 +1364,95 @@ test('Preprocessors - 3', async () => {
1363
1364
  );
1364
1365
  expect(result).toHaveProperty('valid', false);
1365
1366
  });
1367
+
1368
+ test('Preprocessors - 3', async () => {
1369
+ const validator = new SchemaValidator();
1370
+
1371
+ type SomeType = { age: number; marriedAt: number };
1372
+
1373
+ const schema: ObjectSchemaDefinition<SomeType> = {
1374
+ type: 'object',
1375
+ properties: {
1376
+ age: 'number',
1377
+ marriedAt: 'number'
1378
+ },
1379
+ preprocessors: {
1380
+ '*': (value: SomeType): void => {
1381
+ if (value.marriedAt > value.age) {
1382
+ value.marriedAt = value.age;
1383
+ }
1384
+ }
1385
+ }
1386
+ };
1387
+
1388
+ const obj = {
1389
+ age: 50,
1390
+ marriedAt: 80
1391
+ };
1392
+ await validator.validate(schema, obj);
1393
+ expect(obj).toHaveProperty('marriedAt', 50);
1394
+ });
1395
+
1396
+ test('Preprocessors - 4', async () => {
1397
+ const validator = new SchemaValidator().addSchemaType('Date', {
1398
+ validators: [
1399
+ (value) =>
1400
+ value instanceof Date && !Number.isNaN(value)
1401
+ ? {
1402
+ valid: true
1403
+ }
1404
+ : {
1405
+ valid: false,
1406
+ errors: ['should be a valid Date object']
1407
+ }
1408
+ ]
1409
+ });
1410
+
1411
+ validator.addPreprocessor('StringToDate', (value: unknown): Date => {
1412
+ const time = Date.parse(value.toString());
1413
+ if (Number.isNaN(time)) return undefined;
1414
+ return new Date(time);
1415
+ });
1416
+
1417
+ let obj = [new Date().toJSON()];
1418
+
1419
+ let result = await validator.validate(
1420
+ {
1421
+ preprocessor: 'StringToDate',
1422
+ type: 'array',
1423
+ ofType: 'Date'
1424
+ },
1425
+ obj
1426
+ );
1427
+ expect(result).toHaveProperty('valid', true);
1428
+
1429
+ obj = [new Date().toJSON()];
1430
+ result = await validator.validate(
1431
+ {
1432
+ preprocessor: (value: unknown): Date => {
1433
+ const time = Date.parse(value.toString());
1434
+ if (Number.isNaN(time)) return undefined;
1435
+ return new Date(time);
1436
+ },
1437
+ type: 'array',
1438
+ ofType: 'Date'
1439
+ },
1440
+ obj
1441
+ );
1442
+ expect(result).toHaveProperty('valid', true);
1443
+
1444
+ obj = ['sdfsdf12', new Date().toJSON()];
1445
+ result = await validator.validate(
1446
+ {
1447
+ preprocessor: (value: unknown): Date => {
1448
+ const time = Date.parse(value.toString());
1449
+ if (Number.isNaN(time)) return undefined;
1450
+ return new Date(time);
1451
+ },
1452
+ type: 'array',
1453
+ ofType: 'Date'
1454
+ },
1455
+ obj
1456
+ );
1457
+ expect(result).toHaveProperty('valid', false);
1458
+ });
@@ -12,7 +12,8 @@ import {
12
12
  ArraySchemaDefinition,
13
13
  ISchemaValidator,
14
14
  DefaultSchemaType,
15
- ObjectSchemaDefinition
15
+ ObjectSchemaDefinition,
16
+ SchemaDefintion
16
17
  } from './index';
17
18
  import { validateNumber } from './validators/validateNumber';
18
19
  import { validateString } from './validators/validateString';
@@ -213,8 +214,8 @@ export default class SchemaValidator<T = Record<string, never>>
213
214
  };
214
215
  }
215
216
 
216
- public async validate(
217
- schema: keyof T | DefaultSchemaType | Schema<any>,
217
+ public async validate<K>(
218
+ schema: keyof T | DefaultSchemaType | Schema<K>,
218
219
  obj: any
219
220
  ): Promise<ValidationResult> {
220
221
  if (!schema) throw new Error('schemaName is required');
@@ -293,7 +294,7 @@ export default class SchemaValidator<T = Record<string, never>>
293
294
  'it is only possible to use a full schema schema definition as alias'
294
295
  );
295
296
 
296
- const schemaToMerge = { ...schema };
297
+ const schemaToMerge = { ...(schema as any) };
297
298
  delete schemaToMerge.type;
298
299
  delete schemaToMerge.schemaName;
299
300
  const finalSchema: Schema<any> = deepExtend(
@@ -34,7 +34,7 @@ export const validateObject = async (
34
34
  if (typeof schema.preprocessors[key] === 'function') {
35
35
  obj[key] = await Promise.resolve(
36
36
  (schema.preprocessors[key] as (unknown) => unknown)(
37
- obj[key]
37
+ key === '*' ? obj : obj[key]
38
38
  )
39
39
  );
40
40
  } else {