Skip to content

Commit

Permalink
fixed broken validation check for non-required attribiutes
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jonathan committed Apr 23, 2024
1 parent 0603cf6 commit 8472ca2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
72 changes: 72 additions & 0 deletions __tests__/Entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type User = Entity & {
readonly id: string
readonly createdAt: Date
name: string
empty?: string
}

const createUser = defineEntity<User>({
Expand All @@ -66,6 +67,11 @@ const createUser = defineEntity<User>({
required: true,
validator: value => 2 < value.length,
},

empty: {
required: false,
validator: value => guard(value),
},
},
})

Expand Down Expand Up @@ -118,6 +124,72 @@ describe('Entity', () => {
expect(name).not.toBe(e1.name)
})

it('partial validator 2', () => {
const id = '123'
const createdAt = new Date()
const name = 'daniel'
const empty = 'yay'

const e1 = createUser({
id,
createdAt,
name: 'jonathan',
empty,
})

try {
e1.name = ''
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof ValueError) {
expect(error.name).toBe('ValueError')
expect(error.message).toBe('name is invalid')
}
else {
expect(true).toBeFalsy()
}
}

expect(e1.id).toBe(id)
expect(e1.createdAt).toBe(createdAt)
expect(name).not.toBe(e1.name)
expect(empty).toBe(e1.empty)
})

it('partial validator 3', () => {
const id = '123'
const createdAt = new Date()
const name = 'daniel'
const empty = undefined

const e1 = createUser({
id,
createdAt,
name: 'jonathan',
empty,
})

try {
e1.name = ''
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof ValueError) {
expect(error.name).toBe('ValueError')
expect(error.message).toBe('name is invalid')
}
else {
expect(true).toBeFalsy()
}
}

expect(e1.id).toBe(id)
expect(e1.createdAt).toBe(createdAt)
expect(name).not.toBe(e1.name)
expect(empty).toBe(undefined)
})

it('EntityLifecycle', () => {
const id = '123'
const createdAt = new Date()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cosmicmind/domainjs",
"version": "0.0.1-rc-022224-4",
"version": "0.0.1-rc-022224-4-a-a",
"description": "A domain-driven design framework for scalable systems.",
"keywords": [],
"author": {
Expand Down
17 changes: 12 additions & 5 deletions src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,19 @@ function createEntity<E extends Entity>(target: E, handler: EntityLifecycle<E> =

for (const key in attributes) {
const property = attributes[key] as unknown
if (guard(property, 'required') && property.required && !(key in target)) {
throw new ValueError(`${JSON.stringify(target)} ${key} is required`)
if (guard(property, 'required') && property.required) {
if (!(key in target)) {
throw new ValueError(`${JSON.stringify(target)} ${key} is required`)
}

if (guard(property, 'validator') && false === property.validator?.(target[key], entity)) {
throw new ValueError(`${JSON.stringify(target)} ${key} is invalid`)
}
}

if (guard(property, 'validator') && false === property.validator?.(target[key], entity)) {
throw new ValueError(`${JSON.stringify(target)} ${key} is invalid`)
else if (key in target && 'undefined' !== typeof target[key]) {
if (guard(property, 'validator') && false === property.validator?.(target[key], entity)) {
throw new ValueError(`${JSON.stringify(target)} ${key} is invalid`)
}
}
}

Expand Down

0 comments on commit 8472ca2

Please sign in to comment.