swagger-object-validator
Advanced tools
Comparing version
@@ -6,2 +6,3 @@ import * as Swagger from 'swagger-schema-official'; | ||
allowAdditionalProperties?: boolean; | ||
allowXNullable?: boolean; | ||
disallowHttp?: boolean; | ||
@@ -8,0 +9,0 @@ disallowHttps?: boolean; |
@@ -6,2 +6,5 @@ /// <reference types="bluebird" /> | ||
import { ITraceStep, IValidationError } from '../result'; | ||
export declare function validateType(test: any, schema: Swagger.Schema, spec: Swagger.Spec, config: IValidatorConfig, trace: Array<ITraceStep>): Promise<Array<IValidationError>>; | ||
export interface ISchemaWithNullable extends Swagger.Schema { | ||
'x-nullable'?: boolean; | ||
} | ||
export declare function validateType(test: any, schema: ISchemaWithNullable, spec: Swagger.Spec, config: IValidatorConfig, trace: Array<ITraceStep>): Promise<Array<IValidationError>>; |
@@ -26,2 +26,5 @@ "use strict"; | ||
} | ||
if (config.allowXNullable === true && typeIs === 'null' && schema['x-nullable'] === true) { | ||
typeShouldBe = 'null'; | ||
} | ||
if (typeIs !== typeShouldBe) { | ||
@@ -28,0 +31,0 @@ pushError_1.pushError({ |
@@ -49,2 +49,4 @@ "use strict"; | ||
break; | ||
case 'null': | ||
break; | ||
case 'array': | ||
@@ -51,0 +53,0 @@ validator = ArrayValidator_1.validateArray; |
@@ -6,2 +6,5 @@ /// <reference types="bluebird" /> | ||
import { ITraceStep, IValidationError } from '../result'; | ||
export declare function validateObject(test: any, schema: Swagger.Schema, spec: Swagger.Spec, config: IValidatorConfig, trace: Array<ITraceStep>): Promise<Array<IValidationError>>; | ||
export interface ISchemaWithNullable extends Swagger.Schema { | ||
'x-nullable'?: boolean; | ||
} | ||
export declare function validateObject(test: any, schema: ISchemaWithNullable, spec: Swagger.Spec, config: IValidatorConfig, trace: Array<ITraceStep>): Promise<Array<IValidationError>>; |
@@ -11,2 +11,5 @@ "use strict"; | ||
} | ||
if (config.allowXNullable === true && test === null && schema['x-nullable'] === true) { | ||
return Promise.resolve([]); | ||
} | ||
if (!test || !(test instanceof Object)) { | ||
@@ -17,2 +20,5 @@ var typeIs = typeof (test); | ||
} | ||
if (test === null) { | ||
typeIs = 'null'; | ||
} | ||
var typeShouldBe = schema.title ? schema.title : 'object'; | ||
@@ -19,0 +25,0 @@ return Promise.resolve([ |
{ | ||
"name": "swagger-object-validator", | ||
"version": "1.1.7", | ||
"version": "1.2.0", | ||
"description": "Validate your objects against a swagger spec and receive in-depth error traces", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -192,2 +192,5 @@ # What? | ||
// allow usage of x-nullable for properties, defaults to disallow | ||
allowXNullable?: boolean; | ||
// HTTP and HTTPS are allowed by default | ||
@@ -260,3 +263,17 @@ disallowHttp?: boolean; | ||
## Allowing `x-nullable` properties | ||
A common extension for Swagger 2 is `x-nullable`, based on | ||
[`nullable` from the OpenAPI 3 spec](https://swagger.io/specification/#schemaNullable). | ||
This allows a property to be returned as `null` instead of the intended type. | ||
By enabling this configuration, the `x-nullable` property is recognized and respected | ||
when validating types. | ||
```TypeScript | ||
let config: IValidatorConfig = { | ||
allowXNullable: true | ||
}; | ||
``` | ||
## Ignoring errors | ||
@@ -263,0 +280,0 @@ You may want to ignore certain errors. Let's assume you need some magic to allow a certain |
62526
1.9%1155
1.58%477
3.7%