athena-framework / athena

An ecosystem of reusable, independent components
https://athenaframework.org
MIT License
211 stars 17 forks source link

Recursive Validator Not Working #327

Closed RainbowZephyr closed 11 months ago

RainbowZephyr commented 11 months ago

Hello, Firstly I would like thank you for such a wonderful repo, it has been used extensively at work. I am running into a bit of trouble however, from what I understand is that the recursive validator applies on nested objects. I have the following two objects:

module Models
  class Record
    include JSON::Serializable
    include AVD::Validatable

    getter :run_name, :measurement_points, :balancing_planes, :plane_added_weight

    @run_name : String

    @[Assert::NotBlank(message: "Measurement points cannot be empty")]
    @measurement_points : Array(Models::MeasurementPoint)

  end
end
module Models
  class MeasurementPoint
    include JSON::Serializable
    include AVD::Validatable

    getter :axes, :name

    @name : String

    @[Assert::NotBlank(message: "Axes cannot be empty")]
    @axes : Array(String)
  end
end

If I turn the JSON below into a crystal object and validate it using recursive validator it does not spot the missing axes array

{         
  "name": "Measurement Point 1",
  "axes": [ ]
}

I am using the validator as follow:

validator = Athena::Validator::Validator::RecursiveValidator.new()
errors = validator.validate(job)
errors.inspect() # Returns an empty result

Should I validate each nested object or is this behaviour wrong?

Thanks in advance

Blacksmoke16 commented 11 months ago

I currently don't have access to a computer to test, but I'm pretty sure you want to use https://athenaframework.org/Validator/Constraints/Valid/ on the array of objects, then use https://athenaframework.org/Validator/Constraints/All/ with NotBlank for the array of strings.

See if that does it, and if not can go from there.

RainbowZephyr commented 11 months ago

That does the trick, thank you so much :smile: