Skip to content

Commit 7cd7546

Browse files
braaarNoNameProvided
authored andcommitted
docs: remove documentation about schema based validation
1 parent 45d125e commit 7cd7546

File tree

1 file changed

+1
-81
lines changed

1 file changed

+1
-81
lines changed

README.md

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -905,87 +905,7 @@ isBoolean(value);
905905

906906
## Defining validation schema without decorators
907907

908-
You can define your validation schemas without decorators:
909-
910-
- you can define it in the separate object
911-
- you can define it in the `.json` file
912-
913-
This feature maybe useful in the cases if:
914-
915-
- are using es5/es6 and don't have decorators available
916-
- you don't have a classes, and instead using interfaces
917-
- you don't want to use model at all
918-
- you want to have a validation schema separate of your model
919-
- you want beautiful json-schema based validation models
920-
- you simply hate decorators
921-
922-
Here is an example of using it:
923-
924-
1. Create a schema object:
925-
926-
```typescript
927-
import { ValidationSchema } from 'class-validator';
928-
export let UserValidationSchema: ValidationSchema = {
929-
// using interface here is not required, its just for type-safety
930-
name: 'myUserSchema', // this is required, and must be unique
931-
properties: {
932-
firstName: [
933-
{
934-
type: 'minLength', // validation type. All validation types are listed in ValidationTypes class.
935-
constraints: [2],
936-
},
937-
{
938-
type: 'maxLength',
939-
constraints: [20],
940-
},
941-
],
942-
lastName: [
943-
{
944-
type: 'minLength',
945-
constraints: [2],
946-
},
947-
{
948-
type: 'maxLength',
949-
constraints: [20],
950-
},
951-
],
952-
email: [
953-
{
954-
type: 'isEmail',
955-
},
956-
],
957-
},
958-
};
959-
```
960-
961-
Same schema can be provided in `.json` file, depend on your wish.
962-
963-
2. Register your schema:
964-
965-
```typescript
966-
import { registerSchema } from 'class-validator';
967-
import { UserValidationSchema } from './UserValidationSchema';
968-
registerSchema(UserValidationSchema); // if schema is in .json file, then you can simply do registerSchema(require("path-to-schema.json"));
969-
```
970-
971-
Better to put this code in a global place, maybe when you bootstrap your application, for example in `app.ts`.
972-
973-
3. Validate your object using validation schema:
974-
975-
```typescript
976-
import { validate } from 'class-validator';
977-
const user = { firstName: 'Johny', secondName: 'Cage', email: '[email protected]' };
978-
validate('myUserSchema', user).then(errors => {
979-
if (errors.length > 0) {
980-
console.log('Validation failed: ', errors);
981-
} else {
982-
console.log('Validation succeed.');
983-
}
984-
});
985-
```
986-
987-
That's it. Here `"myUserSchema"` is the name of our validation schema.
988-
`validate` method will perform validation based on this schema
908+
Schema-based validation without decorators is no longer supported by `class-validator`. This feature was broken in version 0.12 and it will not be fixed. If you are interested in schema-based validation, you can find several such frameworks in [the zod readme's comparison section](https://github.com/colinhacks/zod#comparison).
989909

990910
## Validating plain objects
991911

0 commit comments

Comments
 (0)