Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Unexpected validation pass about nested fields #9

Open
vmarcosp opened this issue Dec 14, 2022 · 1 comment
Open

Unexpected validation pass about nested fields #9

vmarcosp opened this issue Dec 14, 2022 · 1 comment

Comments

@vmarcosp
Copy link
Member

@mununk reported on ReForm (rescriptbr/reform#244):

I encounter a weird unexpected validation pass about the nested field array. Here is my example:

module ProfileFormFields = %lenses(
  type t = {
    nickname: string,
    age: int,
  }
)

module FormFields = %lenses(
  type state = {
    name: string,
    email: string,
    profiles: array<ProfileFormFields.t>,
  }
)

module Form = ReForm.Make(FormFields)
...
        schema([
          custom(({profiles}) => {
            let errors: array<ReSchema.childFieldError> = // <-- 1
              profiles
              ->Array.mapWithIndex((index, profile) =>
                if profile.nickname == "" || profile.age <= 0 {
                  let error: ReSchema.childFieldError = {
                    error: "Invalid profile",
                    index,
                    name: "???",
                  }
                  Some(error)
                } else {
                  None
                }
              )
              ->Array.keepMap(x => x)

            switch errors {
            | [] => Valid
            | _ => NestedErrors(errors) // <- 2. return NestedErrors!
            }
          }, Profiles)
        ])
...

Field(Profiles)->form.getNestedFieldError(index)->Js.log // errors are all None??

The length of errors in comment:1 is not 0, so it means that this predicate function returns NestedErrors(errors). But none of the indexes in the nested field has an error. So, the submission proceeded even though the nested fields are not satisfied in the custom schema.

I guess that validate function (https://github.com/rescriptbr/reschema/blob/master/src/ReSchema.res#L287:L292) in ReSchema should check the NestedErrors there. Or anything I missed for the nested fields validation?

@vmarcosp
Copy link
Member Author

vmarcosp commented Dec 14, 2022

cc @fakenickels @mununki

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant