CAKE20

AI Web PaaS

Input validation

Declare input conditions and editor test data together in the API config.

export config

  • The config is exported with export const.
  • In config.filter, create a Zod shape to check each input value.
  • config.sample is written with values ​​that pass the filter and is used only for test execution in the editor.
  • GET, DELETE, and HEAD check the query string, so numbers are converted to z.coerce.number().
  • POST, PUT, and PATCH inspect the JSON body and pass the converted value to the handler.
  • Dynamic path values ​​such as [id] and [...path] are received as context.params, not as a filter.
export const config = {
  filter: {
    username: z.string().min(3),
    age: z.number().positive(),
    email: z.string().email()
  },
  sample: {
    username: "candy",
    age: 17,
    email: "[email protected]"
  }
};

export default async (data: Input<typeof config.filter>) => ({
  ...data,
  count: await db.user.count()
});

If you have a config.filter, you must have a valid config.sample to run editor tests.