Skip to content

Commit 6c975db

Browse files
committed
Add .isObject() and .isArray() (and switch from underscore to lodash to do it)
1 parent b73d4cf commit 6c975db

File tree

11 files changed

+280
-43
lines changed

11 files changed

+280
-43
lines changed

ReadMe.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ Advantages over JSON Schema:
1515
* control over error message depending on which validation fails
1616
* errors relate to the property name, not randomly placed in an arbitrary array
1717

18-
Code Coverage using `c8`:
18+
We aim to test every part of `sound` which means we currently have over 350 tests.
19+
20+
```
21+
# tests 369
22+
# pass 369
23+
```
24+
25+
We also aim to have 100% code coverage of `sound.js` - using `c8` - which is now almost 700 lines:
1926

2027
```
2128
----------|---------|----------|---------|---------|-------------------
@@ -128,6 +135,42 @@ check.val: {
128135
}
129136
```
130137

138+
## Checks ##
139+
140+
* isString()
141+
* isInteger()
142+
* isFloat()
143+
* isBoolean()
144+
* isDate()
145+
* isObject()
146+
* isArray()
147+
* isUrl()
148+
* isDomain()
149+
* isEmailAddress()
150+
* isToken()
151+
* isMatch()
152+
* isEnum()
153+
* toTrim()
154+
* toLowerCase()
155+
* toUpperCase()
156+
* toReplace()
157+
* isEqual()
158+
* isNotEmpty()
159+
* isMinLen()
160+
* isMaxLen()
161+
* isMinVal()
162+
* isMaxVal()
163+
* isGreaterThan()
164+
* isLessThan()
165+
166+
## Coercions ##
167+
168+
* toString()
169+
* toString()
170+
* toInteger()
171+
* toFloat()
172+
* toBoolean()
173+
131174
## Unknown Params ##
132175

133176
Only params that we know about are passed back as `check.val`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"dependencies": {
2323
"isemail": "^3.2.0",
24-
"underscore": "^1.13.4"
24+
"lodash": "^4.17.21"
2525
},
2626
"devDependencies": {
2727
"c8": "^7.11.3",

sound.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// --------------------------------------------------------------------------------------------------------------------
1212

1313
// npm
14-
import * as _ from 'underscore'
14+
import _ from 'lodash'
1515
import isemail from 'isemail'
1616

1717
const valid = {
@@ -34,6 +34,8 @@ const T_IS_INTEGER = 'isInteger'
3434
const T_IS_FLOAT = 'isFloat'
3535
const T_IS_BOOLEAN = 'isBoolean'
3636
const T_IS_DATE = 'isDate'
37+
const T_IS_OBJECT = 'isObject'
38+
const T_IS_ARRAY = 'isArray'
3739

3840
const T_IS_URL = 'isUrl'
3941
const T_IS_DOMAIN = 'isDomain'
@@ -131,6 +133,22 @@ Constraint.prototype.isDate = function(msg) {
131133
return this
132134
}
133135

136+
Constraint.prototype.isObject = function(msg) {
137+
this.rules.push({
138+
type : T_IS_OBJECT,
139+
msg : msg,
140+
})
141+
return this
142+
}
143+
144+
Constraint.prototype.isArray = function(msg) {
145+
this.rules.push({
146+
type : T_IS_ARRAY,
147+
msg : msg,
148+
})
149+
return this
150+
}
151+
134152
Constraint.prototype.isEqual = function(value, msg) {
135153
this.rules.push({
136154
type : T_IS_EQUAL,
@@ -441,6 +459,22 @@ const validateParam = function(name, value, constraint) {
441459
}
442460
}
443461
}
462+
else if ( r.type === T_IS_OBJECT ) {
463+
if ( !_.isPlainObject(value) ) {
464+
return {
465+
ok : false,
466+
err : r.msg || name + ' should be an object',
467+
}
468+
}
469+
}
470+
else if ( r.type === T_IS_ARRAY ) {
471+
if ( !_.isArray(value) ) {
472+
return {
473+
ok : false,
474+
err : r.msg || name + ' should be an array',
475+
}
476+
}
477+
}
444478
else if ( r.type === T_IS_EQUAL ) {
445479
if ( value !== r.value ) {
446480
return {

test/basic.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as _ from 'underscore'
1+
import _ from 'lodash'
22
import test from 'tape'
33
import sound from '../sound.js'
44

@@ -142,7 +142,7 @@ const tests = [
142142
yes : 'yes',
143143
},
144144
test : function(t, err) {
145-
t.ok(_.isObject(err), "is an object")
145+
t.ok(_.isPlainObject(err), "is an object")
146146
t.end()
147147
},
148148
},
@@ -163,7 +163,7 @@ const tests = [
163163
usernameA : '',
164164
},
165165
test : function(t, err) {
166-
t.ok(_.isObject(err), "is an object")
166+
t.ok(_.isPlainObject(err), "is an object")
167167
t.ok( _.isUndefined(err.username1), "username1 string passes")
168168
t.ok( _.isUndefined(err.username2), "username2 string passes")
169169
t.ok(!_.isUndefined(err.username3), "username3 string fails")
@@ -206,7 +206,7 @@ const tests = [
206206
int13 : 8,
207207
},
208208
test : function(t, err) {
209-
t.ok(_.isObject(err), "is an object")
209+
t.ok(_.isPlainObject(err), "is an object")
210210
t.ok( _.isUndefined(err.int1), "integer passes")
211211
t.ok( _.isUndefined(err.int2), "integer passes")
212212
t.ok(!_.isUndefined(err.int3), "integer passes")
@@ -249,7 +249,7 @@ const tests = [
249249
url7 : 'http://localhost.localdomain:8000/',
250250
},
251251
test : function(t, err) {
252-
t.ok(_.isObject(err), "is an object")
252+
t.ok(_.isPlainObject(err), "is an object")
253253
t.ok( _.isUndefined(err.url1), "url1 passes")
254254
t.ok( _.isUndefined(err.url2), "url2 passes")
255255
t.ok( _.isUndefined(err.url3), "url3 passes")
@@ -295,7 +295,7 @@ const tests = [
295295
domain24 : 'status-2345.example.com',
296296
},
297297
test : function(t, err) {
298-
t.ok(_.isObject(err), "is an object")
298+
t.ok(_.isPlainObject(err), "is an object")
299299
t.ok( _.isUndefined(err.domain1), "domain1 passes")
300300
t.ok( _.isUndefined(err.domain2), "domain2 passes")
301301
t.ok( _.isUndefined(err.domain3), "domain3 passes")
@@ -341,7 +341,7 @@ const tests = [
341341
email8 : '[email protected]',
342342
},
343343
test : function(t, err) {
344-
t.ok(_.isObject(err), "is an object")
344+
t.ok(_.isPlainObject(err), "is an object")
345345
t.ok(!_.isUndefined(err.email1), "email1 fails")
346346
t.ok(!_.isUndefined(err.email2), "email2 fails")
347347
t.ok(!_.isUndefined(err.email3), "email3 fails")
@@ -371,7 +371,7 @@ const tests = [
371371
tok7 : '*&%@#&',
372372
},
373373
test : function(t, err) {
374-
t.ok(_.isObject(err), "is an object")
374+
t.ok(_.isPlainObject(err), "is an object")
375375
t.ok( _.isUndefined(err.tok1), "tok1 passes")
376376
t.ok( _.isUndefined(err.tok2), "tok2 passes")
377377
t.ok(!_.isUndefined(err.tok3), "tok3 fails")
@@ -394,7 +394,7 @@ const tests = [
394394
url : 'http://chilts.org/',
395395
},
396396
test : function(t, err) {
397-
t.ok(_.isObject(err), "is an object")
397+
t.ok(_.isPlainObject(err), "is an object")
398398

399399
t.end()
400400
},
@@ -409,7 +409,7 @@ const tests = [
409409
favColour2 : 'purple',
410410
},
411411
test : function(t, err) {
412-
t.ok(_.isObject(err), "is an object")
412+
t.ok(_.isPlainObject(err), "is an object")
413413

414414
t.ok( _.isUndefined(err.username), "username is correct")
415415
t.ok( _.isUndefined(err.favColour1), "red is ok")
@@ -435,7 +435,7 @@ const tests = [
435435
favColour7 : '',
436436
},
437437
test : function(t, err) {
438-
t.ok(_.isObject(err), "is an object")
438+
t.ok(_.isPlainObject(err), "is an object")
439439

440440
t.ok( _.isUndefined(err.username), "username is correct")
441441
t.ok( _.isUndefined(err.favColour1), "'red' is ok")

test/coercions.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as _ from 'underscore'
1+
import _ from 'lodash'
22
import test from 'tape'
33
import sound from '../sound.js'
44

@@ -50,8 +50,8 @@ const tests = [
5050
percentage5 : 'not a number',
5151
},
5252
test : function(t, err, res) {
53-
t.ok(_.isObject(err), "err is an object")
54-
t.ok(_.isObject(res), "res is an object")
53+
t.ok(_.isPlainObject(err), "err is an object")
54+
t.ok(_.isPlainObject(res), "res is an object")
5555

5656
t.ok(!_.isUndefined(err.percentage0), "-1 fails the regex")
5757
t.ok(_.isUndefined(err.percentage1), "0 is ok")
@@ -88,8 +88,8 @@ const tests = [
8888
bool10 : 'invalid',
8989
},
9090
test : function(t, err, res) {
91-
t.ok(_.isObject(err), "err is an object")
92-
t.ok(_.isObject(res), "res is an object")
91+
t.ok(_.isPlainObject(err), "err is an object")
92+
t.ok(_.isPlainObject(res), "res is an object")
9393

9494
t.ok( _.isUndefined(err.bool0), "0 is ok")
9595
t.ok( _.isUndefined(err.bool1), "1 is ok")
@@ -133,8 +133,8 @@ const tests = [
133133
float7 : 'invalid',
134134
},
135135
test : function(t, err, res) {
136-
t.ok(_.isObject(err), "err is an object")
137-
t.ok(_.isObject(res), "res is an object")
136+
t.ok(_.isPlainObject(err), "err is an object")
137+
t.ok(_.isPlainObject(res), "res is an object")
138138

139139
t.ok( _.isUndefined(err.float0), "0 is ok")
140140
t.ok( _.isUndefined(err.float1), "1 is ok")

test/conversions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as _ from 'underscore'
1+
import _ from 'lodash'
22
import test from 'tape'
33
import sound from '../sound.js'
44

@@ -35,8 +35,8 @@ const tests = [
3535
notEmpty : ' ', // gets us past the default '' check
3636
},
3737
test : function(t, err, res) {
38-
t.ok(_.isObject(err), "err is an object")
39-
t.ok(_.isObject(res), "res is an object")
38+
t.ok(_.isPlainObject(err), "err is an object")
39+
t.ok(_.isPlainObject(res), "res is an object")
4040

4141
t.ok(!_.isUndefined(err.username0), "string fails")
4242
t.ok(_.isUndefined(err.username1), "string passes")

0 commit comments

Comments
 (0)