diff --git a/lib/rules/object.js b/lib/rules/object.js index 0b87b0c..2a79db6 100644 --- a/lib/rules/object.js +++ b/lib/rules/object.js @@ -50,20 +50,20 @@ module.exports = function ({ schema, messages }, path, context) { for (let i = 0; i < keys.length; i++) { const property = keys[i]; - + const rule = this.getRuleFromSchema(subSchema[property]); + const name = escapeEvalString(property); const safeSubName = identifierRegex.test(name) ? `.${name}` : `['${name}']`; const safePropName = `parentObj${safeSubName}`; const newPath = (path ? path + "." : "") + property; - const labelName = subSchema[property].label; + const labelName = rule.schema.label; const label = labelName ? `'${escapeEvalString(labelName)}'` : undefined; sourceCode.push(`\n// Field: ${escapeEvalString(newPath)}`); sourceCode.push(`field = parentField ? parentField + "${safeSubName}" : "${name}";`); sourceCode.push(`value = ${safePropName};`); sourceCode.push(`label = ${label}`); - const rule = this.getRuleFromSchema(subSchema[property]); const innerSource = ` ${safePropName} = ${context.async ? "await " : ""}context.fn[%%INDEX%%](value, field, parentObj, errors, context, label); `; diff --git a/test/rules/object.spec.js b/test/rules/object.spec.js index a6fde96..074ae2c 100644 --- a/test/rules/object.spec.js +++ b/test/rules/object.spec.js @@ -133,6 +133,12 @@ describe("Test rule: object", () => { 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}]); }); + it("shorthand label",()=>{ + const check = v.compile({ $$root: true, type: "object", props: { shorthand_label: "string|label:My Label" } }); + const res = check({ shorthand_label: 123 }); + expect(res[0].label).toEqual("My Label"); + + }); describe("Test sanitization", () => { diff --git a/test/validator.spec.js b/test/validator.spec.js index c6ed2b1..d6aad2e 100644 --- a/test/validator.spec.js +++ b/test/validator.spec.js @@ -251,6 +251,11 @@ describe("Test getRuleFromSchema method", () => { expect(res2.schema).toEqual({ type: "array", optional: true, items: "string", min: 1 }); }); + it("should convert label", () => { + const res = v.getRuleFromSchema("string|label:My Label"); + expect(res.schema).toEqual({ type: "string", label: "My Label" }); + }); + }); describe("Test objects shorthand rule ($$type)", () => {