28 lines
719 B
TypeScript
28 lines
719 B
TypeScript
import { ValueObject } from '../../../../shared/domain/ValueObject';
|
|
import { Result, Ok, Err } from '../../../../shared/domain/Result';
|
|
|
|
interface UrlProps {
|
|
value: string;
|
|
}
|
|
|
|
export class Url extends ValueObject<UrlProps> {
|
|
private constructor(props: UrlProps) {
|
|
super(props);
|
|
}
|
|
|
|
static create(raw: string): Result<Url, string> {
|
|
if (!raw || raw.trim().length === 0) {
|
|
return Err('URL must not be empty');
|
|
}
|
|
const trimmed = raw.trim();
|
|
if (!trimmed.startsWith('http://') && !trimmed.startsWith('https://')) {
|
|
return Err('URL must start with http:// or https://');
|
|
}
|
|
return Ok(new Url({ value: trimmed }));
|
|
}
|
|
|
|
toString(): string {
|
|
return this.props.value;
|
|
}
|
|
}
|