Skip to content

Validate And Transform

Create a new file in stack9/workflow-functions/custom called validate-and-transform-customer.js

@prop context - object - context

module.exports = {
  exec: async (context) => {
    const { entity } = context;
    const { first_name, last_name } = entity;

    if (!first_name) {
      return {
        entity,
        valid: false,
        errorMessage: 'First name is required',
      };
    }

    return {
      valid: true,
      entity: {
        ...entity,
        full_name: `${first_name} ${last_name}`,
      },
    };
  },
  timeout: 5000,
};

or in TypeScript

import { CustomFunction } from "@april9/stack9-sdk"

// stack9/server/workflow-functions/custom/validate-and-transform-customer.ts
class ExampleValidateAndTransform extends CustomFunction {
  constructor(private context: CustomFunctionContext<User>) {
    super();
  }

  async exec(): Promise<CustomFunctionResponse> {
   const { first_name, last_name } = this.context.entity;

    if (!first_name) {
      return {
        entity,
        valid: false,
        errorMessage: 'First name is required',
      };
    }

    return {
      valid: true,
      entity: {
        ...entity,
        full_name: `${first_name} ${last_name}`,
      },
    };
}

export default withCustomFunction(ExampleValidateAndTransform, 5000);

Registration

Register in the entity definition file your want the hook to be executed. eg: stack9/entities/custom/customer.json

  "hooks": [
    {
      "type": "function",
      "hookType": "ValidateAndTransformInput",
      "functionName": "custom/validate-and-transform-customer"
    },
  ]