blob: 5650cd43d5c05bfca5aa0163f3ef9fe4a4333bcc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
/*
* Copyright (c) 2016 Network New Technologies Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.serialization.JsonMapperFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
/**
* Created by steve on 22/10/16.
*/
public class BaseJsonSchemaValidatorTest {
private static final ObjectMapper mapper = JsonMapperFactory.getInstance();
public static JsonNode getJsonNodeFromClasspath(String name) throws IOException {
InputStream is1 = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(name);
return mapper.readTree(is1);
}
public static JsonNode getJsonNodeFromStringContent(String content) throws IOException {
return mapper.readTree(content);
}
public static JsonNode getJsonNodeFromUrl(String url) throws IOException {
return mapper.readTree(new URL(url));
}
public static JsonSchema getJsonSchemaFromClasspath(String name) {
return getJsonSchemaFromClasspath(name, SpecVersion.VersionFlag.V4, null);
}
public static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion) {
return getJsonSchemaFromClasspath(name, schemaVersion, null);
}
public static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion, SchemaValidatorsConfig config) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(schemaVersion);
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(name);
if (config == null) {
return factory.getSchema(is);
}
return factory.getSchema(is, config);
}
public static JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(schemaContent);
}
public static JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(SchemaLocation.of(uri));
}
public static JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(jsonNode);
}
// Automatically detect version for given JsonNode
public static JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNode));
return factory.getSchema(jsonNode);
}
}
|