@apigames/json-schema
Advanced tools
Comparing version 1.0.13 to 1.0.14
@@ -6,4 +6,4 @@ import { Schema, ValidatorResult } from 'jsonschema'; | ||
setupSchemaDependencies?(registerSchemaFunction: SetupSchemaDependenciesFunction): void; | ||
schemaDefinition(): Schema; | ||
schemaDefinition(context: any): Schema; | ||
postSchemaValidation?(payloadDocument: any, validationResult: ValidatorResult): void; | ||
} |
import { ValidationError, Validator } from 'jsonschema'; | ||
import type { IJsonSchemaDefinition } from './json.schema.definition'; | ||
export default class JsonSchemaValidator { | ||
private _context; | ||
private _validator; | ||
@@ -12,3 +13,3 @@ private _validationErrors; | ||
get validator(): Validator; | ||
validate: (payloadDocument: any, schemaDefinition: IJsonSchemaDefinition) => boolean; | ||
validate: (payloadDocument: any, schemaDefinition: IJsonSchemaDefinition, context?: any) => boolean; | ||
} |
@@ -7,2 +7,3 @@ "use strict"; | ||
constructor() { | ||
this._context = undefined; | ||
this._validator = undefined; | ||
@@ -15,3 +16,3 @@ this._validationErrors = undefined; | ||
} | ||
const _schemaDefinition = schemaDefinition.schemaDefinition(); | ||
const _schemaDefinition = schemaDefinition.schemaDefinition(this._context); | ||
this.validator.addSchema(_schemaDefinition, schemaDefinition.schemaName()); | ||
@@ -32,4 +33,5 @@ }; | ||
}; | ||
this.validate = (payloadDocument, schemaDefinition) => { | ||
this.validate = (payloadDocument, schemaDefinition, context = undefined) => { | ||
this._validationErrors = undefined; | ||
this._context = context; | ||
if (json_1.isUndefined(payloadDocument)) { | ||
@@ -41,3 +43,3 @@ this._validationErrors = [new jsonschema_1.ValidationError('The payload was empty.')]; | ||
this.setupSchema(schemaDefinition); | ||
const _schemaDefinition = schemaDefinition.schemaDefinition(); | ||
const _schemaDefinition = schemaDefinition.schemaDefinition(context); | ||
const validationResult = this.validator.validate(payloadDocument, _schemaDefinition); | ||
@@ -44,0 +46,0 @@ if ((validationResult.valid) && (schemaDefinition.postSchemaValidation)) { |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"main": "lib/index.js", | ||
@@ -9,0 +9,0 @@ "types": "lib/index.d.ts", |
24250
359