ynn-http-assert
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "ynn-http-assert", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A more powerful http assertion library.", | ||
@@ -48,11 +48,11 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"eslint": "^5.12.0", | ||
"jest": "^23.6.0", | ||
"eslint": "^5.16.0", | ||
"jest": "^24.7.1", | ||
"pre-commit": "^1.2.2" | ||
}, | ||
"dependencies": { | ||
"@lvchengbin/is": "0.0.25", | ||
"@lvchengbin/is": "0.0.27", | ||
"deep-equal": "^1.0.1", | ||
"http-errors": "^1.7.1" | ||
"http-errors": "^1.7.2" | ||
} | ||
} |
@@ -46,2 +46,11 @@ const deepEqual = require( 'deep-equal' ); | ||
undefined() { | ||
this.acceptUndefined = true; | ||
return this; | ||
} | ||
required( name, code = 400, message ) { | ||
return this.assert( this.value(), code, message || `${name} is required` ); | ||
} | ||
value( v ) { | ||
@@ -72,2 +81,7 @@ /** | ||
* - url | ||
* - @todo date | ||
* - @todo time | ||
* - @todo dateTime | ||
* - @todo timestamp | ||
* - @todo mixedTime | ||
*/ | ||
@@ -149,3 +163,3 @@ is( type, ...args ) { | ||
json( ...args ) { | ||
jsonstr( ...args ) { | ||
let res = false; | ||
@@ -166,2 +180,29 @@ let fn; | ||
json( ...args ) { | ||
const value = this.value(); | ||
let res = is.plainObject( value ) || is.array( value ); | ||
if( !res ) { | ||
try { | ||
JSON.parse( value ); | ||
res = true; | ||
} catch( e ) { | ||
res = false; | ||
} | ||
} | ||
return this.assert( res, ...args ); | ||
} | ||
object( ...args ) { | ||
const type = [ '*', '{}', '[]' ].indexOf( args[ 0 ] ) > -1 ? args[ 0 ] : '*'; | ||
const value = this.value(); | ||
if( type === '[]' ) { | ||
return this.assert( is.array( value ), ...args ); | ||
} | ||
if( type === '{}' ) { | ||
return this.assert( value && is.object( value ) && !is.array( value ), ...args ); | ||
} | ||
return this.assert( value && ( is.object( value ) || is.array( value ) ), ...args ); | ||
} | ||
custom( fn, ...args ) { | ||
@@ -183,2 +224,11 @@ const res = fn.call( this, this.value(), this ); | ||
mutate( value ) { | ||
if( is.function( value ) ) { | ||
this.value( value.call( this, this.value() ) ); | ||
return this; | ||
} | ||
this.value( value ); | ||
return this; | ||
} | ||
assert( value, ...args ) { | ||
@@ -190,5 +240,5 @@ if( this.skip || value ) return this; | ||
return this; | ||
} else { | ||
this.throw( ...args ); | ||
} | ||
if( this.acceptUndefined && is.undefined( this.value() ) ) return this; | ||
this.throw( ...args ); | ||
} | ||
@@ -195,0 +245,0 @@ |
67323
666
+ Added@lvchengbin/is@0.0.27(transitive)
- Removed@lvchengbin/is@0.0.25(transitive)
Updated@lvchengbin/is@0.0.27
Updatedhttp-errors@^1.7.2