Skip to content

Commit e9af726

Browse files
author
Fernando
committed
shorthand label in object
1 parent 105f908 commit e9af726

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Diff for: lib/rules/object.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ module.exports = function ({ schema, messages }, path, context) {
5050

5151
for (let i = 0; i < keys.length; i++) {
5252
const property = keys[i];
53-
53+
const rule = this.getRuleFromSchema(subSchema[property]);
54+
5455
const name = escapeEvalString(property);
5556
const safeSubName = identifierRegex.test(name) ? `.${name}` : `['${name}']`;
5657
const safePropName = `parentObj${safeSubName}`;
5758
const newPath = (path ? path + "." : "") + property;
5859

59-
const labelName = subSchema[property].label;
60+
const labelName = rule.schema.label;
6061
const label = labelName ? `'${escapeEvalString(labelName)}'` : undefined;
6162

6263
sourceCode.push(`\n// Field: ${escapeEvalString(newPath)}`);
6364
sourceCode.push(`field = parentField ? parentField + "${safeSubName}" : "${name}";`);
6465
sourceCode.push(`value = ${safePropName};`);
6566
sourceCode.push(`label = ${label}`);
66-
const rule = this.getRuleFromSchema(subSchema[property]);
6767
const innerSource = `
6868
${safePropName} = ${context.async ? "await " : ""}context.fn[%%INDEX%%](value, field, parentObj, errors, context, label);
6969
`;

Diff for: test/rules/object.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ describe("Test rule: object", () => {
133133
expect(v.validate({foo:"bar"}, {$$root: true, type: "object", maxProps: 0, strict: "remove"})).toEqual([{actual: 1, field: undefined, message: "The object '' must contain 0 properties at most.", type: "objectMaxProps", expected: 0}]);
134134
});
135135

136+
it("shorthand label",()=>{
137+
const check = v.compile({ $$root: true, type: "object", props: { shorthand_label: "string|label:My Label" } });
138+
const res = check({ shorthand_label: 123 });
139+
expect(res[0].label).toEqual("My Label");
140+
141+
});
136142

137143
describe("Test sanitization", () => {
138144

Diff for: test/validator.spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ describe("Test getRuleFromSchema method", () => {
251251
expect(res2.schema).toEqual({ type: "array", optional: true, items: "string", min: 1 });
252252
});
253253

254+
it("should convert label", () => {
255+
const res = v.getRuleFromSchema("string|label:My Label");
256+
expect(res.schema).toEqual({ type: "string", label: "My Label" });
257+
});
258+
254259
});
255260

256261
describe("Test objects shorthand rule ($$type)", () => {

0 commit comments

Comments
 (0)