@cleverbrush/schema 0.0.3 → 0.0.6

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/README.md CHANGED
@@ -23,7 +23,7 @@ Any schema defined by object can contain the following fields:
23
23
  - `type` - schema type (see the list above)
24
24
  - `isRequired` - defines if `undefined` value is considered valid
25
25
  - `isNullable` - defines if `null` value is considered valid
26
- - `validators` - optional array of custom validation functions (see [Examples](#examples))
26
+ - `validators` - optional array of custom validation functions (see Examples section at the bottom of this page)
27
27
 
28
28
  ### Number
29
29
 
@@ -274,6 +274,6 @@ There is a possibility to register a schema, give it a name and then reuse it:
274
274
 
275
275
  // { valid: true }
276
276
 
277
- ## Examples {#examples}
277
+ ## Examples
278
278
 
279
279
  For Examples see unit tests in the schemaValidator.tests.ts
package/dist/index.d.ts CHANGED
@@ -66,8 +66,8 @@ export interface ISchemasProvider<T = Record<string, never>> {
66
66
  };
67
67
  }
68
68
  export interface ISchemaValidator<T = Record<string, never>> {
69
- addSchemaType<K, L extends ObjectSchemaDefinitionParam<M>, M = any>(name: keyof K, schema: L): SchemaValidator<T & {
70
- [key in keyof K]: L;
69
+ addSchemaType<K, L extends ObjectSchemaDefinitionParam<M>, M = any>(name: keyof K, schema: L | Array<Schema<any>>): SchemaValidator<T & {
70
+ [key in keyof K]: typeof schema;
71
71
  }>;
72
72
  validate(schema: keyof T | DefaultSchemaType | Schema<any>, obj: any): Promise<ValidationResult>;
73
73
  }
@@ -2,7 +2,7 @@ import { ISchemasProvider, Schema, ISchemaActions, ObjectSchemaDefinitionParam,
2
2
  export default class SchemaValidator<T = Record<string, never>> implements ISchemasProvider<T>, ISchemaValidator<T> {
3
3
  private _schemasMap;
4
4
  private _schemasCache;
5
- addSchemaType<K, L extends ObjectSchemaDefinitionParam<M>, M = any>(name: keyof K, schema: L): SchemaValidator<T & {
5
+ addSchemaType<K, L extends ObjectSchemaDefinitionParam<M>, M = any>(name: keyof K, schema: L | Array<Schema<any>>): SchemaValidator<T & {
6
6
  [key in keyof K]: L;
7
7
  }>;
8
8
  get schemas(): {
@@ -40,13 +40,18 @@ class SchemaValidator {
40
40
  addSchemaType(name, schema) {
41
41
  if (typeof name !== 'string' || !name)
42
42
  throw new Error('Name is required');
43
- if (typeof schema !== 'object')
44
- throw new Error('Object is required');
45
43
  if (isDefaultType(name.toString()))
46
44
  throw new Error(`You can't add a schema named "${name}" because it's a name of a default schema, please consider another name to be used`);
47
45
  if (this._schemasMap.has(name.toString())) {
48
46
  throw new Error(`Schema "${name}" already exists`);
49
47
  }
48
+ if (Array.isArray(schema)) {
49
+ this._schemasMap.set(name.toString(), schema);
50
+ this._schemasCache = null;
51
+ return this;
52
+ }
53
+ if (typeof schema !== 'object')
54
+ throw new Error('Array or Object is required');
50
55
  this._schemasMap.set(name.toString(), {
51
56
  ...schema,
52
57
  type: 'object'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleverbrush/schema",
3
- "version": "0.0.3",
3
+ "version": "0.0.6",
4
4
  "keywords": [
5
5
  "object schema validator",
6
6
  "schema",
@@ -9,12 +9,17 @@
9
9
  "author": "Andrew Zolotukhin <andrew_zol@cleverbrush.com>",
10
10
  "license": "BSD",
11
11
  "main": "./dist/index.js",
12
+ "readme": "https://github.com/cleverbrush/libs/tree/master/libs/schema#readme",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "github:cleverbrush/libs"
16
+ },
12
17
  "scripts": {
13
18
  "watch": "tsc --watch",
14
19
  "build": "tsc"
15
20
  },
16
21
  "dependencies": {
17
- "@cleverbrush/deep": "0.0.3"
22
+ "@cleverbrush/deep": "0.0.6"
18
23
  },
19
24
  "types": "./dist/index.d.ts"
20
25
  }
package/src/index.ts CHANGED
@@ -124,8 +124,8 @@ export interface ISchemasProvider<T = Record<string, never>> {
124
124
  export interface ISchemaValidator<T = Record<string, never>> {
125
125
  addSchemaType<K, L extends ObjectSchemaDefinitionParam<M>, M = any>(
126
126
  name: keyof K,
127
- schema: L
128
- ): SchemaValidator<T & { [key in keyof K]: L }>;
127
+ schema: L | Array<Schema<any>>
128
+ ): SchemaValidator<T & { [key in keyof K]: typeof schema }>;
129
129
 
130
130
  validate(
131
131
  schema: keyof T | DefaultSchemaType | Schema<any>,
@@ -97,39 +97,39 @@ test('Schemas property is updated after adding a new schema', () => {
97
97
  });
98
98
 
99
99
  test('Trows when trying to add a schema with name = "number"', () => {
100
- let validator = new SchemaValidator();
100
+ const validator = new SchemaValidator();
101
101
  expect(() => validator.addSchemaType('number', {})).toThrow();
102
102
  });
103
103
 
104
104
  test('Trows when trying to add a schema with name = "array"', () => {
105
- let validator = new SchemaValidator();
105
+ const validator = new SchemaValidator();
106
106
  expect(() => validator.addSchemaType('array', {})).toThrow();
107
107
  });
108
108
 
109
109
  test('Trows when trying to add a schema with name = "date"', () => {
110
- let validator = new SchemaValidator();
110
+ const validator = new SchemaValidator();
111
111
  expect(() => validator.addSchemaType('date', {})).toThrow();
112
112
  });
113
113
 
114
114
  test('Trows when trying to add a schema with name = "string"', () => {
115
- let validator = new SchemaValidator();
115
+ const validator = new SchemaValidator();
116
116
  expect(() => validator.addSchemaType('string', {})).toThrow();
117
117
  });
118
118
 
119
119
  test('Throws if schema name is empty', () => {
120
- let validator = new SchemaValidator();
120
+ const validator = new SchemaValidator();
121
121
  expect(() => validator.addSchemaType('', {})).toThrow();
122
122
  });
123
123
 
124
124
  test('Throws if schema name is not a string', () => {
125
- let validator = new SchemaValidator();
125
+ const validator = new SchemaValidator();
126
126
  expect(() =>
127
127
  validator.addSchemaType(new Date() as any as string, {})
128
128
  ).toThrow();
129
129
  });
130
130
 
131
131
  test('Throws if schema is not an object', () => {
132
- let validator = new SchemaValidator();
132
+ const validator = new SchemaValidator();
133
133
  expect(() =>
134
134
  validator.addSchemaType(
135
135
  'string',
@@ -138,6 +138,33 @@ test('Throws if schema is not an object', () => {
138
138
  ).toThrow();
139
139
  });
140
140
 
141
+ test('Array as schema type', async () => {
142
+ const validator = new SchemaValidator()
143
+ .addSchemaType('name', {
144
+ properties: {
145
+ name: 'string'
146
+ }
147
+ })
148
+ .addSchemaType('number_or_string', ['number', 'string', 'name']);
149
+
150
+ let result = await validator.schemas.number_or_string.validate(123);
151
+
152
+ expect(result).toHaveProperty('valid', true);
153
+
154
+ result = await validator.schemas.number_or_string.validate('some string');
155
+
156
+ expect(result).toHaveProperty('valid', true);
157
+
158
+ result = await validator.schemas.number_or_string.validate({});
159
+
160
+ expect(result).toHaveProperty('valid', false);
161
+
162
+ result = await validator.schemas.number_or_string.validate({
163
+ name: 'some name'
164
+ });
165
+ expect(result).toHaveProperty('valid', true);
166
+ });
167
+
141
168
  test('Validate - no schema', async () => {
142
169
  const validator = new SchemaValidator();
143
170
  const cth = jest.fn();
@@ -741,7 +768,7 @@ test('Validate - array - ofType - 1', async () => {
741
768
  });
742
769
 
743
770
  test('Validate - object - 1', async () => {
744
- let validator = new SchemaValidator().addSchemaType(
771
+ const validator = new SchemaValidator().addSchemaType(
745
772
  'user',
746
773
  getUserSchema()
747
774
  );
@@ -824,7 +851,7 @@ test('Validate - object - 1', async () => {
824
851
  });
825
852
 
826
853
  test('Validate - object - 2', async () => {
827
- let validator = new SchemaValidator().addSchemaType(
854
+ const validator = new SchemaValidator().addSchemaType(
828
855
  'user',
829
856
  deepExtend(getUserSchema(), {
830
857
  validators: [
@@ -853,7 +880,7 @@ test('Validate - object - 2', async () => {
853
880
  aliases: []
854
881
  };
855
882
 
856
- let result = await validator.validate('user', user);
883
+ const result = await validator.validate('user', user);
857
884
  expect(result).toHaveProperty('valid', false);
858
885
  });
859
886
 
@@ -73,11 +73,11 @@ export default class SchemaValidator<T = Record<string, never>>
73
73
 
74
74
  public addSchemaType<K, L extends ObjectSchemaDefinitionParam<M>, M = any>(
75
75
  name: keyof K,
76
- schema: L
76
+ schema: L | Array<Schema<any>>
77
77
  ): SchemaValidator<T & { [key in keyof K]: L }> {
78
78
  if (typeof name !== 'string' || !name)
79
79
  throw new Error('Name is required');
80
- if (typeof schema !== 'object') throw new Error('Object is required');
80
+
81
81
  if (isDefaultType(name.toString()))
82
82
  throw new Error(
83
83
  `You can't add a schema named "${name}" because it's a name of a default schema, please consider another name to be used`
@@ -86,6 +86,14 @@ export default class SchemaValidator<T = Record<string, never>>
86
86
  throw new Error(`Schema "${name}" already exists`);
87
87
  }
88
88
 
89
+ if (Array.isArray(schema)) {
90
+ this._schemasMap.set(name.toString(), schema as Schema<any>);
91
+ this._schemasCache = null;
92
+ return this as any as SchemaValidator<T & { [key in keyof K]: L }>;
93
+ }
94
+
95
+ if (typeof schema !== 'object')
96
+ throw new Error('Array or Object is required');
89
97
  this._schemasMap.set(name.toString(), {
90
98
  ...schema,
91
99
  type: 'object'