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