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 
Javascript :: typeorm cache all queries 
Javascript :: datatable editable row update other cell 
Javascript :: on change jquery kendo switch 
Javascript :: javascript returns odd 
Javascript :: setImmediate clearImmediate 
Javascript :: js set utils 
Javascript :: how to identify the li anchor tag text is empty in javascript 
Javascript :: javascript node retry promise.all 
Javascript :: react native update performance useReducer 
Javascript :: Using Intl.NumberFormat() to Print JavaScript Number Format with Commas 
Javascript :: how to customize reactnative view dropshadow 
Javascript :: Classes and constructor functions in ES6 
Javascript :: where is the waypoint json file lunar client mac 
Javascript :: populating selectfield from json file 
Javascript :: add value get value 
Javascript :: react native red Oval bubble 
Javascript :: socket io import es6 
Javascript :: How do you convert VARCHAR to TIMESTAMP 
Javascript :: Safe Area View for android / Removing overflow of screen for android 
Javascript :: @click:append 
Javascript :: trigger keyup event jquery 
Javascript :: react Bootstrap Five Heart rating 
Javascript :: make form submit on new window using jquery 
Javascript :: sequelize default curdate 
Javascript :: how to rmeove white space in a string with jquery 
Javascript :: como usar for js 
Javascript :: es6 parameter destructuring nested object 
Javascript :: print array elements in new line javascript 
Javascript :: how to make messaging website with firebase javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =