DEV Community

Cover image for Custom Functions FTW
David Kershaw
David Kershaw

Posted on

Custom Functions FTW

CsvPath Validation Language is functions-based. It applies a very simple syntax and a large number of functions to validate CSV, JSONL, and Excel files in ways that were never-before possible.

And then comes the moment when you want to do some crazy thing that the CsvPath Framework contributors didn't think of. What to do? You create a custom function that does exactly that.

Custom functions even work in FlightPath Data and FlightPath Server. I call that out because FlightPath Data is a multi-project environment. And FlightPath Server is both multi-project and also multi-user. That means functions must be scoped and sandboxed. They are and they work great!

Let's create a trivial example to show the setup of a custom function. I'll leave the actual functionality as an exercise for the reader, since that part is demonstrated copiously in the CsvPath Framework Github repo.

The goal

Our goal is to create a function called sure(). It will functionally be the same as yes(). I.e. it returns True.

Our csvpath statement will be:

    $[*][ sure() ]
Enter fullscreen mode Exit fullscreen mode

If you try that in FlightPath Data you will get this error message:

The config.ini

The first step is to point to a functions import file. By default, import files are called functions.imports and live in the project's config directory. In FlightPath Data, click Open config at the bottom left of the app to open the config panel. Then click functions in the vertical tabs to open the functions config form. The form has just one field for the path to the imports file. The path can be relative or absolute.

Once the path is ready click Save and reload and then Close config.

Next we need to edit the imports file to include our sure() function. Right click on the blank space in the project files tree and select Open project directory.

Open the config directory and you should see three files, config.ini, env.json, and functions.imports. If you don't see all three don't worry about it; some files are generated just in time. If there is no functions.imports create one. Then open it.

In functions.imports we're going to add one line that imports our sure() function.

This is basically the same form as Python's. It says find the example/one/yes.py file and import the Yes class, using the name sure as the function name of the imported class. Simple!

Finally we just need to put the custom function in the right place. The right place, starting from the project's home directory, is config/example/one/yes.py.

I copied the regular Yes class from its file in the repo to make my example yes.py. Again, we're just setting up a custom function, not showing how to write an awesome function.

This is where the yes.py file lives.

And... we're basically ready to go. However, if you ran a csvpath already, restart FlightPath to clear the function classes that were already loaded. You can do this programmatically in CsvPath Framework, but there's no button in FlightPath's config yet.

That done, back in FlightPath Data right click in your project files tree and create a new file. Call it test.csvpaths, or whatever you like.

In it, paste our test csvpath:

    $[*][ sure() ]
Enter fullscreen mode Exit fullscreen mode

Make it look like:

Now, with your cursor inside the csvpath statement, click cmd-r (or ctrl-r on Windows).

You should see the message Test run complete. Matched 2 lines. in the status bar and the printouts tab should be blank. (If your status bar says Test run complete but has a different number of lines don't worry about it; your test data and mine just aren't the same).

For a bit of comfort that all is working as expected, add a print line like this and you should get the same printouts shown.

And that's all there is to it. Now you'll never be stumped by the absence of Cool Function X, because you can write it yourself.

To be fair, though, while a simple function can be trivial, as we just saw, more complex functions can be... well, more complex. If you need help creating your awesome function don't hesitate to reach out. We'll be glad to help you get started.

Top comments (0)