Skip to content

Commit

Permalink
refactor: throw error when vine.object is constructed without properties
Browse files Browse the repository at this point in the history
Closes: #17
  • Loading branch information
thetutlage committed Nov 30, 2024
1 parent 715e761 commit 8a2186b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/schema/object/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ export class VineObject<
}

constructor(properties: Properties, options?: FieldOptions, validations?: Validation<any>[]) {
if (!properties) {
throw new Error(
'Missing properties for "vine.object". Use an empty object if you do not want to validate any specific fields'
)
}
super(options, validations)
this.#properties = properties
}
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/schema/object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import { test } from '@japa/runner'
import vine from '../../../index.js'

test.group('VineObject | flat object', () => {
test('fail to construct schema when object is instantiated without object', async ({
assert,
}) => {
assert.throws(
// @ts-expect-error
() => vine.object(),
'Missing properties for "vine.object". Use an empty object if you do not want to validate any specific fields'
)
})

test('fail when value is not an object', async ({ assert }) => {
const schema = vine.object({
username: vine.string(),
Expand Down

0 comments on commit 8a2186b

Please sign in to comment.