1 /* 2 * Copyright (c) 2016 Network New Technologies Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.networknt.schema; 18 19 import java.util.Optional; 20 import java.util.concurrent.ConcurrentHashMap; 21 import java.util.concurrent.ConcurrentMap; 22 23 import com.fasterxml.jackson.databind.JsonNode; 24 import com.networknt.schema.SpecVersion.VersionFlag; 25 26 public class ValidationContext { 27 private final JsonMetaSchema metaSchema; 28 private final JsonSchemaFactory jsonSchemaFactory; 29 private final SchemaValidatorsConfig config; 30 private final ConcurrentMap<String, JsonSchema> schemaReferences; 31 private final ConcurrentMap<String, JsonSchema> schemaResources; 32 private final ConcurrentMap<String, JsonSchema> dynamicAnchors; 33 ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory, SchemaValidatorsConfig config)34 public ValidationContext(JsonMetaSchema metaSchema, 35 JsonSchemaFactory jsonSchemaFactory, SchemaValidatorsConfig config) { 36 this(metaSchema, jsonSchemaFactory, config, new ConcurrentHashMap<>(), new ConcurrentHashMap<>(), new ConcurrentHashMap<>()); 37 } 38 ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory, SchemaValidatorsConfig config, ConcurrentMap<String, JsonSchema> schemaReferences, ConcurrentMap<String, JsonSchema> schemaResources, ConcurrentMap<String, JsonSchema> dynamicAnchors)39 public ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory, 40 SchemaValidatorsConfig config, ConcurrentMap<String, JsonSchema> schemaReferences, 41 ConcurrentMap<String, JsonSchema> schemaResources, ConcurrentMap<String, JsonSchema> dynamicAnchors) { 42 if (metaSchema == null) { 43 throw new IllegalArgumentException("JsonMetaSchema must not be null"); 44 } 45 if (jsonSchemaFactory == null) { 46 throw new IllegalArgumentException("JsonSchemaFactory must not be null"); 47 } 48 this.metaSchema = metaSchema; 49 this.jsonSchemaFactory = jsonSchemaFactory; 50 this.config = config == null ? new SchemaValidatorsConfig() : config; 51 this.schemaReferences = schemaReferences; 52 this.schemaResources = schemaResources; 53 this.dynamicAnchors = dynamicAnchors; 54 } 55 newSchema(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema)56 public JsonSchema newSchema(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema) { 57 return getJsonSchemaFactory().create(this, schemaLocation, evaluationPath, schemaNode, parentSchema); 58 } 59 newValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, String keyword , JsonNode schemaNode, JsonSchema parentSchema)60 public JsonValidator newValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, 61 String keyword /* keyword */, JsonNode schemaNode, JsonSchema parentSchema) { 62 return this.metaSchema.newValidator(this, schemaLocation, evaluationPath, keyword, schemaNode, parentSchema); 63 } 64 resolveSchemaId(JsonNode schemaNode)65 public String resolveSchemaId(JsonNode schemaNode) { 66 return this.metaSchema.readId(schemaNode); 67 } 68 getJsonSchemaFactory()69 public JsonSchemaFactory getJsonSchemaFactory() { 70 return this.jsonSchemaFactory; 71 } 72 getConfig()73 public SchemaValidatorsConfig getConfig() { 74 return this.config; 75 } 76 77 /** 78 * Gets the schema references identified by the ref uri. 79 * 80 * @return the schema references 81 */ getSchemaReferences()82 public ConcurrentMap<String, JsonSchema> getSchemaReferences() { 83 return this.schemaReferences; 84 } 85 86 /** 87 * Gets the schema resources identified by id. 88 * 89 * @return the schema resources 90 */ getSchemaResources()91 public ConcurrentMap<String, JsonSchema> getSchemaResources() { 92 return this.schemaResources; 93 } 94 95 /** 96 * Gets the dynamic anchors. 97 * 98 * @return the dynamic anchors 99 */ getDynamicAnchors()100 public ConcurrentMap<String, JsonSchema> getDynamicAnchors() { 101 return this.dynamicAnchors; 102 } 103 getMetaSchema()104 public JsonMetaSchema getMetaSchema() { 105 return this.metaSchema; 106 } 107 activeDialect()108 public Optional<VersionFlag> activeDialect() { 109 String metaSchema = getMetaSchema().getIri(); 110 return SpecVersionDetector.detectOptionalVersion(metaSchema); 111 } 112 } 113