Skip to content

Instantly share code, notes, and snippets.

@lokshunhung
Created July 5, 2021 02:42
Show Gist options
  • Select an option

  • Save lokshunhung/790bfe430f6488106d89bbeb4019708f to your computer and use it in GitHub Desktop.

Select an option

Save lokshunhung/790bfe430f6488106d89bbeb4019708f to your computer and use it in GitHub Desktop.
class-validator options
const validatorOptions: import("class-validator").ValidatorOptions = {
//* do not include `target` and `value` when reporting error
validationError: { target: false, value: false },
//* unknown objects (aka not instance of decorated class) fails validation instead of passing
//* https://github.com/typestack/class-validator/blob/c2f999f9cc7ee2f40749780a89ae9f1f59f0b3e1/src/validation/ValidationExecutor.ts#L65
forbidUnknownValues: true,
//* strips non-decorated properties (aka "whitelisted") on the object to-be-validated
//* use `@Allow()` to "whitelist" a property if no decorators are appropriate
whitelist: true,
//* report as an error when object to-be-validated contains "non-whitelisted" properties
forbidNonWhitelisted: true,
//* validate all properties, don't stop at first error
// stopAtFirstError: false,
//* use default error messages
// dismissDefaultMessages: false,
//* do not validate property if property is null or undefined (default)
//* https://github.com/typestack/class-validator/blob/c2f999f9cc7ee2f40749780a89ae9f1f59f0b3e1/src/validation/ValidationExecutor.ts#L202
// skipUndefinedProperties: false,
// skipNullProperties: false,
//* this flag is misleading, it checks for `val === null` or `val === undefined`, rather than `"propertyName" in obj`
//* to use the desired behaviour, use `@ValidateIf(o => "propertyName" in o)`
//* https://github.com/typestack/class-validator/issues/308#issuecomment-509000183
// skipMissingProperties: false,
//* groups are confusing, don't use them
// always: false,
// groups: undefined,
// strictGroups: false,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment