Skip to content

The result of the custom validator lost a property field. #109

@kelp404

Description

@kelp404

Hello,

Thank you for this open source project. I use fastest-validator on my websites.

I found an issue about the custom validator.
The result of the custom validator lost a property field.

<script src="https://unpkg.com/fastest-validator"></script>
let v = new FastestValidator({
    messages: {
        // Register our new error message text
        weightMin: "The weight must be greater than {expected}! Actual: {actual}"
    }
});

const schema = {
    name: { type: "string", min: 3, max: 255 },
    weight: {
        type: "custom",
        minWeight: 10,
        check(value, schema) {
            return (value < schema.minWeight)
                ? [{ type: "weightMin", expected: schema.minWeight, actual: value }]
                : true;
        }
    }
};

console.log(v.validate({ name: "John", weight: 8 }, schema));
[
  {
    "message": "The weight must be greater than 10! Actual: 8",
    "type": "weightMin",
    "expected": 10,
    "actual": 8
  }
]

It should be:

[
  {
    "message": "The weight must be greater than 10! Actual: 8",
    "type": "weightMin",
    "expected": 10,
    "field": "weight",
    "actual": 8
  }
]

截圖 2020-02-01 上午11 28 35

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions