class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

fix: schema-based validation does not work

Open thorek opened this issue 6 years ago • 3 comments

Tested with the example from the README - the following validation will always show "Validation succeed". Even in case the object should not pass.

 let UserValidationSchema: ValidationSchema = { // using interface here is not required, its just for type-safety
    name: "myUserSchema", // this is required, and must be unique
    properties: {
        firstName: [{
            type: "minLength", // validation type. All validation types are listed in ValidationTypes class.
            constraints: [2]
        }, {
            type: "maxLength",
            constraints: [20]
        }],
        lastName: [{
            type: "minLength",
            constraints: [2]
        }, {
            type: "maxLength",
            constraints: [20]
        }],
        email: [{
            type: "isEmail"
        }]
    }
  };
  registerSchema(UserValidationSchema); // if schema is in .json file, then you can simply do registerSchema(require("path-to-schema.json"));
  const user = { firstName: "J", secondName: "Cage", email: "johny", some: "thing" };
  validate("myUserSchema", user).then(errors => {
      if (errors.length > 0) {
          console.log("Validation failed: ", errors);
      } else {
          console.log("Validation succeed.");
      }
  });

thorek avatar May 14 '20 09:05 thorek

Duplicate of #595.

ozzywalsh avatar May 17 '20 19:05 ozzywalsh

Schema-based validation will be deprecated in the future probably as a result marking this as low prio.

NoNameProvided avatar Aug 09 '20 00:08 NoNameProvided

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Sep 08 '20 00:09 github-actions[bot]