Skip to content

Instantly share code, notes, and snippets.

@arianacosta
Created February 8, 2020 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arianacosta/929ee0a5bae1d10064a0b170e4f1cf62 to your computer and use it in GitHub Desktop.
Save arianacosta/929ee0a5bae1d10064a0b170e4f1cf62 to your computer and use it in GitHub Desktop.
Similar to UserDto, but with other properties
export class GreetingDto {
static from(input: unknown) {
const parsedInput = typeof input === 'string' ? JSON.parse(input) : input;
if(!GreetingDto.isValid(parsedInput)) {
throw Error('Invalid input type');
}
const { greeting } = parsedInput;
return new GreetingDto(greeting);
}
protected static isValid(input: any) : input is GreetingDto {
return (
typeof input.greeting === 'string'
);
}
constructor(
readonly greeting: string,
) { }
serialize() {
return JSON.stringify(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment