55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
import Joi from "joi";
|
|
|
|
import { DomainError, Result, RuleValidator, handleDomainError } from "..";
|
|
import { UndefinedOr } from "../../../../utilities";
|
|
import {
|
|
IStringValueObjectOptions,
|
|
StringValueObject,
|
|
} from "./StringValueObject";
|
|
|
|
export class Note extends StringValueObject {
|
|
protected static readonly MIN_LENGTH = 0;
|
|
protected static readonly MAX_LENGTH = 255;
|
|
|
|
protected static validate(
|
|
value: UndefinedOr<string>,
|
|
options: IStringValueObjectOptions
|
|
) {
|
|
const rule = Joi.string()
|
|
.allow(null)
|
|
.allow("")
|
|
.default("")
|
|
.trim()
|
|
.min(Note.MIN_LENGTH)
|
|
.max(Note.MAX_LENGTH)
|
|
.label(options.label ? options.label : "value");
|
|
|
|
return RuleValidator.validate<string>(rule, value);
|
|
}
|
|
|
|
public static create(
|
|
value: UndefinedOr<string>,
|
|
options: IStringValueObjectOptions = {}
|
|
) {
|
|
const _options = {
|
|
label: "note",
|
|
...options,
|
|
};
|
|
|
|
const validationResult = Note.validate(value, _options);
|
|
|
|
if (validationResult.isFailure) {
|
|
return Result.fail(
|
|
handleDomainError(
|
|
DomainError.INVALID_INPUT_DATA,
|
|
validationResult.error.message,
|
|
_options
|
|
)
|
|
);
|
|
}
|
|
return Result.ok(new Note(validationResult.object));
|
|
}
|
|
}
|
|
|
|
export class Note_ValidationError extends Joi.ValidationError {}
|