Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword error replacement in nested objects it's not working #147

Open
vvalencia-cl opened this issue Dec 16, 2022 · 0 comments
Open

Keyword error replacement in nested objects it's not working #147

vvalencia-cl opened this issue Dec 16, 2022 · 0 comments

Comments

@vvalencia-cl
Copy link

ajv-errors:3.0.0

I've created this test (using the code from object.spec.ts) and using a simple schema with some nested property.
The idea, based on the documentation, is that the error message for the "required" keyword should be replaced, but this it's not happening.

The error object is generated like this:

{
  "instancePath": "/sections/0",
  "schemaPath": "#/properties/sections/items/required",
  "keyword": "required",
  "params": {
    "missingProperty": "identityFieldValue"
  },
  "message": "must have required property 'identityFieldValue'"
}

This is the test to replicate the problem:

describe("errorMessage value is an object custom", () => {
  const ajv = _ajv();

  it("should replace keyword errors with custom error messages", () => {
    const schema: SchemaObject = {
      type: "object",
      required: ["foo", "sections"],
      properties: {
        foo: {type: "integer"},
        sections: {
          minItems: 1,
          type: "array",
          items: {
            type: "object",
            properties: {
              identityFieldValue: { type: "string", minLength: 1 },
            },
            required: ["identityFieldValue"],
          }
        }
      },
      additionalProperties: false,
      errorMessage: {
        type: "should be an object",
        required: "should have property foo",
        additionalProperties: "should not have properties other than foo",
      },
    };

    const validate = ajv.compile(schema);
    testInvalid({foo: 1, sections: [{

      }]}, [["required"]]);

    function testInvalid(data: any, expectedErrors: (string | string[])[]): void {
      assert.strictEqual(validate(data), false)
      assert.strictEqual(validate.errors?.length, expectedErrors.length)
      validate.errors.forEach((err, i) => {
        const expectedErr = expectedErrors[i]
        if (Array.isArray(expectedErr)) {
          // errorMessage error
          assert.strictEqual(err.keyword, "errorMessage")
          assert.strictEqual(err.message, schema.errorMessage[err.params.errors[0].keyword])
          assert.strictEqual(err.instancePath, "")
          assert.strictEqual(err.schemaPath, "#/errorMessage")
          const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
          assert.deepStrictEqual(
            Array.from(replacedKeywords.sort()),
            Array.from(expectedErr.sort())
          )
        } else {
          // original error
          assert.strictEqual(err.keyword, expectedErr)
        }
      })
    }
  })
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant