@cleverbrush/schema 0.0.15 → 0.0.16

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.
@@ -200,8 +200,10 @@ class SchemaValidator {
200
200
  return await this.validateDefaultType(schema.type, obj, schema);
201
201
  }
202
202
  else if (schema.type === 'alias') {
203
- const alias = this.schemas[schema.schemaName]
204
- .schema;
203
+ const alias = this._schemasMap.get(schema.schemaName);
204
+ if (typeof alias === 'undefined') {
205
+ throw new Error(`Unknown schema alias - ${schema.schemaName}`);
206
+ }
205
207
  if (Array.isArray(alias)) {
206
208
  if ((!schema.isRequired && typeof obj === 'undefined') ||
207
209
  (schema.isNullable && obj === null)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleverbrush/schema",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
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.15"
22
+ "@cleverbrush/deep": "0.0.16"
23
23
  },
24
24
  "types": "./dist/index.d.ts"
25
25
  }
@@ -1607,3 +1607,50 @@ test('Submodules - 2', async () => {
1607
1607
  });
1608
1608
  expect(result2).toHaveProperty('valid', true);
1609
1609
  });
1610
+
1611
+ test('Submodules - 3', async () => {
1612
+ const validator = new SchemaValidator()
1613
+ .addSchemaType('Module1.Schema1', {
1614
+ properties: {
1615
+ b: 'number'
1616
+ }
1617
+ })
1618
+ .addSchemaType('Module1.Schema2', {
1619
+ properties: {
1620
+ a: {
1621
+ type: 'alias',
1622
+ schemaName: 'Module1.Schema1'
1623
+ }
1624
+ }
1625
+ });
1626
+
1627
+ const result2 = await validator.schemas.Module1.Schema2.validate({
1628
+ a: {
1629
+ b: 20
1630
+ }
1631
+ });
1632
+ expect(result2).toHaveProperty('valid', true);
1633
+ });
1634
+
1635
+ test('Submodules - 4', async () => {
1636
+ const validator = new SchemaValidator()
1637
+ .addSchemaType('Module1.Schema1', {
1638
+ properties: {
1639
+ b: 'number'
1640
+ }
1641
+ })
1642
+ .addSchemaType('Module1.Schema2', {
1643
+ properties: {
1644
+ a: {
1645
+ type: 'alias',
1646
+ schemaName: 'Module1.Schema3'
1647
+ }
1648
+ }
1649
+ });
1650
+
1651
+ await validator.schemas.Module1.Schema2.validate({
1652
+ a: {
1653
+ b: 20
1654
+ }
1655
+ }).catch((e) => expect(e).toBeInstanceOf(Error));
1656
+ });
@@ -317,8 +317,12 @@ export default class SchemaValidator<
317
317
  schema
318
318
  );
319
319
  } else if (schema.type === 'alias') {
320
- const alias = this.schemas[schema.schemaName]
321
- .schema as Schema<any>;
320
+ const alias = this._schemasMap.get(schema.schemaName);
321
+ if (typeof alias === 'undefined') {
322
+ throw new Error(
323
+ `Unknown schema alias - ${schema.schemaName}`
324
+ );
325
+ }
322
326
  if (Array.isArray(alias)) {
323
327
  if (
324
328
  (!schema.isRequired && typeof obj === 'undefined') ||