@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.
- package/dist/cjs/string.js +15 -0
- package/dist/cjs/tuple.js +21 -5
- package/package.json +1 -1
- package/src/string.js +13 -0
- package/src/tuple.js +18 -5
- package/types/string.d.ts +4 -0
- package/types/string.d.ts.map +1 -1
- package/types/tuple.d.ts +1 -0
- package/types/tuple.d.ts.map +1 -1
package/dist/cjs/string.js
CHANGED
|
@@ -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',
|
|
35
|
+
this.assert('length', (arr, options) => {
|
|
36
|
+
const {
|
|
37
|
+
loose
|
|
38
|
+
} = options;
|
|
36
39
|
const {
|
|
37
40
|
length
|
|
38
41
|
} = schemas;
|
|
39
|
-
if (
|
|
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
|
|
49
|
+
return arr;
|
|
45
50
|
});
|
|
46
51
|
this.assert('elements', async (arr, options) => {
|
|
47
52
|
const errors = [];
|
|
48
53
|
const result = [];
|
|
49
|
-
|
|
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
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', (
|
|
26
|
+
this.assert('length', (arr, options) => {
|
|
27
|
+
const { loose } = options;
|
|
27
28
|
const { length } = schemas;
|
|
28
|
-
if (
|
|
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
|
|
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 <
|
|
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
package/types/string.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.js"],"names":[],"mappings":"
|
|
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
package/types/tuple.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../src/tuple.js"],"names":[],"mappings":"
|
|
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"}
|