From 8472ca2b0134cf3830821be4ec8780ad30bb680f Mon Sep 17 00:00:00 2001 From: Daniel Jonathan Date: Mon, 22 Apr 2024 19:22:42 -0600 Subject: [PATCH] fixed broken validation check for non-required attribiutes --- __tests__/Entity.spec.ts | 72 ++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- src/Entity.ts | 17 +++++++--- 3 files changed, 85 insertions(+), 6 deletions(-) diff --git a/__tests__/Entity.spec.ts b/__tests__/Entity.spec.ts index 2dcb1a4..067ea5d 100644 --- a/__tests__/Entity.spec.ts +++ b/__tests__/Entity.spec.ts @@ -48,6 +48,7 @@ type User = Entity & { readonly id: string readonly createdAt: Date name: string + empty?: string } const createUser = defineEntity({ @@ -66,6 +67,11 @@ const createUser = defineEntity({ required: true, validator: value => 2 < value.length, }, + + empty: { + required: false, + validator: value => guard(value), + }, }, }) @@ -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() diff --git a/package.json b/package.json index ac0f2b5..e4fd485 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/Entity.ts b/src/Entity.ts index 41dd9e0..29c563d 100644 --- a/src/Entity.ts +++ b/src/Entity.ts @@ -112,12 +112,19 @@ function createEntity(target: E, handler: EntityLifecycle = 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`) + } } }