Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

1.7 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { data } from "react-router";
import type { FORM_DEFAULTS_FIELD } from "./internal/constants";
import { formDefaultValuesKey } from "./internal/constants";
import type { ValidatorError } from "./validation/types";

/**
 * Takes the errors from a `Validator` and returns a `Response`.
 * When you return this from your action, `ValidatedForm` on the frontend will automatically
 * display the errors on the correct fields on the correct form.
 *
 * You can also provide a second argument to `validationError`
 * to specify how to repopulate the form when JS is disabled.
 *
 * @example
 * ```ts
 * const result = validator.validate(await request.formData());
 * if (result.error) return validationError(result.error, result.submittedData);
 * ```
 */
export function validationError(
  error: ValidatorError,
  repopulateFields?: unknown,
  init?: ResponseInit
) {
  return data(
    {
      fieldErrors: error.fieldErrors,
      subaction: error.subaction,
      repopulateFields,
      formId: error.formId
    },
    { status: 422, ...init }
  );
}

export type FormDefaults = {
  [formDefaultsKey: `${typeof FORM_DEFAULTS_FIELD}_${string}`]: any;
};

// FIXME: Remove after https://github.com/egoist/tsup/issues/813 is fixed
export type internal_FORM_DEFAULTS_FIELD = typeof FORM_DEFAULTS_FIELD;

export const setFormDefaults = <DataType = any>(
  formId: string,
  defaultValues: Partial<DataType>
): FormDefaults => ({
  [formDefaultValuesKey(formId)]: defaultValues
});