New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

tdv

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tdv

Typescript (definition|decorator) validator base on Joi

latest
Source
npmnpm
Version
0.4.1
Version published
Maintainers
1
Created
Source

tdv

Typescript (definition|decorator) validator base on Joi

License NPM Build Status Coverage Status Greenkeeper badge

Example

tsconfig.json

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
    },
}
import { Schema, required, optional } from 'tdv'

class User extends Schema {
    @required
    id: number

    @required(Joi => Joi.string().email())
    email: string

    @required
    profile: Profile
}

class Profile extends Schema {
    @optional
    displayName?: string
}

const user = new User({ id: 123, email: 'foo@example.com', profile: { displayName: 'Joe' } })

console.log(user.validate())
{ error: null,
    value: { id: 123, email: 'foo@example.com', profile: { displayName: 'Joe' } },
    then: [Function: then],
    catch: [Function: catch] }

console.log(user.attempt())
{ id: 123, email: 'foo@example.com', profile: { displayName: 'Joe' } }

console.log(user.toJSON())
{ id: 123, email: 'foo@example.com', profile: { displayName: 'Joe' } }

const user2 = new User({})

console.log(user2.validate())
{ error:
        { ValidationError: child "id" fails because ["id" is required]
        at error stack...
        isJoi: true,
        name: 'ValidationError',
        details: [ [Object] ],
        _object: { id: undefined, profile: undefined },
        annotate: [Function] },
    value: { id: undefined, profile: undefined },
    then: [Function: then],
    catch: [Function: catch] }

console.log(user.attempt())
{ ValidationError: {
    "id" [1]: -- missing --
    }

    [1] "id" is required
        at error stack...
    isJoi: true,
    name: 'ValidationError',
    details:
        [ { message: '"id" is required',
            path: [Array],
            type: 'any.required',
            context: [Object] } ],
    _object: { id: undefined, profile: undefined },
    annotate: [Function] }

console.log(user.toJSON())
{ id: undefined, profile: undefined }

FAQs

Package last updated on 05 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts