Skip to content

Commit

Permalink
moved attributes to properties and cleaned up error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jonathan committed Apr 23, 2024
1 parent 8472ca2 commit 1488510
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 188 deletions.
4 changes: 2 additions & 2 deletions __tests__/Aggregate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type UserRegisterEvent = Event & {
}

const createUserAggregateRegisterEvent = defineEvent<UserRegisterEvent>({
attributes: {
properties: {
entity: {
validator: (entity: User): boolean => guard<User>(entity),
},
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('Aggregate', () => {
expect(guard<User>(entity)).toBeTruthy()
},

attributes: {
properties: {
id: {
validator(value: string) {
expect(value).toBe(id)
Expand Down
29 changes: 16 additions & 13 deletions __tests__/Entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@
import {
it,
expect,
expectTypeOf,
describe,
} from 'vitest'

import { guard } from '@cosmicmind/foundationjs'
import {
guard,
} from '@cosmicmind/foundationjs'

import {
Entity,
ValueError,
EntityError,
defineEntity,
} from '@/index'

Expand All @@ -52,7 +55,7 @@ type User = Entity & {
}

const createUser = defineEntity<User>({
attributes: {
properties: {
id: {
required: true,
validator: value => 2 < value.length,
Expand Down Expand Up @@ -110,9 +113,9 @@ describe('Entity', () => {
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof ValueError) {
expect(error.name).toBe('ValueError')
expect(error.message).toBe('name is invalid')
if (error instanceof EntityError) {
expect(error.name).toBe('EntityError')
expectTypeOf(error.message).toMatchTypeOf<string>()
}
else {
expect(true).toBeFalsy()
Expand Down Expand Up @@ -142,9 +145,9 @@ describe('Entity', () => {
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof ValueError) {
expect(error.name).toBe('ValueError')
expect(error.message).toBe('name is invalid')
if (error instanceof EntityError) {
expect(error.name).toBe('EntityError')
expectTypeOf(error.message).toMatchTypeOf<string>()
}
else {
expect(true).toBeFalsy()
Expand Down Expand Up @@ -175,9 +178,9 @@ describe('Entity', () => {
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof ValueError) {
expect(error.name).toBe('ValueError')
expect(error.message).toBe('name is invalid')
if (error instanceof EntityError) {
expect(error.name).toBe('EntityError')
expectTypeOf(error.message).toMatchTypeOf<string>()
}
else {
expect(true).toBeFalsy()
Expand All @@ -204,7 +207,7 @@ describe('Entity', () => {
expect(guard(entity))
},

attributes: {
properties: {
id: {
required: true,
validator: (value, entity) => {
Expand Down
208 changes: 152 additions & 56 deletions __tests__/Event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,132 +33,228 @@
import {
it,
expect,
expectTypeOf,
describe,
} from 'vitest'

import { guard } from '@cosmicmind/foundationjs'
import {
guard,
} from '@cosmicmind/foundationjs'

import {
Entity,
Event,
EventError,
defineEvent,
} from '@/index'

type User = Entity & {
type User = Event & {
readonly id: string
readonly createdAt: Date
name: string
empty?: string
}

type UserEvent = Event & {
id: string
createdAt: Date
correlationId: string
entity: User
}

const createUserEvent = defineEvent<UserEvent>({
attributes: {
const createUser = defineEvent<User>({
properties: {
id: {
validator: (value: string): boolean => 2 < value.length,
required: true,
validator: value => 2 < value.length,
},

correlationId: {
validator: (value: string): boolean => 2 < value.length,
createdAt: {
required: true,
validator: value => guard(value),
},

createdAt: {
validator: (value: Date): boolean => guard<Date>(value),
name: {
required: true,
validator: value => 2 < value.length,
},

entity: {
validator: (value: User): boolean => guard<User>(value),
empty: {
required: false,
validator: value => guard(value),
},
},
})

describe('Event', () => {
it('interface', () => {
const id = '123'
const correlationId = '456'
const createdAt = new Date()
const entity = {
const name = 'daniel'

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

e1.name = 'daniel'

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

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

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

try {
e1.name = ''
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof EventError) {
expect(error.name).toBe('EventError')
expectTypeOf(error.message).toMatchTypeOf<string>()
}
else {
expect(true).toBeFalsy()
}
}

const e1 = createUserEvent({
expect(e1.id).toBe(id)
expect(e1.createdAt).toBe(createdAt)
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,
correlationId,
createdAt,
entity,
name: 'jonathan',
empty,
})

try {
e1.name = ''
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof EventError) {
expect(error.name).toBe('EventError')
expectTypeOf(error.message).toMatchTypeOf<string>()
}
else {
expect(true).toBeFalsy()
}
}

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

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

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

try {
e1.name = ''
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof EventError) {
expect(error.name).toBe('EventError')
expectTypeOf(error.message).toMatchTypeOf<string>()
}
else {
expect(true).toBeFalsy()
}
}

const createEvent = defineEvent<UserEvent>({
trace(event: Event) {
expect(guard<Event>(event)).toBeTruthy()
expect(e1.id).toBe(id)
expect(e1.createdAt).toBe(createdAt)
expect(name).not.toBe(e1.name)
expect(empty).toBe(undefined)
})

it('EventLifecycle', () => {
const id = '123'
const createdAt = new Date()
const name = 'daniel'

const createEvent = defineEvent<User>({
trace(event) {
expect(guard(event)).toBeTruthy()
},

created(event: Event) {
expect(guard<Event>(event)).toBeTruthy()
created(event) {
expect(guard(event))
},

attributes: {
properties: {
id: {
validator: (value: string): boolean => {
required: true,
validator: (value, event) => {
expect(value).toBe(id)
return 2 < value.length
},
},

correlationId: {
validator: (value: string): boolean => {
expect(value).toBe(correlationId)
expect(event.id).toBe(id)
return 2 < value.length
},
},

createdAt: {
validator: (value: Date): boolean => {
required: true,
validator: (value, event) => {
expect(value).toBe(createdAt)
return guard<Date>(value)
expect(event.createdAt).toBe(createdAt)
return guard(value)
},
},

entity: {
validator: (value: User): boolean => {
expect(guard<User>(value)).toBeTruthy()
return guard<User>(value)
name: {
required: true,
validator: (value, event) => {
expect(2 < value.length).toBeTruthy()
expect(2 < event.name.length).toBeTruthy()
return 2 < value.length
},
updated: (newValue, oldValue, event) => {
expect(newValue).toBe('jonathan')
expect(oldValue).toBe(name)
expect(event.id).toBe(id)
expect(event.createdAt).toBe(createdAt)
expect(event.name).toBe(name)
},
},
},
})

const e1 = createEvent({
id,
correlationId,
createdAt,
entity,
name,
})

expect(e1.id).toBe(id)
expect(e1.correlationId).toBe(correlationId)
expect(e1.createdAt).toBe(createdAt)
expect(e1.entity).toStrictEqual(entity)

e1.name = 'jonathan'

expect('jonathan').toBe(e1.name)
})
})
})
Loading

0 comments on commit 1488510

Please sign in to comment.