Skip to content

Commit

Permalink
Service Validations (#3532)
Browse files Browse the repository at this point in the history
* Adds presence, absence validators, adds errors and tests

* Refactor validator definitions, adds acceptance, exclusion

* Return real validation error message via GraphQL

* Adds inclusion validation

* Adds `in` option for acceptance

* Adds format and length validation

* Adds numericality validation

* Comments and usage examples for each validator

* Adds allowNull, allowUndefined, allowEmptyString options to presence validator

* Reorganize tests

* Adds validateUniqueness()

* Tests for validateUniqueness()

* Rewrite mock to more generic style

* No need to import PrismaClient to mock in tests

* TYPESCRIPT

* Type typo

* Make all validators accept unknown types and worry about them inside each one

* Comment typos

* Adds validateWith() for custom validation function

* Adds `model` as first argument of validateUniqueness, pass new instance of PrismaClient to callback function

* validateWith takes no arguments but function

* Humanize field names in error messages

* Reverse order of field name and value

* Don't modify field name case, just use what they gave us

* Adds email, positive and negative validators

* Tweak email validation, add tests

* Removes humanize-string

* Add $self and $scope to validateUniqueness()

* Refactors errors, allows for substitutions in error messages

* Use mustache syntax for error message interpolation

* Make options object just options, move booleans to argument list

* Update syntax to make label optional

* Remove unused argument

* Overload validateUniqueness() so that we can pass optional third parameter with message

* Rename "directive" -> "recipe"

* Adds RedwoodError, extend ServiceValidationError, allow RedwoodErrors through GraphQL

* Fix check for allow list (must be instanceof)

* Switch string interpolation to ${} to match dbAuth

Co-authored-by: Rob Cameron <rob.cameron@fastmail.com>
Co-authored-by: David Price <thedavid@thedavidprice.com>
  • Loading branch information
3 people committed Oct 22, 2021
1 parent 20c20d8 commit d62b806
Show file tree
Hide file tree
Showing 6 changed files with 1,844 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/api/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class RedwoodError extends Error {
constructor(message: string) {
super(message)
this.name = 'RedwoodError'
}
}
3 changes: 3 additions & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export * from './auth'
export * from './errors'
export * from './functions/dbAuth/DbAuthHandler'
export { dbAuthSession } from './functions/dbAuth/shared'
export * from './validations/validations'
export * from './validations/errors'

// @NOTE: use require, to avoid messing around with tsconfig and nested output dirs
const packageJson = require('../package.json')
Expand Down

0 comments on commit d62b806

Please sign in to comment.