@@ -36,18 +36,18 @@ npm install @secjs/validator
3636
3737> Use Validator class to extend in your validation classes
3838
39- ``` js
39+ ``` ts
4040import { Validator } from ' @secjs/validator'
4141
4242export class UserValidator extends Validator {
43- createSchema () {
43+ get schema () {
4444 return {
4545 name: ' string|required' ,
4646 email: ' email|required' ,
4747 }
4848 }
4949
50- updateSchema () {
50+ get updateSchema() {
5151 return {
5252 name: ' string' ,
5353 email: ' string' ,
@@ -57,8 +57,109 @@ export class UserValidator extends Validator {
5757
5858const userValidator = new UserValidator ()
5959
60- userValidator .validate ({ name: ' João' , email: ' lenonSec7@gmail.com' }, ' createSchema' ) // Return on first error or undefined
61- userValidator .validateAll ({ name: ' João' , email: ' lenonSec7@gmail.com' }, ' updateSchema' ) // Return all errors or undefined
60+ try {
61+ const validatedData = await userValidator .validate ({
62+ name: ' João' ,
63+ email: ' lenonSec7@gmail.com'
64+ }, ' schema' )
65+
66+ return validatedData
67+ } catch (error ) {
68+ // All the validation errors found
69+ throw error
70+ }
71+ ```
72+
73+ ### Nice validator options
74+
75+ > Fail on first and clean the data
76+
77+ ``` ts
78+ import { Validator } from ' @secjs/validator'
79+
80+ export class UserValidator extends Validator {
81+ get validateAll() {
82+ return false
83+ }
84+
85+ get removeAdditional() {
86+ return true
87+ }
88+
89+ get schema() {
90+ return {
91+ name: ' string|required' ,
92+ email: ' email|required' ,
93+ }
94+ }
95+ }
96+
97+ const userValidator = new UserValidator ()
98+
99+ try {
100+ const validatedData = await userValidator .validate ({
101+ name: ' João' ,
102+ email: ' lenonSec7@gmail.com' ,
103+ additionalProp: ' hello'
104+ }, ' schema' )
105+
106+ return validatedData // { name: 'João', email: 'lenonSec7@gmail.com' } without additionalProp
107+ } catch (error ) {
108+ // Only the first validation error found
109+ throw error
110+ // [
111+ // {
112+ // message: 'required validation failed on name',
113+ // validation: 'required',
114+ // field: 'name',
115+ // }
116+ // ]
117+ }
118+ ```
119+
120+ ### Custom error messages
121+
122+ > Use custom error messages and Internationalization support
123+
124+ ``` ts
125+ import { Validator } from ' @secjs/validator'
126+
127+ export class UserValidator extends Validator {
128+ get messages() {
129+ return {
130+ email: ' {{ field }} is not a valid email' ,
131+ // pt-br
132+ ' name.required' : ' {{ field }} é obrigatório para criar um usuário'
133+ }
134+ }
135+
136+ get schema() {
137+ return {
138+ name: ' string|required' ,
139+ email: ' email|required' ,
140+ }
141+ }
142+ }
143+
144+ const userValidator = new UserValidator ()
145+
146+ try {
147+ // try implementation...
148+ } catch (error ) {
149+ throw error
150+ // [
151+ // {
152+ // message: 'name é obrigatório para criar um usuário',
153+ // validation: 'required',
154+ // field: 'name',
155+ // },
156+ // {
157+ // message: 'email is not a valid email',
158+ // validation: 'email',
159+ // field: 'email',
160+ // }
161+ // ]
162+ }
62163```
63164
64165## Sanitizer
@@ -69,22 +170,24 @@ userValidator.validateAll({ name: 'João', email: 'lenonSec7@gmail.com' }, 'upda
69170import { Sanitizer } from ' @secjs/validator'
70171
71172export class UserSanitizer extends Sanitizer {
72- createSchema () {
173+ get schema () {
73174 return {
74- email: ' trim' ,
175+ email: ' trim|lower_case ' ,
75176 }
76177 }
77178
78- updateSchema() {
179+ get updateSchema() {
79180 return {
80- email: ' trim' ,
181+ email: ' trim|lower_case ' ,
81182 }
82183 }
83184}
84185
85186const userSanitizer = new UserSanitizer ()
86187
87- userSanitizer .sanitize ({ email: ' lenonSec7@gmail.com' }, ' createSchema' ) // Return the object with sanitizations implemented
188+ userSanitizer .sanitize ({
189+ email: ' lenonSec7@gmail.com '
190+ }, ' schema' ) // Return the object with sanitizations implemented
88191// { email: 'lenonsec7@gmail.com' }
89192```
90193
0 commit comments