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

Optional metadata type #82

Closed
alex35mil opened this issue Apr 30, 2020 · 5 comments · Fixed by #95
Closed

Optional metadata type #82

alex35mil opened this issue Apr 30, 2020 · 5 comments · Fixed by #95

Comments

@alex35mil
Copy link
Member

alex35mil commented Apr 30, 2020

Sometimes, converting input to output requires some additional metadata. E.g. there is a select in UI with a list of items, each item is of Foo.t type which is a record {id: int, label: string}. Select's value is stringified id. In the output, there should be Foo.t. To get the output value in validator, array(Foo.t) is required.

It's achievable in the current state of things by:

  • storing in input Foo.t instead of stringified id
  • storing in input all foos: array(Foo.t) so it's available in validator

But neither looks nice to me.

Proposal: introduce an optional metadata type. If defined, it would be passed as a second argument to validators.

module Foo = {
  type t = {
    id: int,
    label: string,
  };
};

module Form = [%form
  type input = {foo: string};
  type output = {foo: Foo.t};
  type metadata = {foos: array(Foo.t)};
  let validators = {
    foo: {
      strategy: OnFirstChange,
      validate: (input, metadata) =>
        switch (
          metadata.foos
          ->Array.getBy(foo =>
              input.foo
              ->Int.fromString
              ->Option.mapWithDefault(false, id => id == foo.id)
            )
        ) {
        | Some(foo) => Ok(foo)
        | None => Error("Invalid foo")
        },
    },
  }
];

[@react.component]
let make = (~foos: array(Foo.t)) => {
  let form = Form.useForm(
    ~initialInput={foo: ""},
    ~metadata={foos: foos},
    ~onSubmit={...}
  );
  ...
};
@johnhaley81
Copy link
Contributor

We have this use case quite often and so far we have been putting it into another field on the input which is definitely clumsy and doesn't feel correct. Adding a data or metadata sounds like a fantastic change to me.

@alex35mil
Copy link
Member Author

metadata sounds perfect 👍

@alex35mil alex35mil changed the title Optional data type Optional metadata type May 6, 2020
@danwetherald
Copy link

We would love to have this concept to separate "hacky" fake inputs to a designated location to store data later needed to run validations.

One thing to mention here. We would require an API that allows us to update these metadata fields as these can be dynamic or change at a later time.

@danwetherald
Copy link

I would love to help make this happen but the PPX stuff is a bit over my head, is there anything we can do to speed this feature up?

@alex35mil
Copy link
Member Author

@dan003400 I will look into this in upcoming weeks

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

Successfully merging a pull request may close this issue.

3 participants