class-validator: Schema example in README.md not work
I just copy and paste, change object value and try to trigger errors…but result shows empty array… please help, give me some clues… i was trapped by this in hours…thank you
----------- terminal info --------------- [] Validation succeed.
import {ValidationSchema, registerSchema, validate} from "class-validator";
export 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);
const user = { firstName: "1", secondName: "1", email: "johnycage.com" };
validate("myUserSchema", user).then(errors => {
console.log(errors)
if (errors.length > 0) {
console.log("Validation failed: ", errors);
} else {
console.log("Validation succeed.");
}
});
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 10
- Comments: 17 (5 by maintainers)
FYI, soon we I will open a discussion about removing the schema validation functionality at all from class-validator. There are libraries dedicated for that, so we should not roll our own but focus on the decorator based validation.
Why not at least update the README and say “Don’t even try”? Also spent a whole day debugging this 😐
@vlapo is any update on the issue
It is related to last big refactor. We do not have tests for schema features so I do not realised we broke this feature completely. Sorry for that. I will try fix this in short time.
so I wasted half a day debugging class-validator trying to figure out what AM I doing wrong. You might ask, why didn’t I search the open issues instead of wasting that much time? ok, well, who would have thought that any library could fail the most basic scenario? a validation library that can’t perform any validation is unheard of.
So, as everyone here, I’m wondering is there’s any update on this issue and is this project is still alive?
I understand that open source developers have other responsibilities, but please set the expectations of anyone using your code right.
@NoNameProvided Is there any update on this issue whether your are going to include it in the future or not. Also not to mention that current version is broken
I agree with @NoNameProvided here. This feature seems like a tacked on afterthought to me.
For schema based validation, I would recommend zod.
Duplicate of this issue: https://github.com/typestack/class-validator/issues/1155
Thanks for bumping this (I had no idea this features was broken), but this issue will probably not see a lot of action in the near future, as we have quite low maintainer activity and no way to add more maintainers. See #1719