Skip to content

Commit fde6a53

Browse files
committed
[src,spec] #4: .object check will not pass an array
1 parent 7e7aa95 commit fde6a53

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

src/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class IsObject extends OfType {
216216
static typeName = 'object'
217217

218218
static checkIt(it:any): boolean {
219-
return typeof it === 'object' && it !== null
219+
return typeof it === 'object' && it !== null && Array.isArray(it) === false;
220220
}
221221
}
222222

@@ -416,20 +416,24 @@ class ObjectOf extends UniqRule {
416416
}
417417
}
418418

419-
type ShapeType = {[key:string]: Checkable}
419+
type ShapeType = {[key:string]: Checkable}|Array<Checkable>;
420420

421421
class Shape extends UniqRule {
422422
static ruleName = 'shape'
423423

424-
static config(shape: ShapeType): Object {
424+
static config(shape: ShapeType): {shape:ShapeType} {
425425
return {shape}
426426
}
427427

428-
static create(checks:Check[], params:object): Check[] {
428+
static create(checks:Check[], params:{shape: ShapeType}): Check[] {
429+
const shapeRule = Array.isArray(params.shape)
430+
? IsArray
431+
: IsObject;
432+
429433
return [
430-
...IsObject.create(
434+
...shapeRule.create(
431435
filterByRuleName(checks, this.ruleName),
432-
IsObject.config(true),
436+
shapeRule.config(true),
433437
),
434438
{
435439
rule: this.ruleName,
@@ -470,7 +474,7 @@ function shiftPath(key:string|number, {path, ...rest}: Issue): Issue {
470474
class Exact extends Shape {
471475
static ruleName = 'shape'
472476

473-
static config(shape: ShapeType): Object {
477+
static config(shape: ShapeType): {shape: ShapeType} {
474478
return {shape}
475479
}
476480

test/types.spec.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ describe('TypedProps', function() {
180180
should(report[0].rule).be.equal('type')
181181
should(report[0].details.type).be.equal('object')
182182
})
183+
184+
it('Should not pass not an array', function() {
185+
const value = []
186+
187+
const type = Type.object
188+
189+
const report = check(value, type)
190+
191+
should(report).has.lengthOf(1)
192+
should(report[0].rule).be.equal('type')
193+
should(report[0].details.type).be.equal('object')
194+
})
183195
})
184196

185197
describe('.array', function() {
@@ -203,7 +215,7 @@ describe('TypedProps', function() {
203215
should(report).has.lengthOf(0)
204216
})
205217

206-
it('Should not pass not array', function() {
218+
it('Should not pass not an array', function() {
207219
const value = null
208220

209221
const type = Type.array
@@ -214,6 +226,18 @@ describe('TypedProps', function() {
214226
should(report[0].rule).be.equal('type')
215227
should(report[0].details.type).be.equal('array')
216228
})
229+
230+
it('Should not pass an object', function() {
231+
const value = {}
232+
233+
const type = Type.array
234+
235+
const report = check(value, type)
236+
237+
should(report).has.lengthOf(1)
238+
should(report[0].rule).be.equal('type')
239+
should(report[0].details.type).be.equal('array')
240+
})
217241
})
218242

219243
describe('.func', function() {
@@ -572,6 +596,21 @@ describe('TypedProps', function() {
572596
should(report[0].details.type).be.equal('object')
573597
})
574598

599+
it('Should not treat an array as an object', function() {
600+
const value = [0, 1];
601+
602+
const type = Type.shape({
603+
0: Type.number,
604+
1: Type.string,
605+
})
606+
607+
const report = check(value, type)
608+
609+
should(report).has.lengthOf(1)
610+
should(report[0].rule).be.equal('type')
611+
should(report[0].details.type).be.equal('object')
612+
})
613+
575614
it('Should check an array', function() {
576615
const value = [null, null]
577616

@@ -590,6 +629,21 @@ describe('TypedProps', function() {
590629
should(report[1].details.type).be.equal('string')
591630
})
592631

632+
it('Should not treat an object as an array', function() {
633+
const value = {0: 0, 1: '1'}
634+
635+
const type = Type.shape([
636+
Type.number,
637+
Type.string,
638+
])
639+
640+
const report = check(value, type)
641+
642+
should(report).has.lengthOf(1)
643+
should(report[0].rule).be.equal('type')
644+
should(report[0].details.type).be.equal('array')
645+
})
646+
593647
it('Should not pass incorrect', function() {
594648
const value = {
595649
one: 1,

0 commit comments

Comments
 (0)