Skip to content

Commit

Permalink
added var params to aggregates and value models
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jonathan committed Jan 2, 2024
1 parent f9a0bad commit 8b1f941
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 81 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ updating entities, for example:
export const createUser = defineEntity<User>({
attributes: {
id: {
validate(value): boolean | never {
validator(value): boolean | never {
// id validation logic
// return true | false
// or throw an error
},
},

name: {
validate(value): boolean | never {
validator(value): boolean | never {
// name validation logic
// return true | false
// or throw an error
},
},

age: {
validate(value): boolean | never {
validator(value): boolean | never {
// age validation logic
// return true | false
// or throw an error
Expand Down Expand Up @@ -133,7 +133,7 @@ export const createUser = defineEntity<User>({
// ...

age: {
validate(value): boolean | never {
validator(value): boolean | never {
// ... do something
},

Expand Down Expand Up @@ -190,7 +190,7 @@ export const createEmail = defineValue(Email, {
// ... do something
},

validate(value): boolean | never {
validator(value): boolean | never {
// email validation logic
// return true | false
// or throw an error
Expand Down Expand Up @@ -240,23 +240,23 @@ export type User = Entity & {
export const createUser = defineEntity<User>({
attributes: {
id: {
validate(value): boolean | never {
validator(value): boolean | never {
// id validation logic
// return true | false
// or throw an error
},
},

name: {
validate(value): boolean | never {
validator(value): boolean | never {
// name validation logic
// return true | false
// or throw an error
},
},

age: {
validate(value): boolean | never {
validator(value): boolean | never {
// age validation logic
// return true | false
// or throw an error
Expand Down Expand Up @@ -371,7 +371,7 @@ export const createUserAggregate = defineAggregate(UserAggregate, {
// ...

age: {
validate(value): boolean | never {
validator(value): boolean | never {
// ... do something
},

Expand Down Expand Up @@ -453,7 +453,7 @@ export type RegisterAccountEvent = Event & {
export const createRegisterAccountEvent = defineEvent<RegisterAccountEvent>({
attributes: {
id: {
validate(value): boolean | never {
validator(value): boolean | never {
// id validation logic
// return true | false
// or throw an error
Expand Down
14 changes: 7 additions & 7 deletions __tests__/Aggregate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Email extends Value<string> {
}

const createEmail = defineValue(Email, {
validate: (value: string): boolean => 'string' === typeof string().email('email is invalid').strict(true).validateSync(value),
validator: (value: string): boolean => 'string' === typeof string().email('email is invalid').strict(true).validateSync(value),
})

interface User extends Entity {
Expand All @@ -81,7 +81,7 @@ type UserRegisterEvent = Event & {
const createUserAggregateRegisterEvent = defineEvent<UserRegisterEvent>({
attributes: {
entity: {
validate: (entity: User): boolean => guard<User>(entity),
validator: (entity: User): boolean => guard<User>(entity),
},
},
})
Expand Down Expand Up @@ -148,21 +148,21 @@ describe('Aggregate', () => {

attributes: {
id: {
validate(value: string) {
validator(value: string) {
expect(value).toBe(id)
return 2 < value.length
},
},

createdAt: {
validate(value: Date) {
validator(value: Date) {
expect(value).toBe(createdAt)
return guard<Date>(value)
},
},

name: {
validate(value: string) {
validator(value: string) {
expect(2 < value.length).toBeTruthy()
return 2 < value.length
},
Expand All @@ -177,14 +177,14 @@ describe('Aggregate', () => {
},

version: {
validate(value: number) {
validator(value: number) {
expect(0 < value).toBeTruthy()
return 0 < value
},
},

email: {
validate(value: Email, entity: User) {
validator(value: Email, entity: User) {
expect(email).toBe(value.value)
expect(email).toBe(entity.email.value)
return email === value.value
Expand Down
18 changes: 9 additions & 9 deletions __tests__/Entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { guard } from '@cosmicmind/foundationjs'

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

Expand All @@ -53,15 +53,15 @@ type User = Entity & {
const createUser = defineEntity<User>({
attributes: {
id: {
validate: value => 2 < value.length,
validator: value => 2 < value.length,
},

createdAt: {
validate: value => guard(value),
validator: value => guard(value),
},

name: {
validate: value => 2 < value.length,
validator: value => 2 < value.length,
},
},
})
Expand Down Expand Up @@ -101,8 +101,8 @@ describe('Entity', () => {
expect(true).toBeFalsy()
}
catch (error) {
if (error instanceof EntityError) {
expect(error.name).toBe('EntityError')
if (error instanceof ValueError) {
expect(error.name).toBe('ValueError')
expect(error.message).toBe('name is invalid')
}
else {
Expand Down Expand Up @@ -131,23 +131,23 @@ describe('Entity', () => {

attributes: {
id: {
validate: (value, entity) => {
validator: (value, entity) => {
expect(value).toBe(id)
expect(entity.id).toBe(id)
return 2 < value.length
},
},

createdAt: {
validate: (value, entity) => {
validator: (value, entity) => {
expect(value).toBe(createdAt)
expect(entity.createdAt).toBe(createdAt)
return guard(value)
},
},

name: {
validate: (value, entity) => {
validator: (value, entity) => {
expect(2 < value.length).toBeTruthy()
expect(2 < entity.name.length).toBeTruthy()
return 2 < value.length
Expand Down
16 changes: 8 additions & 8 deletions __tests__/Event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ type UserEvent = Event & {
const createUserEvent = defineEvent<UserEvent>({
attributes: {
id: {
validate: (value: string): boolean => 2 < value.length,
validator: (value: string): boolean => 2 < value.length,
},

correlationId: {
validate: (value: string): boolean => 2 < value.length,
validator: (value: string): boolean => 2 < value.length,
},

createdAt: {
validate: (value: Date): boolean => guard<Date>(value),
validator: (value: Date): boolean => guard<Date>(value),
},

entity: {
validate: (value: User): boolean => guard<User>(value),
validator: (value: User): boolean => guard<User>(value),
},
},
})
Expand Down Expand Up @@ -120,28 +120,28 @@ describe('Event', () => {

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

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

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

entity: {
validate: (value: User): boolean => {
validator: (value: User): boolean => {
expect(guard<User>(value)).toBeTruthy()
return guard<User>(value)
},
Expand Down
6 changes: 3 additions & 3 deletions __tests__/Value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ class Email extends Value<string> {
}

const createEmail = defineValue(Email, {
validate: (value: string): boolean =>
validator: (value: string): boolean =>
'string' === typeof string().email('email is invalid').strict(true).validateSync(value),
})

class Version extends Value<number> {}

const createVersionValue = defineValue(Version, {
validate: (value: number): boolean => 0 < value,
validator: (value: number): boolean => 0 < value,
})

describe('Value', () => {
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('Value', () => {
expect(email).toBe(vo.value)
},

validate(value: string, vo: Email): boolean {
validator(value: string, vo: Email): boolean {
expect(email).toBe(value)
expect(email).toBe(vo.value)
return 'string' === typeof string().email('email is invalid').strict(true).validateSync(value)
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-010124-4-d",
"version": "0.0.1-rc-010224-4",
"description": "A domain-driven design framework for scalable systems.",
"keywords": [],
"author": {
Expand Down
12 changes: 7 additions & 5 deletions src/Aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ import {
export abstract class Aggregate<E extends Entity, T extends EventTopics = EventTopics> extends EventObservable<T> {
protected root: E

protected constructor(root: E) {
protected constructor(root: E, ...args: unknown[]) {
super()
this.root = root
this.root = 'function' === typeof this.prepare ? this.prepare(root, ...args) : root
}

protected prepare?(root: E, ...args: unknown[]): E
}

export type AggregateTypeFor<A> = A extends Aggregate<infer E> ? E : A

export type AggregateConstructor<A extends Aggregate<Entity>> = new (root: AggregateTypeFor<A>) => A
export type AggregateConstructor<A extends Aggregate<Entity>> = new (root: AggregateTypeFor<A>, ...args: unknown[]) => A

export function defineAggregate<A extends Aggregate<Entity>>(_class: AggregateConstructor<A>, handler: EntityLifecycle<AggregateTypeFor<A>> = {}): (root: AggregateTypeFor<A>) => A {
export function defineAggregate<A extends Aggregate<Entity>>(_class: AggregateConstructor<A>, handler: EntityLifecycle<AggregateTypeFor<A>> = {}): (root: AggregateTypeFor<A>, ...args: unknown[]) => A {
const createEntity = defineEntity<AggregateTypeFor<A>>(handler)
return (root: AggregateTypeFor<A>): A => new _class(createEntity(root))
return (root: AggregateTypeFor<A>, ...args: unknown[]): A => new _class(createEntity(root), ...args)
}
Loading

0 comments on commit 8b1f941

Please sign in to comment.