params_checker 0.2.1 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0013a86614eb2a40c8e5d526ebba0cdd244367c32e86812f388059e26abaf952
4
- data.tar.gz: 675209c8ae0ac1ba49bffd934afa6bb34a66e7c02e107cfbd0718336f362534c
3
+ metadata.gz: c3075bf683b197823fd1825abe5f3f8f5ff503e8eb4735c9210c2f311407e6a4
4
+ data.tar.gz: f65abbd75d382dc6a8d407b8d3bc8af3f985a2a46bb811edab4854afe60da350
5
5
  SHA512:
6
- metadata.gz: a83330c3e167398a9d90773f568d9666fd4c63fefcae418d3767fc9c868952f09de975435a456ae5a3f7f6c3371a772a2feb64dc3181a3301d119b84fc13260d
7
- data.tar.gz: 797e848185371dc8ca275b47fe0d2c92ac8bd1ac05179b1a772eff6b3e3ca7f9145c7dc8b44e294a6d9d897927004f3cb0968465c30ef57877f6269c2e9416d6
6
+ metadata.gz: 962476c34df899a36236a37811e999f0b75ca421a0854067b116209efe9ecc7791d0221a5fb652c6e5706768da5ed2b93bc2f8f29c7c972860579521ba46c1c9
7
+ data.tar.gz: fbea5edec0b3404d30a108195fc4574b60fd3767899c50e563b50851f07a8e57c1f68db3393b808553291a7ae15818c61df874cdeba9579e5201ecf21de1891c
data/README.md CHANGED
@@ -1,9 +1,56 @@
1
1
  # ParamsChecker
2
- Short description and motivation.
2
+ ### Table of Content
3
+ - [Introducion](#introduction)
4
+ - [Installation](#installation)
5
+ - [Quickstart](#quickstart)
6
+ - [Usage](#usage)
7
+ - [1. How a Params Checker command behave](#1-how-a-params-checker-command-behave)
8
+ - [2. Schema fields](#2-schema-fields)
9
+ - [3. Error types](#3-error-types)
10
+ - [4. Custom validation](#4-custom-validation)
11
+ - [5. Nested ParamsChecker](#5-nested-paramschecker)
12
+ - [More examples](#more-examples)
13
+ - [1. Basic usage](#1-basic-usage)
14
+ - [2. Advanced usage](#2-advanced-usage)
15
+ - [Api Details](#api-details)
16
+ - [1. Available schema fields](#1-available-schema-fields)
17
+ - [2. Available schema fields's arguments](#2-available-schema-fieldss-arguments)
18
+ - [Incoming features](#incoming-features)
19
+ - [Contributing](#contributing)
20
+ - [License](#license)
21
+ ## Introduction
22
+ - When your Rails application is still small, rails's model validation indeed is very convenient. Adding new validations is easy, rails always validate for you,...But once your app grows up, Rails's model validation will become messy. You have to validate differently in different use cases, your model grows big(because it contains too many validations),... That's when Params Checker comes in.
23
+ - ParamsChecker's main purpose is to help you validating data, seperating your validation code from your model. ParamsChecker is partly inspired by Django REST framework's validation module.
24
+ - It's very easy and fast to use Params Checker. These is no learning curve.
25
+ - Example:
26
+ ```ruby
27
+ class CreateUserValidator < ParamsChecker::BaseParamsChecker
28
+ def schema
29
+ {
30
+ age: int_field,
31
+ email: char_field
32
+ }
33
+ end
34
+ end
3
35
 
4
- ## Usage
5
- How to use my plugin.
36
+ cmd = CreateUserValidator.call(
37
+ params: { age: 5 }
38
+ )
39
+
40
+ cmd.failure?
41
+ => true
6
42
 
43
+ cmd.errors
44
+ => {
45
+ errors: [{
46
+ message: 'Fields are not valid',
47
+ error_type: 'fields_errors',
48
+ field_errors: {
49
+ email: 'This field is required.',
50
+ }
51
+ }]
52
+ }
53
+ ```
7
54
  ## Installation
8
55
  Add this line to your application's Gemfile:
9
56
 
@@ -18,11 +65,1127 @@ $ bundle
18
65
 
19
66
  Or install it yourself as:
20
67
  ```bash
21
- $ gem 'params_checker', git: 'git@gitlab.com:rexylab/params-checker.git', branch: 'development'
68
+ $ gem 'params_checker', git: 'git@gitlab.com:rexylab/params-checker.git', branch: 'master'
69
+ ```
70
+ ## Quickstart
71
+ - You can just use a simple params checker like this:
72
+ ```ruby
73
+ class BasicUsageValidator < ParamsChecker::BaseParamsChecker
74
+ def schema
75
+ {
76
+ name: char_field,
77
+ age: int_field,
78
+ email: char_field
79
+ }
80
+ end
81
+ end
82
+ ```
83
+ - Or if you want more conditional validation, you can pass arguments like this. For more detail, please read [here](#1-available-schema-fields).
84
+
85
+ ```ruby
86
+
87
+ class BasicUsageValidator < ParamsChecker::BaseParamsChecker
88
+ def schema
89
+ {
90
+ name: char_field(min_length: 5, max_length: 255),
91
+ age: int_field(min: 0, required: false, allow_nil: true),
92
+ email: char_field(allow_blank: true, default: 'ted@rexy.tech')
93
+ }
94
+ end
95
+ end
22
96
  ```
97
+ - You can also add your own custom logic in checker functions. For more detail, please read [here](#4-custom-validation).
98
+
99
+ ```ruby
100
+ # you can also add your own custom logic in checker functions, like this
101
+ class BasicUsageValidator < ParamsChecker::BaseParamsChecker
102
+ def schema
103
+ {
104
+ name: char_field,
105
+ age: int_field,
106
+ email: char_field
107
+ }
108
+ end
109
+
110
+ def check_name(name)
111
+ # add error for the :name field
112
+ add_error('This name is already exists.') if name == 'Ted Nguyen'
113
+
114
+ # you can format your fields and return it
115
+ name + ' RexyTech'
116
+ end
117
+
118
+ def check_email(email)
119
+ # raise error, stop all others validations
120
+ raise_error('This email is already exists.') if email == 'ted@rexy.tech'
121
+
122
+ email
123
+ end
124
+ end
125
+
126
+ ```
127
+ ## Usage
128
+ ### 1. How a Params Checker command behave:
129
+ - If your params is invalid:
130
+ - cmd.success? will return false
131
+ - cmd.failure? will return true
132
+ - cmd.result will return formatted params
133
+ - cmd.errors will return a empty hash({})
134
+ - If your params is valid:
135
+ - cmd.success? will return true
136
+ - cmd.failure? will return false
137
+ - cmd.result will return a empty hash({})
138
+ - cmd.errors will return the errors
139
+ - Example:
140
+ ```ruby
141
+ class CreateUserValidator < ParamsChecker::BaseParamsChecker
142
+ def schema
143
+ {
144
+ age: int_field,
145
+ email: char_field
146
+ }
147
+ end
148
+ end
149
+
150
+ cmd = CreateUserValidator.call(
151
+ params: {}
152
+ )
153
+
154
+ cmd.failure?
155
+ => true
156
+
157
+ cmd.errors
158
+ => {
159
+ errors: [{
160
+ message: 'Fields are not valid',
161
+ error_type: 'fields_errors',
162
+ field_errors: {
163
+ name: 'This field is required.',
164
+ email: 'This field is required.'
165
+ }
166
+ }]
167
+ }
168
+ ```
169
+ - For more examples, read [here](#1-basic-usage).
170
+
171
+ ### 2. Schema fields:
172
+ - At the time of writing this document, Params Checker supports 16 kinds of data type checking(we call these "schema fields"). Read details about these schema fields [here](#1-available-schema-fields).
173
+ - You can modify a schema field's arguments to tell ParamsChecker how you want to validate that specific field.
174
+ - Example:
175
+ ```ruby
176
+ class Validator < ParamsChecker::BaseParamsChecker
177
+ ## only names with a length from 5 characters to 20 characters can pass this ParamsChecker
178
+ def schema
179
+ {
180
+ name: char_field(min_length: 5, max_length: 20)
181
+ }
182
+ end
183
+ end
184
+ ```
185
+ - For more examples, read [here](#1-custom-schema-fieldss-arguments).
186
+
187
+ ### 3. Error types
188
+ - Currently ParamsChecker have 2 error types
189
+ - field_errors
190
+ - example:
191
+ ```ruby
192
+ {
193
+ errors: [{
194
+ message: 'Fields are not valid',
195
+ error_type: 'fields_errors',
196
+ field_errors: {
197
+ name: 'This field is required.'
198
+ }
199
+ }]
200
+ }
201
+ ```
202
+ - general_error
203
+ - example:
204
+ ```ruby
205
+ {
206
+ errors: [{
207
+ message: "Email or password invalid",
208
+ error_type: "general_error"
209
+ }]
210
+ }
211
+ ```
212
+ - A ParamsChecker validator will return field_errors when:
213
+ - Field is invalid (validated by [schema fields](#2-schema-fields))
214
+ - An error is raised by #add_error(Read more [here](#4-custom-validation))
215
+ - A ParamsChecker validator will return general_error when:
216
+ - An error is raised by #raise_error(Read more [here](#4-custom-validation))
217
+ - A ParamsChecker validator can have multiple field_errors, but only have one general_error.
218
+ - If a ParamsChecker validator that have both field_errors and general_error, it will always only show the general_error.
219
+ ### 4. Custom validation
220
+ - In reality, there will be edge cases that you need to manually validate(like, validating if the user have enough permissions, validate if the user have enough balance to make transactions, ...) ParamsChecker provide checker functions to help you manually validate things ParamsChecker can not validate.
221
+ - example:
222
+ ```ruby
223
+ class Validator < ParamsChecker::BaseParamsChecker
224
+ def schema
225
+ {
226
+ name: char_field
227
+ }
228
+ end
229
+
230
+ # you can define a custom checker function by defining
231
+ # a method with the name convention: check_<field_name>
232
+ # example:
233
+ def check_name(name_param)
234
+ # put your validation logic here.
235
+
236
+ # the add_method will add an error to the :name field, stop all the code lines bellow it
237
+ add_error('Name already exists.') if name_param == 'Ted Nguyen'
238
+
239
+ # You can modify, format the param and return it here.
240
+ name_param + ' RexyTech'
241
+ end
242
+ end
243
+
244
+ # invalid params(lacking field "name")
245
+ cmd = Validator.call(
246
+ params: {}
247
+ )
248
+
249
+ cmd.failure?
250
+ => true
251
+
252
+ cmd.errors
253
+ # the custom validator functions only execute once
254
+ # the all params pass the type checking step(check
255
+ # if name is string, check if name's length is valid,...)
256
+ => {
257
+ errors: [{
258
+ message: 'Fields are not valid',
259
+ error_type: 'fields_errors',
260
+ field_errors: {
261
+ name: 'This field is required.'
262
+ }
263
+ }]
264
+ }
265
+
266
+ # invalid params(Name already exists)
267
+ cmd = Validator.call(
268
+ params: { name: 'Ted Nguyen' }
269
+ )
270
+
271
+ cmd.failure?
272
+ => true
273
+
274
+ cmd.errors
275
+ => {
276
+ errors: [{
277
+ message: 'Fields are not valid',
278
+ error_type: 'fields_errors',
279
+ field_errors: {
280
+ name: 'Name already exists.'
281
+ }
282
+ }]
283
+ }
284
+
285
+ # valid params
286
+ cmd = Validator.call(
287
+ params: { name: 'Unique Ted Nguyen' }
288
+ )
289
+
290
+ cmd.failure?
291
+ => false
292
+ cmd.success?
293
+ => true
294
+
295
+ cmd.result
296
+ => {
297
+ name: 'Unique Ted Nguyen RexyTech'
298
+ }
299
+
300
+ ```
301
+ - You can also retrieve other params to more validation by using the second parameter
302
+ - example:
303
+ ```ruby
304
+ class Validator < ParamsChecker::BaseParamsChecker
305
+ def schema
306
+ {
307
+ name: char_field,
308
+ age: int_field
309
+ }
310
+ end
311
+
312
+ def check_name(name_param, params)
313
+ if name_param == 'Ted Nguyen' || params[:age] == 5
314
+ # something like this
315
+ add_error('Your name can not be Ted Nguyen and your age can not be 5.')
316
+ end
317
+
318
+ # You can pass the second argument as the field name
319
+ # to link the error to a specify field.
320
+
321
+ # In check_<field_name> functions, as default, the second
322
+ # argument will be the <field_name> of the function( in
323
+ # check_name function, it is :name)
324
+ add_error('This name also already exists.', :age) if name_param == 'Ted Nguyen 1'
325
+
326
+ name_param
327
+ end
328
+ end
329
+
330
+ cmd = Validator.call(
331
+ params: { name: 'Unique Ted Nguyen', age: 5 }
332
+ )
333
+
334
+ cmd.failure?
335
+ => true
336
+
337
+ cmd.errors
338
+ => {
339
+ errors: [{
340
+ message: 'Fields are not valid',
341
+ error_type: 'fields_errors',
342
+ field_errors: {
343
+ name: 'Your name can not be Ted Nguyen and your age can not be 5.'
344
+ }
345
+ }]
346
+ }
347
+ ```
348
+ - You can validate multiple fields at once, removing old fields, or adding new fields by using #check
349
+ - example:
350
+ ```ruby
351
+ class Validator < ParamsChecker::BaseParamsChecker
352
+ def schema
353
+ {
354
+ name: char_field,
355
+ age: int_field
356
+ }
357
+ end
358
+
359
+ def check(params)
360
+ if params[:name] == 'Ted Nguyen' || params[:age] == 5
361
+ # In #check function, you need to specify which field should
362
+ # ParamsChecker link the error to.
363
+ add_error('Your name can not be Ted Nguyen and your age can not be 5.', :name)
364
+ end
365
+
366
+ params.except!(:age)
367
+ params[:is_super_admin] = params[:name] == 'Admin Ted Nguyen'
368
+ params
369
+ end
370
+ end
371
+
372
+ cmd = Validator.call(
373
+ params: { name: 'Unique Ted Nguyen', age: 5 }
374
+ )
375
+
376
+ cmd.failure?
377
+ => false
378
+
379
+ cmd.result
380
+ => {
381
+ name: "Unique Ted Nguyen",
382
+ is_super_admin: false
383
+ }
384
+ ```
385
+ - Besides the function #add_error to add one error message to one field, we also have #raise_error, which add the error message as the general error.
386
+ - example:
387
+ ```ruby
388
+ class Validator < ParamsChecker::BaseParamsChecker
389
+ def schema
390
+ {
391
+ email: char_field,
392
+ password: char_field
393
+ }
394
+ end
395
+
396
+ def check(params)
397
+ if params[:email] != 'ted@rexy.tech' || params[:password] != 'password'
398
+ # one ParamsChecker error can only have one general_error,
399
+ # so if we use raise_error, it will stop all the code of
400
+ # a ParamsChecker Validate and return general_error(while
401
+ # add_error only stop the code of current function)
402
+ raise_error('Email or password invalid')
403
+ end
404
+
405
+ params
406
+ end
407
+ end
408
+
409
+ cmd = Validator.call(
410
+ params: { email: 'ted+1@rexy.tech', password: 'password1' }
411
+ )
412
+
413
+ cmd.failure?
414
+ => true
415
+
416
+ cmd.errors
417
+ => {
418
+ errors: [{
419
+ message: "Email or password invalid",
420
+ error_type: "general_error"
421
+ }]
422
+ }
423
+ ```
424
+
425
+ - For more examples, read [here](#2-using-custom-validators).
426
+ ### 5. Nested ParamsChecker
427
+ - ParamsChecker also support nested ParamsChecker.
428
+ - example:
429
+ ```ruby
430
+ class Mother < ParamsChecker::BaseParamsChecker
431
+ def schema
432
+ {
433
+ name: char_field,
434
+ age: int_field
435
+ }
436
+ end
437
+ end
438
+
439
+ class Kid < ParamsChecker::BaseParamsChecker
440
+ def schema
441
+ {
442
+ name: char_field,
443
+ age: int_field,
444
+ mother: Mother.init
445
+ }
446
+ end
447
+ end
448
+
449
+ cmd = Kid.call(
450
+ params: {
451
+ name: 'Ted',
452
+ age: 15,
453
+ mother: {
454
+ name: "Ted's mother",
455
+ }
456
+ }
457
+ )
458
+
459
+ cmd.failure?
460
+ => true
461
+ cmd.errors
462
+ => {
463
+ errors: [{
464
+ message: 'Fields are not valid',
465
+ error_type: 'fields_errors',
466
+ field_errors: {
467
+ mother: {
468
+ age: "This field is required."
469
+ }
470
+ }
471
+ }]
472
+ }
473
+
474
+ cmd = Kid.call(
475
+ params: {
476
+ name: 'Ted',
477
+ age: 15,
478
+ mother: {
479
+ name: "Ted's mother",
480
+ age: 35
481
+ }
482
+ }
483
+ )
484
+
485
+ cmd.failure?
486
+ => false
487
+ ```
23
488
 
489
+ - You can also pass the argument "many", to treat the it as an array of multiple ParamsCheckers:
490
+ - example:
491
+ ```ruby
492
+ class Book < ParamsChecker::BaseParamsChecker
493
+ def schema
494
+ {
495
+ name: char_field,
496
+ released_at: date_field,
497
+ }
498
+ end
499
+ end
500
+
501
+ class Author < ParamsChecker::BaseParamsChecker
502
+ def schema
503
+ {
504
+ name: char_field,
505
+ age: int_field,
506
+ books: Book.init(many: true)
507
+ }
508
+ end
509
+ end
510
+
511
+ cmd = Author.call(
512
+ params: {
513
+ name: 'Ted',
514
+ age: 15,
515
+ books: [
516
+ {
517
+ name: 'Harry Potter 1',
518
+ released_at: '1997-06-26'
519
+ },
520
+ {
521
+ name: 'Harry Potter 2',
522
+ },
523
+ {
524
+ name: 'Harry Potter 3',
525
+ released_at: '1999-07-08'
526
+ },
527
+ ]
528
+ }
529
+ )
530
+
531
+ cmd.failure?
532
+ => true
533
+ cmd.errors
534
+ => {
535
+ errors: [{
536
+ message: 'Fields are not valid',
537
+ error_type: 'fields_errors',
538
+ field_errors: {
539
+ books: [
540
+ nil,
541
+ {
542
+ released_at: "This field is required."
543
+ },
544
+ nil
545
+ ]
546
+ }
547
+ }]
548
+ }
549
+ ```
550
+
551
+ ## More examples
552
+ ### 1. Basic usage
553
+ ```ruby
554
+ class BasicUsageValidator < ParamsChecker::BaseParamsChecker
555
+ def schema
556
+ {
557
+ name: char_field,
558
+ age: int_field,
559
+ email: char_field
560
+ }
561
+ end
562
+ end
563
+
564
+ # validate number 1
565
+ params = {}
566
+ cmd = BasicUsageValidator.call(
567
+ params: params
568
+ )
569
+
570
+ cmd.success?
571
+ => false
572
+ cmd.failure?
573
+ => true
574
+
575
+ cmd.result
576
+ => {}
577
+ cmd.errors
578
+ => {
579
+ errors: [{
580
+ message: 'Fields are not valid',
581
+ error_type: 'fields_errors',
582
+ field_errors: {
583
+ name: 'This field is required.',
584
+ age: 'This field is required.',
585
+ email: 'This field is required.'
586
+ }
587
+ }]
588
+ }
589
+
590
+ # validate number 2
591
+ params = {
592
+ name: true,
593
+ email: 'ted@rexy.tech'
594
+ }
595
+ cmd = BasicUsageValidator.call(
596
+ params: params
597
+ )
598
+
599
+ cmd.success?
600
+ => false
601
+ cmd.failure?
602
+ => true
603
+
604
+ cmd.result
605
+ => {}
606
+ cmd.errors
607
+ => {
608
+ errors: [{
609
+ message: 'Fields are not valid',
610
+ error_type: 'fields_errors',
611
+ field_errors: {
612
+ name: "This field's type must be string.",
613
+ age: 'This field is required.'
614
+ }
615
+ }]
616
+ }
617
+
618
+ # validate number 3
619
+ params = {
620
+ name: 'ted nguyen',
621
+ age: 2_000_000_001,
622
+ email: 'a' * 256
623
+ }
624
+ cmd = BasicUsageValidator.call(
625
+ params: params
626
+ )
627
+
628
+ cmd.success?
629
+ => false
630
+ cmd.failure?
631
+ => true
632
+
633
+ cmd.result
634
+ => {}
635
+ cmd.errors
636
+ => {
637
+ errors: [{
638
+ message: 'Fields are not valid',
639
+ error_type: 'fields_errors',
640
+ field_errors: {
641
+ age: "This integer field's value must be in range from -2000000000 to 2000000000.",
642
+ email: "This string field's length must be in range from 0 to 255."
643
+ }
644
+ }]
645
+ }
646
+
647
+
648
+ # validate number 4
649
+ params = {
650
+ name: 'ted nguyen',
651
+ age: 23,
652
+ email: 'ted@rexy.tech'
653
+ }
654
+ cmd = BasicUsageValidator.call(
655
+ params: params
656
+ )
657
+
658
+ cmd.success?
659
+ => true
660
+ cmd.failure?
661
+ => false
662
+
663
+ cmd.result
664
+ => {
665
+ name: 'ted nguyen',
666
+ age: 23,
667
+ email: 'ted@rexy.tech'
668
+ }
669
+ cmd.errors
670
+ => {}
671
+
672
+ ```
673
+ ### 2. Advance usage
674
+ ##### 1. Custom schema fields's arguments
675
+
676
+ ```ruby
677
+ class AdvancedUsageValidator < ParamsChecker::BaseParamsChecker
678
+ def schema
679
+ {
680
+ name: char_field(min_length: 4, max_length: 30),
681
+ age: int_field(max: 130),
682
+ email: char_field,
683
+ is_male: boolean_field(required: false, default: true),
684
+ phone: char_field(allow_blank: true)
685
+ }
686
+ end
687
+ end
688
+
689
+ # validate number 5
690
+ params = {
691
+ name: 'Ted',
692
+ age: 135,
693
+ email: '',
694
+ phone: ''
695
+ }
696
+ cmd = AdvancedUsageValidator.call(
697
+ params: params
698
+ )
699
+
700
+ cmd.success?
701
+ => false
702
+ cmd.failure?
703
+ => true
704
+
705
+ cmd.result
706
+ => {}
707
+ cmd.errors
708
+ => {
709
+ errors: [{
710
+ message: 'Fields are not valid',
711
+ error_type: 'fields_errors',
712
+ field_errors: {
713
+ name: "This string field's length must be in range from 4 to 30.",
714
+ age: "This integer field's value must be in range from -2000000000 to 130.",
715
+ email: 'This field cannot be blank.'
716
+ }
717
+ }]
718
+ }
719
+
720
+ # validate number 6
721
+ params = {
722
+ name: 'Ted Nguyen',
723
+ age: 23,
724
+ email: 'ted@rexy.tech',
725
+ phone: ''
726
+ }
727
+ cmd = AdvancedUsageValidator.call(
728
+ params: params
729
+ )
730
+
731
+ cmd.success?
732
+ => true
733
+ cmd.failure?
734
+ => false
735
+
736
+ cmd.result
737
+ => {
738
+ name: 'Ted Nguyen',
739
+ age: 23,
740
+ email: 'ted@rexy.tech',
741
+ phone: ''
742
+ }
743
+ cmd.errors
744
+ => {}
745
+ ```
746
+ ##### 2. Using custom validators
747
+
748
+ ```ruby
749
+ class AnotherAdvancedValidator < ParamsChecker::BaseParamsChecker
750
+ def schema
751
+ {
752
+ name: char_field,
753
+ age: int_field,
754
+ email: char_field
755
+ }
756
+ end
757
+
758
+ def check_name(name)
759
+ add_error('This name is already exists.') if name == 'Ted Nguyen'
760
+
761
+ name
762
+ end
763
+
764
+ def check_age(age)
765
+ add_error('You must be older than 18 years old.') if age < 18
766
+
767
+ age
768
+ end
769
+
770
+ def check_email(email)
771
+ raise_error('This email is already exists.') if email == 'ted@rexy.tech'
772
+
773
+ email
774
+ end
775
+
776
+ def check(params)
777
+ raise_error("You don't have permission to create a user.") unless context[:is_super_admin]
778
+
779
+ params[:is_adult] = params[:age] >= 18
780
+ params
781
+ end
782
+ end
783
+
784
+ # validate number 7
785
+ params = {
786
+ name: 'Ted Nguyen',
787
+ age: 17,
788
+ email: 'ted@rexy.tech'
789
+ }
790
+ cmd = AdvancedUsageValidator.call(
791
+ params: params
792
+ )
793
+
794
+ cmd.success?
795
+ => false
796
+ cmd.failure?
797
+ => true
798
+
799
+ cmd.result
800
+ => {}
801
+ cmd.errors
802
+ => {
803
+ errors: [
804
+ {
805
+ message: "This email is already exists.",
806
+ error_type: "general_error"
807
+ }
808
+ ]
809
+ }
810
+
811
+ # validate number 8
812
+ params = {
813
+ name: 'Ted Nguyen',
814
+ age: 17,
815
+ email: 'ted+1@rexy.tech'
816
+ }
817
+ cmd = AdvancedUsageValidator.call(
818
+ params: params
819
+ )
820
+
821
+ cmd.success?
822
+ => false
823
+ cmd.failure?
824
+ => true
825
+
826
+ cmd.result
827
+ => {}
828
+ cmd.errors
829
+ => {
830
+ errors: [
831
+ {
832
+ message: "You don't have permission to create a user.",
833
+ error_type: "general_error"
834
+ }
835
+ ]
836
+ }
837
+
838
+ # validate number 9
839
+ params = {
840
+ name: 'Ted Nguyen',
841
+ age: 17,
842
+ email: 'ted+1@rexy.tech'
843
+ }
844
+ cmd = AdvancedUsageValidator.call(
845
+ params: params
846
+ )
847
+
848
+ cmd.success?
849
+ => false
850
+ cmd.failure?
851
+ => true
852
+
853
+ cmd.result
854
+ => {}
855
+ cmd.errors
856
+ => {
857
+ errors: [{
858
+ message: 'Fields are not valid',
859
+ error_type: 'fields_errors',
860
+ field_errors: {
861
+ name: 'This name is already exists.',
862
+ age: 'You must be older than 18 years old.'
863
+ }
864
+ }]
865
+ }
866
+
867
+ # validate number 10
868
+ params = {
869
+ name: 'Teddy Nguyen',
870
+ age: 19,
871
+ email: 'ted+1@rexy.tech'
872
+ }
873
+ cmd = AdvancedUsageValidator.call(
874
+ params: params
875
+ )
876
+
877
+ cmd.success?
878
+ => false
879
+ cmd.failure?
880
+ => true
881
+
882
+ cmd.result
883
+ => {
884
+ name: 'Teddy Nguyen',
885
+ age: 19,
886
+ email: 'ted+1@rexy.tech'
887
+ }
888
+ cmd.errors
889
+ => {}
890
+ ```
891
+
892
+ ## Api Details
893
+ ### 1. Available schema fields:
894
+ - text_field
895
+ - [required](#required)
896
+ - [default](#default)
897
+ - [allow_blank](#allow_blank)
898
+ - [min_length](#min_length)
899
+ - [max_length](#max_length)
900
+ - [allow_nil](#allow_nil)
901
+ - char_field
902
+ - [required](#required)
903
+ - [default](#default)
904
+ - [allow_blank](#allow_blank)
905
+ - [min_length](#min_length)
906
+ - [max_length](#max_length)
907
+ - [allow_nil](#allow_nil)
908
+ - bignum_field
909
+ - [required](#required)
910
+ - [default](#default)
911
+ - [min](#min)
912
+ - [max](#max)
913
+ - [allow_nil](#allow_nil)
914
+ - num_field
915
+ - [required](#required)
916
+ - [default](#default)
917
+ - [min](#min)
918
+ - [max](#max)
919
+ - [allow_nil](#allow_nil)
920
+ - bigint_field
921
+ - [required](#required)
922
+ - [default](#default)
923
+ - [min](#min)
924
+ - [max](#max)
925
+ - [allow_nil](#allow_nil)
926
+ - int_field
927
+ - [required](#required)
928
+ - [default](#default)
929
+ - [min](#min)
930
+ - [max](#max)
931
+ - [allow_nil](#allow_nil)
932
+ - positive_bignum_field
933
+ - [required](#required)
934
+ - [default](#default)
935
+ - [min](#min)
936
+ - [max](#max)
937
+ - [allow_nil](#allow_nil)
938
+ - positive_num_field
939
+ - [required](#required)
940
+ - [default](#default)
941
+ - [min](#min)
942
+ - [max](#max)
943
+ - [allow_nil](#allow_nil)
944
+ - positive_bigint_field
945
+ - [required](#required)
946
+ - [default](#default)
947
+ - [min](#min)
948
+ - [max](#max)
949
+ - [allow_nil](#allow_nil)
950
+ - positive_int_field
951
+ - [required](#required)
952
+ - [default](#default)
953
+ - [min](#min)
954
+ - [max](#max)
955
+ - [allow_nil](#allow_nil)
956
+ - arr_field
957
+ - [required](#required)
958
+ - [default](#default)
959
+ - [allow_empty](#allow_empty)
960
+ - [allow_nil](#allow_nil)
961
+ - hash_field
962
+ - [required](#required)
963
+ - [default](#default)
964
+ - [allow_nil](#allow_nil)
965
+ - date_field
966
+ - [required](#required)
967
+ - [default](#default)
968
+ - [allow_nil](#allow_nil)
969
+ - time_field
970
+ - [required](#required)
971
+ - [default](#default)
972
+ - [allow_nil](#allow_nil)
973
+ - datetime_field
974
+ - [required](#required)
975
+ - [default](#default)
976
+ - [allow_nil](#allow_nil)
977
+ - boolean_field
978
+ - [required](#required)
979
+ - [default](#default)
980
+ - [allow_nil](#allow_nil)
981
+
982
+ ### 2. Available schema fields's arguments:
983
+ ##### required
984
+
985
+ - description: to indicate that if you need to validate field's presence or not.
986
+ - default value: true
987
+ - type: boolean
988
+ - example:
989
+ ```ruby
990
+ class Validator < ParamsChecker::BaseParamsChecker
991
+ def schema
992
+ {
993
+ name: char_field(required: false)
994
+ }
995
+ end
996
+ end
997
+ ```
998
+ ##### default:
999
+ - description: if field is absent, this value will be set to field.
1000
+ - default value: nil
1001
+ - type: base on field type
1002
+ - example:
1003
+ ```ruby
1004
+ class Validator < ParamsChecker::BaseParamsChecker
1005
+ def schema
1006
+ {
1007
+ name: char_field(default: 'Ted Nguyen')
1008
+ }
1009
+ end
1010
+ end
1011
+ ```
1012
+ ##### allow_nil
1013
+ - description: to indicate that if you need to validate if field is nil or not.
1014
+ - default value: false
1015
+ - type: boolean
1016
+ - example:
1017
+ ```ruby
1018
+ class Validator < ParamsChecker::BaseParamsChecker
1019
+ def schema
1020
+ {
1021
+ name: char_field(allow_nil: true)
1022
+ }
1023
+ end
1024
+ end
1025
+ ```
1026
+ ##### allow_blank
1027
+ - description: to indicate that if you need to validate if field is blank or not.
1028
+ - default value: false
1029
+ - type: boolean
1030
+ - example:
1031
+ ```ruby
1032
+ class Validator < ParamsChecker::BaseParamsChecker
1033
+ def schema
1034
+ {
1035
+ name: char_field(allow_blank: true)
1036
+ }
1037
+ end
1038
+ end
1039
+ ```
1040
+ ##### allow_empty
1041
+ - description: to indicate that if you need to validate field's emptiness or not.
1042
+ - default value: false
1043
+ - type: boolean
1044
+ - example:
1045
+ ```ruby
1046
+ class Validator < ParamsChecker::BaseParamsChecker
1047
+ def schema
1048
+ {
1049
+ books: arr_field(allow_empty: true)
1050
+ }
1051
+ end
1052
+ end
1053
+ ```
1054
+ ##### min_length
1055
+ - description: to limit minimum string length that the params can pass.
1056
+ - applicable schema fields:
1057
+ - char_field:
1058
+ - default value: 0
1059
+ - acceptable argument value: from 0 to 255
1060
+ - type: integer
1061
+ - text_field:
1062
+ - default value: 0
1063
+ - acceptable argument value: from 0 to 30_000
1064
+ - type: integer
1065
+ - example:
1066
+ ```ruby
1067
+ class Validator < ParamsChecker::BaseParamsChecker
1068
+ def schema
1069
+ {
1070
+ name: char_field(min_length: 5)
1071
+ }
1072
+ end
1073
+ end
1074
+ ```
1075
+ ##### max_length
1076
+ - description: to limit minimum string length that the params can pass.
1077
+ - applicable schema fields:
1078
+ - char_field:
1079
+ - default value: 0
1080
+ - acceptable argument value: from 0 to 255
1081
+ - type: integer
1082
+ - text_field:
1083
+ - default value: 0
1084
+ - acceptable argument value: from 0 to 30_000
1085
+ - type: integer
1086
+ - example:
1087
+ ```ruby
1088
+ class Validator < ParamsChecker::BaseParamsChecker
1089
+ def schema
1090
+ {
1091
+ name: char_field(max_length: 10)
1092
+ }
1093
+ end
1094
+ end
1095
+ ```
1096
+ ##### min
1097
+ - description: to limit minimum value that the params can pass.
1098
+ - applicable schema fields:
1099
+ - int_field:
1100
+ - default value: 2_000_000_000
1101
+ - acceptable argument value: from -2_000_000_000 to 2_000_000_000
1102
+ - type: integer
1103
+ - bigint_field:
1104
+ - default value: 2_000_000_000_000
1105
+ - acceptable argument value: from -2_000_000_000_000 to 2_000_000_000_000
1106
+ - type: integer
1107
+ - positive_int_field:
1108
+ - default value: 0
1109
+ - acceptable argument value: from 0 to 2_000_000_000
1110
+ - type: integer
1111
+ - positive_bigint_field:
1112
+ - default value: 0
1113
+ - acceptable argument value: from 0 to 2_000_000_000_000
1114
+ - type: integer
1115
+ - num_field:
1116
+ - default value: 2_000_000_000
1117
+ - acceptable argument value: from -2_000_000_000 to 2_000_000_000
1118
+ - type: integer, decimal
1119
+ - bignum_field:
1120
+ - default value: 2_000_000_000_000
1121
+ - acceptable argument value: from -2_000_000_000_000 to 2_000_000_000_000
1122
+ - type: integer, decimal
1123
+ - positive_num_field:
1124
+ - default value: 0
1125
+ - acceptable argument value: from 0 to 2_000_000_000
1126
+ - type: integer, decimal
1127
+ - positive_bignum_field:
1128
+ - default value: 0
1129
+ - acceptable argument value: from 0 to 2_000_000_000_000
1130
+ - type: integer, decimal
1131
+ - example:
1132
+ ```ruby
1133
+ class Validator < ParamsChecker::BaseParamsChecker
1134
+ def schema
1135
+ {
1136
+ age: int_field(min: 18)
1137
+ }
1138
+ end
1139
+ end
1140
+ ```
1141
+ ##### max
1142
+ - description: to limit maximum value that the params can pass.
1143
+ - applicable schema fields:
1144
+ - int_field:
1145
+ - default value: 2_000_000_000
1146
+ - acceptable argument value: from -2_000_000_000 to 2_000_000_000
1147
+ - type: integer
1148
+ - bigint_field:
1149
+ - default value: 2_000_000_000_000
1150
+ - acceptable argument value: from -2_000_000_000_000 to 2_000_000_000_000
1151
+ - type: integer
1152
+ - positive_int_field:
1153
+ - default value: 2_000_000_000
1154
+ - acceptable argument value: from 0 to 2_000_000_000
1155
+ - type: integer
1156
+ - positive_bigint_field:
1157
+ - default value: 2_000_000_000_000
1158
+ - acceptable argument value: from 0 to 2_000_000_000_000
1159
+ - type: integer
1160
+ - num_field:
1161
+ - default value: 2_000_000_000
1162
+ - acceptable argument value: from -2_000_000_000 to 2_000_000_000
1163
+ - type: integer, decimal
1164
+ - bignum_field:
1165
+ - default value: 2_000_000_000_000
1166
+ - acceptable argument value: from -2_000_000_000_000 to 2_000_000_000_000
1167
+ - type: integer, decimal
1168
+ - positive_num_field:
1169
+ - default value: 2_000_000_000
1170
+ - acceptable argument value: from 0 to 2_000_000_000
1171
+ - type: integer, decimal
1172
+ - positive_bignum_field:
1173
+ - default value: 2_000_000_000_000
1174
+ - acceptable argument value: from 0 to 2_000_000_000_000
1175
+ - type: integer, decimal
1176
+ - example:
1177
+ ```ruby
1178
+ class Validator < ParamsChecker::BaseParamsChecker
1179
+ def schema
1180
+ {
1181
+ age: int_field(max: 130)
1182
+ }
1183
+ end
1184
+ end
1185
+ ```
1186
+
1187
+ ## Incoming features(incoming)
24
1188
  ## Contributing
25
1189
  Contribution directions go here.
26
-
27
1190
  ## License
28
1191
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).