@bedrockio/yada 1.2.8 → 1.3.0
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/CHANGELOG.md +8 -0
- package/dist/cjs/Schema.js +6 -0
- package/dist/cjs/object.js +27 -0
- package/dist/cjs/string.js +1 -1
- package/package.json +1 -1
- package/src/Schema.js +5 -0
- package/src/object.js +29 -0
- package/src/string.js +1 -1
- package/types/Schema.d.ts +1 -0
- package/types/Schema.d.ts.map +1 -1
- package/types/object.d.ts +8 -0
- package/types/object.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
package/dist/cjs/Schema.js
CHANGED
package/dist/cjs/object.js
CHANGED
|
@@ -149,6 +149,32 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
/**
|
|
152
|
+
* Gets the schema for the given field. Accepts either a string
|
|
153
|
+
* separated by "." or an array of path names.
|
|
154
|
+
* @param {string|Array<string>} [path] The path of the field.
|
|
155
|
+
*/
|
|
156
|
+
get(path) {
|
|
157
|
+
const {
|
|
158
|
+
fields
|
|
159
|
+
} = this.meta;
|
|
160
|
+
if (!fields) {
|
|
161
|
+
throw new Error('Cannot select field on an open object schema.');
|
|
162
|
+
}
|
|
163
|
+
path = Array.isArray(path) ? path : path.split('.');
|
|
164
|
+
const [name, ...rest] = path;
|
|
165
|
+
const schema = fields[name];
|
|
166
|
+
if (!schema) {
|
|
167
|
+
throw new Error(`Cannot find field "${name}".`);
|
|
168
|
+
}
|
|
169
|
+
if (rest.length) {
|
|
170
|
+
return schema.get(rest);
|
|
171
|
+
} else {
|
|
172
|
+
return schema;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Returns a new schema that only validates the selected fields.
|
|
152
178
|
* @param {...string} [names] Names to include.
|
|
153
179
|
*/
|
|
154
180
|
pick(...names) {
|
|
@@ -162,6 +188,7 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
162
188
|
}
|
|
163
189
|
|
|
164
190
|
/**
|
|
191
|
+
* Returns a new schema that omits fields.
|
|
165
192
|
* @param {...string} [names] Names to exclude.
|
|
166
193
|
*/
|
|
167
194
|
omit(...names) {
|
package/dist/cjs/string.js
CHANGED
|
@@ -35,7 +35,7 @@ class StringSchema extends _TypeSchema.default {
|
|
|
35
35
|
allowEmpty
|
|
36
36
|
} = options;
|
|
37
37
|
if (val === '' && (required || allowEmpty === false)) {
|
|
38
|
-
throw new _errors.LocalizedError('
|
|
38
|
+
throw new _errors.LocalizedError('Value is required.');
|
|
39
39
|
}
|
|
40
40
|
return val;
|
|
41
41
|
});
|
package/package.json
CHANGED
package/src/Schema.js
CHANGED
package/src/object.js
CHANGED
|
@@ -133,6 +133,34 @@ class ObjectSchema extends TypeSchema {
|
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
|
+
* Gets the schema for the given field. Accepts either a string
|
|
137
|
+
* separated by "." or an array of path names.
|
|
138
|
+
* @param {string|Array<string>} [path] The path of the field.
|
|
139
|
+
*/
|
|
140
|
+
get(path) {
|
|
141
|
+
const { fields } = this.meta;
|
|
142
|
+
if (!fields) {
|
|
143
|
+
throw new Error('Cannot select field on an open object schema.');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
path = Array.isArray(path) ? path : path.split('.');
|
|
147
|
+
|
|
148
|
+
const [name, ...rest] = path;
|
|
149
|
+
const schema = fields[name];
|
|
150
|
+
|
|
151
|
+
if (!schema) {
|
|
152
|
+
throw new Error(`Cannot find field "${name}".`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (rest.length) {
|
|
156
|
+
return schema.get(rest);
|
|
157
|
+
} else {
|
|
158
|
+
return schema;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Returns a new schema that only validates the selected fields.
|
|
136
164
|
* @param {...string} [names] Names to include.
|
|
137
165
|
*/
|
|
138
166
|
pick(...names) {
|
|
@@ -147,6 +175,7 @@ class ObjectSchema extends TypeSchema {
|
|
|
147
175
|
}
|
|
148
176
|
|
|
149
177
|
/**
|
|
178
|
+
* Returns a new schema that omits fields.
|
|
150
179
|
* @param {...string} [names] Names to exclude.
|
|
151
180
|
*/
|
|
152
181
|
omit(...names) {
|
package/src/string.js
CHANGED
|
@@ -37,7 +37,7 @@ class StringSchema extends TypeSchema {
|
|
|
37
37
|
this.assert('empty', (val, options) => {
|
|
38
38
|
const { required, allowEmpty } = options;
|
|
39
39
|
if (val === '' && (required || allowEmpty === false)) {
|
|
40
|
-
throw new LocalizedError('
|
|
40
|
+
throw new LocalizedError('Value is required.');
|
|
41
41
|
}
|
|
42
42
|
return val;
|
|
43
43
|
});
|
package/types/Schema.d.ts
CHANGED
package/types/Schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAoaA,kDAEC;AAzZD;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,mBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,sBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,uBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,YAFa,IAAI,CAIhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAShB;IAED;;OAEG;IACH,+BAFa,IAAI,CAMhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED,iDAyCC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAMhB;IAED,2BAYC;IAED;;MAOC;IAED;;;;MASC;IAED;;MAOC;IAED,4BAMC;IAED,kBAEC;IAED,YAGC;IAID;;OAEG;IACH,kCAFa,IAAI,CAiDhB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED,gEAQC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAYC;IAED,qBAsCC;CACF"}
|
package/types/object.d.ts
CHANGED
|
@@ -20,10 +20,18 @@ declare class ObjectSchema extends TypeSchema {
|
|
|
20
20
|
*/
|
|
21
21
|
append(arg: SchemaMap | Schema): ObjectSchema;
|
|
22
22
|
/**
|
|
23
|
+
* Gets the schema for the given field. Accepts either a string
|
|
24
|
+
* separated by "." or an array of path names.
|
|
25
|
+
* @param {string|Array<string>} [path] The path of the field.
|
|
26
|
+
*/
|
|
27
|
+
get(path?: string | Array<string>): any;
|
|
28
|
+
/**
|
|
29
|
+
* Returns a new schema that only validates the selected fields.
|
|
23
30
|
* @param {...string} [names] Names to include.
|
|
24
31
|
*/
|
|
25
32
|
pick(...names?: string[]): ObjectSchema;
|
|
26
33
|
/**
|
|
34
|
+
* Returns a new schema that omits fields.
|
|
27
35
|
* @param {...string} [names] Names to exclude.
|
|
28
36
|
*/
|
|
29
37
|
omit(...names?: string[]): ObjectSchema;
|
package/types/object.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AAmOA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAMnB;wBApOY;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,EAAE;AAD3C;;GAEG;AAEH;IAME,cA0EC;IAED,iBAEC;IAED;;OAEG;IAEH,YAHW,SAAS,GAAC,MAAM,gBAkC1B;IAED;;;;OAIG;IACH,WAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAsB9B;IAED;;;OAGG;IACH,gBAFc,MAAM,EAAA,gBAWnB;IAED;;;OAGG;IACH,gBAFc,MAAM,EAAA,gBAWnB;CAcF;mBAxMgC,UAAU;uBAHpB,cAAc"}
|