Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ function property(data: any, key: keyof any, schema: Schema, options: Schemaster

Schema.extend('array', (data, { inner, meta }, options) => {
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options)
checkWithinRange(data.length, meta, 'array length', options, !isNullable(inner!.meta.default))
checkWithinRange(data.length, meta, 'array length', options)
return [data.map((_, index) => property(data, index, inner!, options))]
})

Expand Down
11 changes: 11 additions & 0 deletions packages/core/tests/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ describe('Array', () => {
expect(() => new Config(['dress'])).to.throw()
expect(() => new Config(['dress', 'skirt', 'socks', 'swimming suit'])).to.throw()
})

test('array (length) with a defaulted inner', () => {
// the inner schema's default used to disable the length check entirely
const Config = Schema.array(Schema.string().default('x')).min(2)

expect(() => new Config([])).to.throw()
expect(() => new Config(['dress'])).to.throw()

expect(new Config(['dress', 'skirt'])).to.deep.equal(['dress', 'skirt'])
expect(new Config(['dress', null])).to.deep.equal(['dress', 'x'])
})
})