Created
February 8, 2020 19:33
-
-
Save arianacosta/929ee0a5bae1d10064a0b170e4f1cf62 to your computer and use it in GitHub Desktop.
Similar to UserDto, but with other properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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