Function validatedFunction

Creates a function that sanitizes and validates its arguments before running a custom function.

export const validatedFunction = function ({ validate, run }) {
return function (args) {
try {
let cleaned = validate.clean(args);
validate.validate(cleaned);
return run(args);
} catch (error) {
throw new Meteor.Error("400", error.message);
}
};
};
export const getData = validatedFunction({
validate: new SimpleSchema({
_id: { type: String, regEx: SimpleSchema.RegEx.Id },
}),
async run({ _id }) {
return Collection.findOneAsync({ _id });
},
});

const data = await getData({ _id });
  • Parameters

    • options: { run: Function; validate: { clean: Function; validate: Function } }

      The options for the validated function.

      • run: Function

        The function to be executed after validation.

      • validate: { clean: Function; validate: Function }

        An object containing validation methods.

        • clean: Function

          A function that sanitizes the arguments.

        • validate: Function

          A function that validates the sanitized arguments.

    Returns Function

    A function that takes args as its parameter, sanitizes, validates, and runs run.