@bedrockio/yada 1.0.17 → 1.0.18

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/cjs/tuple.js CHANGED
@@ -32,24 +32,35 @@ class TupleSchema extends _Schema.default {
32
32
  }
33
33
  return val;
34
34
  });
35
- this.assert('length', val => {
35
+ this.assert('length', (arr, options) => {
36
+ const {
37
+ loose
38
+ } = options;
36
39
  const {
37
40
  length
38
41
  } = schemas;
39
- if (val.length !== length) {
42
+ if (loose && arr.length === 0) {
43
+ // Empty arrays allowed with loose option so take no action.
44
+ } else if (arr.length !== length) {
40
45
  throw new _errors.LocalizedError('Tuple must be exactly {length} elements.', {
41
46
  length: schemas.length
42
47
  });
43
48
  }
44
- return val;
49
+ return arr;
45
50
  });
46
51
  this.assert('elements', async (arr, options) => {
47
52
  const errors = [];
48
53
  const result = [];
49
- for (let i = 0; i < schemas.length; i++) {
54
+ const min = Math.min(arr.length, schemas.length);
55
+ for (let i = 0; i < min; i++) {
56
+ let el = arr[i];
50
57
  const schema = schemas[i];
51
- const el = arr[i];
52
58
  try {
59
+ if (el === undefined) {
60
+ // Coerce undefined to null to make sure validation
61
+ // is run but keep the same error message.
62
+ el = null;
63
+ }
53
64
  result.push(await schema.validate(el, options));
54
65
  } catch (error) {
55
66
  if (error.details?.length === 1) {
@@ -66,6 +77,11 @@ class TupleSchema extends _Schema.default {
66
77
  }
67
78
  });
68
79
  }
80
+ loose() {
81
+ return this.clone({
82
+ loose: true
83
+ });
84
+ }
69
85
  toString() {
70
86
  return 'tuple';
71
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/tuple.js CHANGED
@@ -23,24 +23,33 @@ class TupleSchema extends Schema {
23
23
  return val;
24
24
  });
25
25
 
26
- this.assert('length', (val) => {
26
+ this.assert('length', (arr, options) => {
27
+ const { loose } = options;
27
28
  const { length } = schemas;
28
- if (val.length !== length) {
29
+ if (loose && arr.length === 0) {
30
+ // Empty arrays allowed with loose option so take no action.
31
+ } else if (arr.length !== length) {
29
32
  throw new LocalizedError('Tuple must be exactly {length} elements.', {
30
33
  length: schemas.length,
31
34
  });
32
35
  }
33
- return val;
36
+ return arr;
34
37
  });
35
38
 
36
39
  this.assert('elements', async (arr, options) => {
37
40
  const errors = [];
38
41
  const result = [];
42
+ const min = Math.min(arr.length, schemas.length);
39
43
 
40
- for (let i = 0; i < schemas.length; i++) {
44
+ for (let i = 0; i < min; i++) {
45
+ let el = arr[i];
41
46
  const schema = schemas[i];
42
- const el = arr[i];
43
47
  try {
48
+ if (el === undefined) {
49
+ // Coerce undefined to null to make sure validation
50
+ // is run but keep the same error message.
51
+ el = null;
52
+ }
44
53
  result.push(await schema.validate(el, options));
45
54
  } catch (error) {
46
55
  if (error.details?.length === 1) {
@@ -60,6 +69,10 @@ class TupleSchema extends Schema {
60
69
  });
61
70
  }
62
71
 
72
+ loose() {
73
+ return this.clone({ loose: true });
74
+ }
75
+
63
76
  toString() {
64
77
  return 'tuple';
65
78
  }
package/types/tuple.d.ts CHANGED
@@ -13,6 +13,7 @@ declare class TupleSchema extends Schema {
13
13
  * @private
14
14
  */
15
15
  private setup;
16
+ loose(): TupleSchema;
16
17
  }
17
18
  export {};
18
19
  //# sourceMappingURL=tuple.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../src/tuple.js"],"names":[],"mappings":"AA8EA;;;;;;GAMG;AACH,8CAJc,MAAM,iBASnB;;AAvFD;IACE,0BAGC;IAED;;OAEG;IACH,cAgDC;CAgBF"}
1
+ {"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../src/tuple.js"],"names":[],"mappings":"AA2FA;;;;;;GAMG;AACH,8CAJc,MAAM,iBASnB;;AApGD;IACE,0BAGC;IAED;;OAEG;IACH,cAyDC;IAED,qBAEC;CAgBF"}