Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 1.17 KB

allowed-values.md

File metadata and controls

41 lines (31 loc) · 1.17 KB

Allowed Values

This is used to specify the values that should be accepted for a property. It can be used without a validator but if a validator is provided, the values would first be checked against this list before being passed to the validator

Example:

import { Schema } from "ivo";

const userSchema = new Schema({
  role: { default: "user", allow: ["admin", "moderator", "user"] },
  name: { required: true, validator: validateName },
});

NotAllowedError:

If you need to specify custom errors to handle invalid values, you could do it like in the example below

import { Schema } from "ivo";

const userSchema = new Schema({
  role: {
    default: "user",
    allow: {
      values: ["admin", "moderator", "user"],
      error: "Invalid role provided",
    },
  },
  name: { required: true, validator: validateName },
});

// the error above can match the following type
type NotAllowedError =
  | string
  | InputFieldError
  | ((valueProvided: any, allowedValues: any[]) => string | InputFieldError);

N.B: if the value of the NotAllowedError is to be generated by a function and this function happens to throw an error, the default error message will be used