@cleverbrush/schema 0.0.8 → 0.0.9

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.
@@ -174,8 +174,15 @@ class SchemaValidator {
174
174
  else if (schema.type === 'alias') {
175
175
  const alias = this.schemas[schema.schemaName]
176
176
  .schema;
177
- if (Array.isArray(alias))
178
- throw new Error('it is impossible to use alternative schema alias as "type" field');
177
+ if (Array.isArray(alias)) {
178
+ if ((!schema.isRequired && typeof obj === 'undefined') ||
179
+ (schema.isNullable && obj === null)) {
180
+ return {
181
+ valid: true
182
+ };
183
+ }
184
+ return await this.validate(alias, obj);
185
+ }
179
186
  if (typeof alias !== 'object')
180
187
  throw new Error('it is only possible to use a full schema schema definition as alias');
181
188
  const schemaToMerge = { ...schema };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleverbrush/schema",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
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.8"
22
+ "@cleverbrush/deep": "0.0.9"
23
23
  },
24
24
  "types": "./dist/index.d.ts"
25
25
  }
@@ -1152,3 +1152,53 @@ test('Validate schema - 2', async () => {
1152
1152
 
1153
1153
  expect(result).toHaveProperty('valid', true);
1154
1154
  });
1155
+
1156
+ test('Not required alternative schema alias', async () => {
1157
+ const validator = new SchemaValidator()
1158
+ .addSchemaType('Alternate1', {
1159
+ properties: {
1160
+ a1: 'string'
1161
+ }
1162
+ })
1163
+ .addSchemaType('Alternate2', {
1164
+ properties: {
1165
+ a2: 'string'
1166
+ }
1167
+ })
1168
+ .addSchemaType('Alternate', ['Alternate1', 'Alternate2']);
1169
+
1170
+ let result = await validator.validate(
1171
+ {
1172
+ type: 'alias',
1173
+ isRequired: false,
1174
+ schemaName: 'Alternate'
1175
+ },
1176
+ undefined
1177
+ );
1178
+
1179
+ expect(result).toHaveProperty('valid', true);
1180
+
1181
+ result = await validator.validate(
1182
+ {
1183
+ type: 'alias',
1184
+ isRequired: false,
1185
+ schemaName: 'Alternate'
1186
+ },
1187
+ {
1188
+ a2: 'something'
1189
+ }
1190
+ );
1191
+
1192
+ expect(result).toHaveProperty('valid', true);
1193
+
1194
+ result = await validator.validate(
1195
+ {
1196
+ type: 'alias',
1197
+ isRequired: false,
1198
+ schemaName: 'Alternate'
1199
+ },
1200
+ 'invalid'
1201
+ );
1202
+
1203
+ expect(result).toHaveProperty('valid', false);
1204
+ });
@@ -259,10 +259,17 @@ export default class SchemaValidator<T = Record<string, never>>
259
259
  } else if (schema.type === 'alias') {
260
260
  const alias = this.schemas[schema.schemaName]
261
261
  .schema as Schema<any>;
262
- if (Array.isArray(alias))
263
- throw new Error(
264
- 'it is impossible to use alternative schema alias as "type" field'
265
- );
262
+ if (Array.isArray(alias)) {
263
+ if (
264
+ (!schema.isRequired && typeof obj === 'undefined') ||
265
+ (schema.isNullable && obj === null)
266
+ ) {
267
+ return {
268
+ valid: true
269
+ };
270
+ }
271
+ return await this.validate(alias, obj);
272
+ }
266
273
  if (typeof alias !== 'object')
267
274
  throw new Error(
268
275
  'it is only possible to use a full schema schema definition as alias'