feat(domain-core): add revision definition to domain core

This commit is contained in:
Karol Sójko
2022-11-18 11:54:42 +01:00
parent edbedc181b
commit c8f3a0ce7b
8 changed files with 114 additions and 2 deletions
@@ -1,6 +1,7 @@
import { ValueObject } from '../Core/ValueObject'
import { Result } from '../Core/Result'
import { UuidProps } from './UuidProps'
import { Validator } from '../Core/Validator'
export class Uuid extends ValueObject<UuidProps> {
get value(): string {
@@ -12,8 +13,9 @@ export class Uuid extends ValueObject<UuidProps> {
}
static create(uuid: string): Result<Uuid> {
if (!!uuid === false || uuid.length === 0) {
return Result.fail<Uuid>('Uuid cannot be empty')
const validUuidOrError = Validator.isValidUuid(uuid)
if (validUuidOrError.isFailed()) {
return Result.fail<Uuid>(validUuidOrError.getError())
} else {
return Result.ok<Uuid>(new Uuid({ value: uuid }))
}
@@ -0,0 +1,32 @@
import { Validator } from './Validator'
describe('Validator', () => {
const validUuids = [
'2221101c-1da9-4d2b-9b32-b8be2a8d1c82',
'c08f2f29-a74b-42b4-aefd-98af9832391c',
'b453fa64-1493-443b-b5bb-bca7b9c696c7',
]
const invalidUuids = [
123,
'[email protected]',
'',
null,
'b453fa64-1493-443b-b5bb-ca7b9c696c7',
'c08f*f29-a74b-42b4-aefd-98af9832391c',
'c08f*f29-a74b-42b4-aefd-98af9832391c',
'../../escaped.sh',
]
it('should validate proper uuids', () => {
for (const validUuid of validUuids) {
expect(Validator.isValidUuid(validUuid)).toBeTruthy()
}
})
it('should not validate invalid uuids', () => {
for (const invalidUuid of invalidUuids) {
expect(Validator.isValidUuid(invalidUuid as string)).toBeFalsy()
}
})
})
@@ -0,0 +1,14 @@
import { Result } from './Result'
export class Validator {
private static readonly UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i
static isValidUuid(value: string): Result<string> {
const matchesUuidRegex = String(value).toLowerCase().match(Validator.UUID_REGEX) !== null
if (matchesUuidRegex) {
return Result.ok()
}
return Result.fail(`Given value is not a valid uuid: ${value}`)
}
}
@@ -0,0 +1,22 @@
import { ValueObject } from '../Core/ValueObject'
import { Result } from '../Core/Result'
import { ContentTypeProps } from './ContentTypeProps'
import { ContentType as ContentTypeValues } from '@standardnotes/common'
export class ContentType extends ValueObject<ContentTypeProps> {
get value(): string | null {
return this.props.value
}
private constructor(props: ContentTypeProps) {
super(props)
}
static create(contentType: string | null): Result<ContentType> {
if (contentType !== null && !Object.values(ContentTypeValues).includes(contentType as ContentTypeValues)) {
return Result.fail<ContentType>(`Value is not a valid content type: ${contentType}`)
} else {
return Result.ok<ContentType>(new ContentType({ value: contentType }))
}
}
}
@@ -0,0 +1,3 @@
export interface ContentTypeProps {
value: string | null
}
@@ -0,0 +1,19 @@
import { Entity } from '../Core/Entity'
import { Result } from '../Core/Result'
import { UniqueEntityId } from '../Core/UniqueEntityId'
import { RevisionProps } from './RevisionProps'
export class Revision extends Entity<RevisionProps> {
get id(): UniqueEntityId {
return this._id
}
private constructor(props: RevisionProps, id?: UniqueEntityId) {
super(props, id)
}
static create(props: RevisionProps, id?: UniqueEntityId): Result<Revision> {
return Result.ok<Revision>(new Revision(props, id))
}
}
@@ -0,0 +1,14 @@
import { Uuid } from '../Common/Uuid'
import { ContentType } from './ContentType'
export interface RevisionProps {
itemUuid: Uuid
content: string | null
contentType: ContentType
itemsKeyId: string | null
encItemKey: string | null
authHash: string | null
creationDate: Date
createdAt: Date
updatedAt: Date
}
+6
View File
@@ -8,7 +8,13 @@ export * from './Core/Entity'
export * from './Core/Id'
export * from './Core/Result'
export * from './Core/UniqueEntityId'
export * from './Core/Validator'
export * from './Core/ValueObject'
export * from './Core/ValueObjectProps'
export * from './Map/MapInterface'
export * from './Revision/ContentType'
export * from './Revision/ContentTypeProps'
export * from './Revision/Revision'
export * from './Revision/RevisionProps'