Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support server-only validations #33

Merged
merged 1 commit into from
Jan 16, 2023
Merged

Conversation

brophdawg11
Copy link
Owner

Allow a server-only set of custom validations to be passed into validateServerFormData:

import type { ServerOnlyCustomValidations } from 'remix-validity-state'
import { validateServerFormData } from 'remix-validity-state'

let formDefinition: FormSchema = {
  inputs: {
    emailAddress: {
      validationAttrs: {
        type: "email",
        required: true,
      },
      customValidations: {
        // always "valid" in shared validations
        uniqueEmail: () => true,
      },
      errorMessages: {
        uniqueEmail: () => 'Email address already in use!'',
      },
    },
  }
};

const serverCustomValidations: ServerOnlyCustomValidations<FormSchema> = {
  emailAddress: {
    async uniqueEmail(value) {
      let isUnique = await checkEmail(value);
      return isUnique;
    },
  },
};

export async function action({ request }: ActionArgs) {
  const formData = await request.formData();
  const serverFormInfo = await validateServerFormData(
    formData,
    formDefinitions,
    serverCustomValidations
  );
  // serverFormInfo.valid here will be reflective of the custom server-only
  // validation and will leverage your shared `errorMessages`
  if (!serverFormInfo.valid) {
    return json({ serverFormInfo });
  }
  // ...
}

Closes #25

@brophdawg11 brophdawg11 merged commit 2f0bfd2 into main Jan 16, 2023
@brophdawg11 brophdawg11 deleted the server-only-validations branch January 16, 2023 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Server-only custom validation
1 participant