Feature request: Custom functions

I’d like to request that Mockaroo allow users to define custom functions that can be used in the formula of any schema.

For example, I’d like to define a function that returns a random number between two random numbers, excluding a particular value. It might look something like this:

def random_except(first, last, except)
  ([*first..last] - [except]).sample
end

The above function could be useful when linking data between two columns, where the user wants to ensure that the value of one column is equal to the value of another column in any row except for the current row. I end up doing that kind of thing a lot. Of course, there are many other potential uses for custom functions.

Thanks for the consideration. I :heart: Mockaroo.

This is now implemented. See the new functions tab. It was easier for me to allow the user to declare lambdas rather than use the def keyword. So you would define something like:

random_except = lambda do |first, last, except|
  ([*first..last] - [except]).sample
end

Then in your schema, call it with:

random_except.call(first, last, except)

Or, use the lambda shorthand notation when calling:

random_except.(first, last, except)
1 Like

Hey, that’s very exciting! Thank you!

When custom functions are used in schemas that are shared with other people via projects, do you recommend manually sharing those custom functions with the other people so that the schemas work for them?

Mockaroo will load the functions from the user who created the schema, not the user generating the data. So if you’re generating data from a schema that’s been shared with you, the functions should still work.

1 Like

That’s great to hear!