shakacode / re-formality

Form validation tool for reason-react
https://re-formality.now.sh
MIT License
244 stars 36 forks source link

Record aliases #84

Open alex35mil opened 4 years ago

alex35mil commented 4 years ago

The requirement of having record definitions inlined might be inconvenient when some type already exists and used across the app.

It's not possible to avoid inlining since PPX would need type information to generate the proper code, but it can be handled somewhat better:

// input alias
module Form = [%form
  [@alias "Article.t"]
  type input = {
    title: string,
    body: string,
  }
];

// collection alias
module Form = [%form
  type input = {
    title: string,
    authors: [@field.collection] array(author),
  }
  [@alias "Author.t"]
  and author = {name: string}
];

An inlined record can be aliased to an existing type in generated code. It would still be required to duplicate record definition so PPX can parse the internal types but the generated code and the rest of the app would be dealing with the provided type instead of the one defined in the PPX'ed module.

So the generated code would be:

// input alias
module Form = {
  type input = Article.t;
};

// collection alias
module Form = {
  type input = {
    title: string,
    authors: array(author),
  }
  and author = Author.t;
};

It should be totally type-safe, but errors might be confusing when an inlined definition and actual type don't match (no way it can be ensured by PPX, unfortunatelly).

Any other drawbacks?