openapi-police
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -7,5 +7,4 @@ import { SchemaObject, SchemaObjectOptions } from "./schema-object"; | ||
parseStyle(data: string, type: string): any; | ||
coerceToType(data: string, type: string): any; | ||
validate(data: any, opts?: SchemaObjectOptions, path?: string): Promise<any>; | ||
protected typeValidator(data: any, spec: any, path: string, opts: SchemaObjectOptions): any; | ||
} |
@@ -191,22 +191,2 @@ "use strict"; | ||
} | ||
coerceToType(data, type) { | ||
let out = data; | ||
if (typeof out === 'string') { | ||
if (type === 'boolean') { | ||
if (out === 'true' || out === '1') { | ||
out = true; | ||
} | ||
else if (out === 'false' || out === '0') { | ||
out = false; | ||
} | ||
} | ||
else if (type === 'number' || type === 'integer') { | ||
out = parseFloat(out); | ||
} | ||
} | ||
else if (out === undefined && type === 'null') { | ||
out = null; | ||
} | ||
return out; | ||
} | ||
async validate(data, opts = {}, path = '') { | ||
@@ -229,2 +209,5 @@ if (this.parameter.content) { | ||
else { | ||
if (typeof opts.coerceTypes === 'undefined') { | ||
opts.coerceTypes = true; | ||
} | ||
if (typeof data === 'string') { | ||
@@ -235,3 +218,5 @@ if (opts.parseStyle !== false) { | ||
} | ||
data = this.coerceToType(data, spec.type); | ||
if (opts.coerceTypes !== false) { | ||
data = this.coerceToType(data, spec.type); | ||
} | ||
} | ||
@@ -238,0 +223,0 @@ if (typeof data === 'undefined') { |
import { Schema, ValidationOptions } from 'jsonpolice'; | ||
import { OpenAPIV3 } from './types'; | ||
export interface SchemaObjectOptions extends ValidationOptions { | ||
coerceTypes?: boolean; | ||
parseStyle?: boolean; | ||
@@ -12,2 +13,3 @@ contentType?: string; | ||
protected readonly validators: Set<string>; | ||
protected coerceToType(data: string, type: string): any; | ||
protected typeValidator(data: any, spec: any, path: string, opts: SchemaObjectOptions): any; | ||
@@ -14,0 +16,0 @@ protected formatValidator(data: any, spec: any, path: string, opts: SchemaObjectOptions): any; |
@@ -49,2 +49,22 @@ "use strict"; | ||
} | ||
coerceToType(data, type) { | ||
let out = data; | ||
if (typeof out === 'string') { | ||
if (type === 'boolean') { | ||
if (out === 'true' || out === '1') { | ||
out = true; | ||
} | ||
else if (out === 'false' || out === '0') { | ||
out = false; | ||
} | ||
} | ||
else if (type === 'number' || type === 'integer') { | ||
out = parseFloat(out); | ||
} | ||
} | ||
else if (out === undefined && type === 'null') { | ||
out = null; | ||
} | ||
return out; | ||
} | ||
typeValidator(data, spec, path, opts) { | ||
@@ -58,2 +78,7 @@ if (typeof spec.type !== 'string') { | ||
else { | ||
if (typeof data === 'string' && spec.type !== 'string') { | ||
if (opts.coerceTypes === true) { | ||
data = this.coerceToType(data, spec.type); | ||
} | ||
} | ||
return super.typeValidator(data, spec, path, opts); | ||
@@ -60,0 +85,0 @@ } |
{ | ||
"name": "openapi-police", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "OpenAPI v3 validators and utilities", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29780
775