1
- 'use strict'
2
-
3
1
const Joi = require ( 'joi' )
4
2
const orderBy = require ( 'lodash.orderby' )
5
3
const find = require ( 'lodash.find' )
6
4
const map = require ( 'lodash.map' )
7
- const assert = require ( 'assert' )
5
+ // const assert = require('assert')
8
6
9
7
module . exports = [
10
8
{
@@ -14,76 +12,61 @@ module.exports = [
14
12
continuous_from : 'must be start from {{startIndex}}' ,
15
13
continuous_broken : 'should be {{expectedValue}}'
16
14
} ,
17
- coerce ( value , state , options ) {
18
- const { comparator, startIndex } = this . _flags
19
-
20
- assert . ok (
21
- comparator === undefined ||
22
- typeof comparator === 'function' ||
23
- typeof comparator === 'string' ,
24
- 'comparator must be a function or a string'
25
- )
26
-
27
- if ( comparator ) {
28
- const localState = {
29
- key : state . key ,
30
- path : state . path . concat ( comparator ) ,
31
- parent : state . parent ,
32
- reference : state . reference
33
- }
34
-
35
- const records = orderBy ( map ( value , comparator ) )
36
-
37
- if ( records [ 0 ] !== startIndex ) {
38
- const context = { comparator, records, startIndex }
15
+ rules : [
16
+ {
17
+ name : 'continuous' ,
18
+ description ( params ) {
19
+ return `${ params . comparator } must be an integer and started continuous from ${ params . startIndex } `
20
+ } ,
21
+ params : {
22
+ comparator : Joi . string ( ) . required ( ) ,
23
+ startIndex : Joi . number ( )
24
+ . integer ( )
25
+ . default ( 0 )
26
+ } ,
27
+ validate ( params , value , state , options ) {
28
+ const { comparator, startIndex } = params
39
29
40
- return this . createError (
41
- 'array.continuous_from' ,
42
- context ,
43
- localState ,
44
- options
45
- )
46
- }
30
+ const localState = {
31
+ key : state . key ,
32
+ path : state . path . concat ( comparator ) ,
33
+ parent : state . parent ,
34
+ reference : state . reference
35
+ }
47
36
48
- for ( let idx = 1 ; idx < records . length ; idx ++ ) {
49
- const diff = records [ idx ] - records [ idx - 1 ]
37
+ const records = orderBy ( map ( value , comparator ) )
50
38
51
- if ( diff !== 1 ) {
52
- const obj = find ( value , i => i . idx === records [ idx ] )
53
- const context = {
54
- comparator,
55
- value : obj ,
56
- expectedValue : records [ idx - 1 ] + 1
57
- }
39
+ if ( records [ 0 ] !== startIndex ) {
40
+ const context = { comparator, records, startIndex }
58
41
59
42
return this . createError (
60
- 'array.continuous_broken ' ,
43
+ 'array.continuous_from ' ,
61
44
context ,
62
45
localState ,
63
46
options
64
47
)
65
48
}
66
- }
67
- }
68
49
69
- return value
70
- } ,
71
- rules : [
72
- {
73
- name : 'continuous' ,
74
- description ( params ) {
75
- return `${ params . comparator } must be an integer and started continuous from ${ params . startIndex } ` ;
76
- } ,
77
- params : {
78
- comparator : Joi . any ( ) ,
79
- startIndex : Joi . number ( ) . integer ( ) . optional ( )
80
- } ,
81
- setup ( params ) {
82
- this . _flags . comparator = params . comparator
83
- this . _flags . startIndex = params . startIndex || 0
84
- } ,
85
- validate ( params , value , state , options ) {
86
- // No-op just to enable description
50
+ for ( let idx = 1 ; idx < records . length ; idx += 1 ) {
51
+ const diff = records [ idx ] - records [ idx - 1 ]
52
+
53
+ if ( diff !== 1 ) {
54
+ const obj = find ( value , i => i . idx === records [ idx ] )
55
+ const context = {
56
+ comparator,
57
+ value : obj ,
58
+ expectedValue : records [ idx - 1 ] + 1
59
+ }
60
+
61
+ return this . createError (
62
+ 'array.continuous_broken' ,
63
+ context ,
64
+ localState ,
65
+ options
66
+ )
67
+ }
68
+ }
69
+
87
70
return value
88
71
}
89
72
}
0 commit comments