Validate multiple fields using validateUniqueness

Can someone show me an example of multiple fields validation using validateUniqueness?
I have read the documentation from here Services | RedwoodJS Docs

Also i get this when i don’t use the optional custom message param.

with optional message

without it

It’s been a while since I implemented this, but you should be able to include all the fields you want to check as that second argument, the object:

export const createUser ({ input }) => {
  return validateUniqueness('user', { firstName: 'Rob', lastName: 'Cameron' }, (db) => {
    return db.user.create({ input })
  })
})

firstName and lastName would both need to be unique in order for the function to be called to create the user. You can see the logic in the source here: redwood/validations.ts at main · redwoodjs/redwood · GitHub

As for the second thing, looks like a Typescript issue. Could you open up an issue on the repo and we’ll have a look? Thanks!

The more I’m thinking about this, I don’t know that we ever tested uniqueness with multiple fields! It’s possible it may not work. :grimacing: If that’s the case let me know and we’ll get a PR in there to fix it. v1.0 is being released eminently!

This works but not in the way i was expecting. If i try to validate 2 fields and the validation is ok for the first field but not for the second one the error is generic “Something went wrong”. if the validation fails for the first field then a get “Field must be unique”. Is this normal behaviour?

I think I had grander ambitions to be able to pass more complex uniqueness constraints, with AND and NOT and at the end of the day I just went with the standard object you’d pass to a Prisma where.

Want to try something? Go into node_modules/@redwoodjs/api/dist/validations/validations.js and look for this section:

  const where = {
    AND: [rest],
    NOT: []
  };

Change it to:

const where = rest;

And then reload the dev server and see if that has be behavior you’d expect!

Note that’ll totally break if you’re also using $self or $scope.

Now that i think about this, what i need to do is show an error message when the response from a create mutation is an error. I need to know what is the error and inform the user. But because of the generic error i ended up on validators. Validators sound and look great but i need to ask. Is there a way i can get what i need without using the validators?. Like in a typical react app making a call to the be and handling the response.

Yep, there are some errors you can throw where you’ll see the proper message in the front end, rather than being scrubbed into a generic “Something went wrong” which is for security concerns. See here: GraphQL | RedwoodJS Docs for throwing errors and then Services | RedwoodJS Docs for getting the error from useMutation and displaying to the user.

1 Like

Damn. I keep missing super important info from the docs. :). This is exactly what i need. Thanks. Will V1 ship by end of month?

2 Likes

Stay tuned!!

:wink: