@bedrockio/yada 1.0.17 → 1.0.19

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.
@@ -25,6 +25,21 @@ class StringSchema extends _TypeSchema.default {
25
25
  });
26
26
  }
27
27
 
28
+ /**
29
+ * @param {number} length
30
+ */
31
+ length(length) {
32
+ return this.clone({
33
+ length
34
+ }).assert('length', str => {
35
+ if (str && str.length !== length) {
36
+ throw new _errors.LocalizedError('Must be exactly {length} characters.', {
37
+ length
38
+ });
39
+ }
40
+ });
41
+ }
42
+
28
43
  /**
29
44
  * @param {number} length
30
45
  */
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.19",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/string.js CHANGED
@@ -27,6 +27,19 @@ class StringSchema extends TypeSchema {
27
27
  });
28
28
  }
29
29
 
30
+ /**
31
+ * @param {number} length
32
+ */
33
+ length(length) {
34
+ return this.clone({ length }).assert('length', (str) => {
35
+ if (str && str.length !== length) {
36
+ throw new LocalizedError('Must be exactly {length} characters.', {
37
+ length,
38
+ });
39
+ }
40
+ });
41
+ }
42
+
30
43
  /**
31
44
  * @param {number} length
32
45
  */
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/string.d.ts CHANGED
@@ -4,6 +4,10 @@
4
4
  export default function _default(): StringSchema;
5
5
  declare class StringSchema extends TypeSchema {
6
6
  constructor();
7
+ /**
8
+ * @param {number} length
9
+ */
10
+ length(length: number): StringSchema;
7
11
  /**
8
12
  * @param {number} length
9
13
  */
@@ -1 +1 @@
1
- {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.js"],"names":[],"mappings":"AA4WA;;GAEG;AACH,iDAEC;AAlWD;IACE,cAWC;IAED;;OAEG;IACH,YAFW,MAAM,gBAUhB;IAED;;OAEG;IACH,YAFW,MAAM,gBAUhB;IAED,qBAIC;IAED;;OAEG;IACH,mBAFW,OAAO,gBAYjB;IAED;;OAEG;IACH,mBAFW,OAAO,gBAYjB;IAED;;OAEG;IACH,WAFW,MAAM,gBAahB;IAED,sBAMC;IAED,sBAMC;IAED,oBAMC;IAED,oBAMC;IAED,qBAMC;IAED,sBAMC;IAED;;;OAGG;IACH;QAF6B,OAAO,GAAzB,OAAO;qBAQjB;IAED,2BAMC;IAED,mBAMC;IAED,wBAMC;IAED,uBAMC;IAED,oBAMC;IAED,qBAOC;IAED,uBAMC;IAED;;OAEG;IACH,oBAFW,MAAM,gBAQhB;IAED;;;;;;;OAOG;IACH;QAN4B,SAAS,GAA1B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,YAAY,GAA7B,MAAM;QACW,YAAY,GAA7B,MAAM;qBA2BhB;IAED;;;;;;;;;;;OAWG;IACH;QAV6B,gBAAgB,GAAlC,OAAO;QACW,sBAAsB,GAAxC,OAAO;QACW,YAAY,GAA9B,OAAO;QACW,YAAY,GAA9B,OAAO;QACW,4BAA4B,GAA9C,OAAO;QACW,eAAe,GAAjC,OAAO;QACW,sBAAsB,GAAxC,OAAO;QACW,eAAe,GAAjC,OAAO;QACY,SAAS,GAA5B,MAAM,EAAE;qBAQlB;IAED;;;;;;;;OAQG;IACH;QAP6B,WAAW,GAA7B,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,kBAAkB,GAApC,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,cAAc,GAAhC,OAAO;QACW,iBAAiB,GAAnC,OAAO;qBAQjB;IAED;;OAEG;IACH,eAFW,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,gBAQ3B;IAED,oBAMC;IAED,oBAMC;IAED,sBAMC;IAED,sBAMC;CAcF"}
1
+ {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.js"],"names":[],"mappings":"AAyXA;;GAEG;AACH,iDAEC;AA/WD;IACE,cAWC;IAED;;OAEG;IACH,eAFW,MAAM,gBAUhB;IAED;;OAEG;IACH,YAFW,MAAM,gBAUhB;IAED;;OAEG;IACH,YAFW,MAAM,gBAUhB;IAED,qBAIC;IAED;;OAEG;IACH,mBAFW,OAAO,gBAYjB;IAED;;OAEG;IACH,mBAFW,OAAO,gBAYjB;IAED;;OAEG;IACH,WAFW,MAAM,gBAahB;IAED,sBAMC;IAED,sBAMC;IAED,oBAMC;IAED,oBAMC;IAED,qBAMC;IAED,sBAMC;IAED;;;OAGG;IACH;QAF6B,OAAO,GAAzB,OAAO;qBAQjB;IAED,2BAMC;IAED,mBAMC;IAED,wBAMC;IAED,uBAMC;IAED,oBAMC;IAED,qBAOC;IAED,uBAMC;IAED;;OAEG;IACH,oBAFW,MAAM,gBAQhB;IAED;;;;;;;OAOG;IACH;QAN4B,SAAS,GAA1B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,YAAY,GAA7B,MAAM;QACW,YAAY,GAA7B,MAAM;qBA2BhB;IAED;;;;;;;;;;;OAWG;IACH;QAV6B,gBAAgB,GAAlC,OAAO;QACW,sBAAsB,GAAxC,OAAO;QACW,YAAY,GAA9B,OAAO;QACW,YAAY,GAA9B,OAAO;QACW,4BAA4B,GAA9C,OAAO;QACW,eAAe,GAAjC,OAAO;QACW,sBAAsB,GAAxC,OAAO;QACW,eAAe,GAAjC,OAAO;QACY,SAAS,GAA5B,MAAM,EAAE;qBAQlB;IAED;;;;;;;;OAQG;IACH;QAP6B,WAAW,GAA7B,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,kBAAkB,GAApC,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,cAAc,GAAhC,OAAO;QACW,iBAAiB,GAAnC,OAAO;qBAQjB;IAED;;OAEG;IACH,eAFW,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,gBAQ3B;IAED,oBAMC;IAED,oBAMC;IAED,sBAMC;IAED,sBAMC;CAcF"}
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"}