0

I'm working on a validation library containing a number of validators. Each validator is curried with a message as the first argument followed by any configuration values.

A user of the library can pass in a map of message functions which will be applied to the validators by the library.

const configuredValidators = Lib.configure(messages);

If using these validators without this configuration step, they can just be imported into whichever file needs them:

import { exampleValidator } from 'lib'

However by preconfiguring the validators, a user of the library now needs to pass these validators around to whichever function needs them. If these functions are a long way from the point of configuration, every function in between now needs to add an argument to pass through the configured validator(s).

When adopting as OO approach there are a number of solutions to this problem, but what is the functional solution? How can I make these partially applied validators available throughout the application without a user needing to muddy the signatures of many other functions?

I understand that reaching out from within a function to an external variable raises issues of purity, but I suppose I'm looking for the convenience of an import, but with functions that have had values partially applied at runtime.

Note: For the purposes of this question, assume the users of the library will be adopting a functional approach.

Undistraction
  • 42,754
  • 56
  • 195
  • 331
  • *"...meaning every function in between now needs to add an argument to pass through the configured validator(s)..."* The functions you're talking about there are the library user's functions, right? Not the library's? – T.J. Crowder Jan 31 '18 at 09:22
  • I may not fully grasp your problem, but as far as I can understand you need a http://requirejs.org/ ? – Edwin Jan 31 '18 at 09:23
  • 1
    @T.J.Crowder Yes. I will clarify. – Undistraction Jan 31 '18 at 09:24

1 Answers1

1

I don't think this is your problem. :-) Your users can solve this with a module that exports the configured validators:

export const configuredValidators = Lib.configure(messages); // Or similar

Then they just import from that module rather than importing the validators from your library directly.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • ...assuming I'm correct [in this comment](https://stackoverflow.com/questions/48538477/exposing-partially-applied-functions-to-application/48538591#comment84072092_48538477). – T.J. Crowder Jan 31 '18 at 09:24
  • 1
    My brain had been taking me to all kinds of crazy places, but not the most obvious one. Thanks. – Undistraction Jan 31 '18 at 09:55