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

Support arbitrary conversions with a Type#convert method #241

Open
2 of 10 tasks
skeggse opened this issue Nov 19, 2019 · 1 comment
Open
2 of 10 tasks

Support arbitrary conversions with a Type#convert method #241

skeggse opened this issue Nov 19, 2019 · 1 comment

Comments

@skeggse
Copy link
Contributor

skeggse commented Nov 19, 2019

This is a:

  • Bug Report
  • Feature Request
  • Question
  • Other

Which concerns:

  • flow-runtime
  • babel-plugin-flow-runtime
  • flow-runtime-validators
  • flow-runtime-mobx
  • flow-config-parser
  • The documentation website

No affordance for convert behavior:

type Request = {
  // Feature: convert strings to numbers.
  quantity: number,
  // Feature: convert datetime strings (v.dateTime) or numeric timestamps to Dates.
  sessionTime: Date,
};

const RequestType = (reify: Type<Request>);

// One possibility for a conversion decoration API. flow-runtime ingests the explicit
// type on the input parameter to determine how to widen the type.
RequestType.getProperty('quantity').addConversion((raw: string) => {
  const value = parseInt(raw, 10);
  if (isNaN(value)) {
    throw new TypeError('expected a base-10 integer');
  }
  return value;
});

// The convert method is a modified assert that expands the supported types to include
// types that can be casted.
const value = RequestType.convert({
  // value.quantity could become 12, or this behavior could be customized like in
  // the constraint model by mutating a PermissiveNumber type.
  quantity: '12.0',
  // value.sessionTime becomes Date(2019-11-19T01:53:00.000Z)
  sessionTime: '2019-11-19T01:53:00Z',
});

A v1 here might be to just decorate all Types with convert methods and conversion arrays that we can extend with addConversion. This would mostly be trying to solve the same use-cases as joi's convert option.

A v2 might be to support a conversion where the types in and the types out are entirely disjoint - e.g. convert(input: string | number): Date but via a declarative syntax

A v3 might be to include default conversion functionality like string -> number, but that'd require a bit more exploration.

I'm open to working on this; just curious if y'all have thoughts on the API/concerns about the implementation.

@skeggse
Copy link
Contributor Author

skeggse commented Nov 20, 2019

I'm tempted to say that some of this functionality shouldn't be in the flow-runtime core; I think this pattern might be possible with type introspection/or a visitor pattern #5.

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

No branches or pull requests

1 participant