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

Explore form validation options #1

Open
dziraf opened this issue Oct 18, 2021 · 1 comment
Open

Explore form validation options #1

dziraf opened this issue Oct 18, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@dziraf
Copy link
Collaborator

dziraf commented Oct 18, 2021

Ideally, we would like to be able to validate forms like we do in other adapters:

  • Sequelize allows to define validators on field level in model specification
  • TypeORM allows to validate fields using class-validator

We need to find out if we are able to define such validations in Prisma as well and if yes, check whether error data contains enough information to display such error to the end user.

Currently, the adapter attempts to save whatever is sent from the form and the query can fail against SQL constraints, but the error displayed to the user is generic and says only that there were errors when saving the record.

Before this is fully researched and implemented, form validations have to be custom-made, example:

const validateForm = (request, context) => {
  if (request.method !== 'post') return request;

  const { translateMessage: tm, resource } = context;
  
  if (!request.payload?.someRequiredInput?.trim?.().length) }
    throw new ValidationError({
      someRequiredInput: { message: tm('someRequiredInputIsRequired', resource.id()) },
    });
  }

  return request;
};

With this, there will be a proper error message displayed under someRequiredInput field in the form if it isn't defined.
This is a before hook which should be used in new and edit actions.

@dziraf dziraf added the enhancement New feature or request label Oct 18, 2021
@nikolasburk
Copy link

Prisma doesn't provide a similar validation mechanism as Sequelize and TypeORM at the moment, but we have several issues that are related to this. It would be great if you could add a comment to the issues that seem most relevant to your use case and I'll try to draw attention to them internally 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants