Created
November 1, 2017 08:47
-
-
Save JonJam/2410a8ab8b67aed45e3a02b0a8496727 to your computer and use it in GitHub Desktop.
validation-ts: class
This file contains 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
import { IsEmail, IsNotEmpty, IsUrl, MaxLength } from "class-validator"; | |
interface ISite { | |
readonly name: string; | |
readonly url: string; | |
readonly emailAddress: string; | |
readonly password: string; | |
} | |
export default class Site implements ISite { | |
@IsNotEmpty({ | |
message: "Name is required." | |
}) | |
@MaxLength(100, { | |
message: "Name must be shorter than or equal to $constraint1 characters." | |
}) | |
public readonly name: string; | |
@IsNotEmpty({ | |
message: "Url is required." | |
}) | |
@IsUrl(undefined, { | |
message: "Url must be a URL." | |
}) | |
@MaxLength(1000, { | |
message: "Url must be shorter than or equal to $constraint1 characters." | |
}) | |
public readonly url: string; | |
@IsNotEmpty({ | |
message: "Email address is required." | |
}) | |
@IsEmail(undefined, { | |
message: "Email address must be an email address." | |
}) | |
@MaxLength(100, { | |
message: "Email address must be shorter than or equal to $constraint1 characters." | |
}) | |
public readonly emailAddress: string; | |
@IsNotEmpty({ | |
message: "Password is required." | |
}) | |
@MaxLength(100, { | |
message: "Password must be shorter than or equal to $constraint1 characters." | |
}) | |
public readonly password: string; | |
constructor( | |
site: ISite = { | |
name: "", | |
password: "", | |
url: "", | |
emailAddress: "" | |
} | |
) { | |
this.name = site.name; | |
this.url = site.url; | |
this.emailAddress = site.emailAddress; | |
this.password = site.password; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment