Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

typeorm class validator

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
import {
    Contains,
    IsInt,
    Length,
    IsEmail,
    IsFQDN,
    IsDate,
    Min,
    Max,
} from "class-validator"

@Entity()
export class Post {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    @Length(10, 20)
    title: string

    @Column()
    @Contains("hello")
    text: string

    @Column()
    @IsInt()
    @Min(0)
    @Max(10)
    rating: number

    @Column()
    @IsEmail()
    email: string

    @Column()
    @IsFQDN()
    site: string

    @Column()
    @IsDate()
    createDate: Date
}
Comment

typeorm class validation

import { validate } from "class-validator"

let post = new Post()
post.title = "Hello" // should not pass
post.text = "this is a great post about hell world" // should not pass
post.rating = 11 // should not pass
post.email = "google.com" // should not pass
post.site = "googlecom" // should not pass

const errors = await validate(post)
if (errors.length > 0) {
    throw new Error(`Validation failed!`)
} else {
    await dataSource.manager.save(post)
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: typeorm caching queries time limit globally 
Javascript :: crear un texto dinamicamente con javascript 
Javascript :: how to use window.alert in javascript 
Javascript :: automatically adjust color 
Javascript :: how can click div close and open next day jquery 
Javascript :: get output dir from webpack options 
Javascript :: js set array relation 
Javascript :: material ui refresh icon 
Javascript :: node "promise.all" "retry" site:stackoverflow.com 
Javascript :: api dfetch data in reactjs 
Javascript :: Using toLocaleString() to Print JavaScript Number Format with Commas 
Javascript :: Pass Props to a Component Using Short circuit evaluation in react 
Javascript :: javascript on enter keyup select button 
Javascript :: architecture express.js 
Javascript :: js regexp eth wallet 
Javascript :: set value 
Javascript :: react native red Triangle Down 
Javascript :: change color jquery css 
Javascript :: btn click on click file javascript 
Javascript :: get data from mulitple query parameters react 
Javascript :: why in the hell does JavaScript - Date getMonth() return 11 
Javascript :: Pass File and JSON Data Both at Same time in Postman API 
Javascript :: React img element rating 
Javascript :: node middle code for server 
Javascript :: javascripts 
Javascript :: fetchapi snippet 
Javascript :: como retirar um numero de um array js 
Javascript :: khai bao bien js 
Javascript :: alpiee js hide amother button click 
Javascript :: copy Konva Transform object 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =