Custom Validator

Sometimes, certain fields require custom validations that are too specialized to generalize in a standard validator. For this purpose, a custom function may be provided in order to manually process the field’s value.

Usage

const schema = new PerfectSchema({
  password: {
    type: String,
    custom(value, self, context) {
      const field = self.getField('confirmPassword');

      return !field.exists || (field.value !== value) ? 'mismatch' : undefined;
    }
  },
  confirmPassword: {
    type: String
  }
});

Custom validators

The custom validator function receives three (3) arguments and is expected to return undefined or a String, whether the field is valid or not. If the field value is not valid, the returned value is the error message to set in the validator.

The arguments represents the value, the field’s self information, and the actual validation context.

The field’s self information is an object providing extra functionality when validating the value.

Extended schema field options