Skip to content

Commit 944e01a

Browse files
authored
Merge pull request #220 from crate-metadata/JsonSchemaInterface
Changed JsonSchema to an interface
2 parents 5aa7c81 + 41f22fb commit 944e01a

File tree

6 files changed

+166
-90
lines changed

6 files changed

+166
-90
lines changed

src/main/java/com/github/fge/jsonschema/main/JsonSchema.java

Lines changed: 12 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -26,69 +26,15 @@
2626
import com.github.fge.jsonschema.core.report.ListProcessingReport;
2727
import com.github.fge.jsonschema.core.report.MessageProvider;
2828
import com.github.fge.jsonschema.core.report.ProcessingReport;
29-
import com.github.fge.jsonschema.core.report.ReportProvider;
30-
import com.github.fge.jsonschema.core.tree.SchemaTree;
31-
import com.github.fge.jsonschema.core.tree.SimpleJsonTree;
32-
import com.github.fge.jsonschema.processors.data.FullData;
33-
import com.github.fge.jsonschema.processors.validation.ValidationProcessor;
34-
35-
import javax.annotation.concurrent.Immutable;
3629

3730
/**
3831
* Single-schema instance validator
3932
*
40-
* <p>This is the class you will use the most often. It is, in essence, a {@link
33+
* <p>This is the interface you will use the most often. It is, in essence, a {@link
4134
* JsonValidator} initialized with a single JSON Schema. Note however that this
4235
* class still retains the ability to resolve JSON References.</p>
43-
*
44-
* <p>It has no public constructors: you should use the appropriate methods in
45-
* {@link JsonSchemaFactory} to obtain an instance of this class.</p>
4636
*/
47-
@Immutable
48-
public final class JsonSchema
49-
{
50-
private final ValidationProcessor processor;
51-
private final SchemaTree schema;
52-
private final ReportProvider reportProvider;
53-
54-
/**
55-
* Package private constructor
56-
*
57-
* @param processor the validation processor
58-
* @param schema the schema to bind to this instance
59-
* @param reportProvider the report provider
60-
*/
61-
JsonSchema(final ValidationProcessor processor, final SchemaTree schema,
62-
final ReportProvider reportProvider)
63-
{
64-
this.processor = processor;
65-
this.schema = schema;
66-
this.reportProvider = reportProvider;
67-
}
68-
69-
private ProcessingReport doValidate(final JsonNode node,
70-
final boolean deepCheck)
71-
throws ProcessingException
72-
{
73-
final FullData data = new FullData(schema, new SimpleJsonTree(node),
74-
deepCheck);
75-
final ProcessingReport report = reportProvider.newReport();
76-
final ProcessingResult<FullData> result
77-
= ProcessingResult.of(processor, report, data);
78-
return result.getReport();
79-
}
80-
81-
private ProcessingReport doValidateUnchecked(final JsonNode node,
82-
final boolean deepCheck)
83-
{
84-
final FullData data = new FullData(schema, new SimpleJsonTree(node),
85-
deepCheck);
86-
final ProcessingReport report = reportProvider.newReport();
87-
final ProcessingResult<FullData> result
88-
= ProcessingResult.uncheckedResult(processor, report, data);
89-
return result.getReport();
90-
}
91-
37+
public interface JsonSchema {
9238
/**
9339
* Validate an instance and return a processing report
9440
*
@@ -102,12 +48,8 @@ private ProcessingReport doValidateUnchecked(final JsonNode node,
10248
*
10349
* @since 2.1.8
10450
*/
105-
public ProcessingReport validate(final JsonNode instance,
106-
final boolean deepCheck)
107-
throws ProcessingException
108-
{
109-
return doValidate(instance, deepCheck);
110-
}
51+
ProcessingReport validate(JsonNode instance, boolean deepCheck)
52+
throws ProcessingException;
11153

11254
/**
11355
* Validate an instance and return a processing report
@@ -119,11 +61,8 @@ public ProcessingReport validate(final JsonNode instance,
11961
* @return a processing report
12062
* @throws ProcessingException a processing error occurred during validation
12163
*/
122-
public ProcessingReport validate(final JsonNode instance)
123-
throws ProcessingException
124-
{
125-
return validate(instance, false);
126-
}
64+
ProcessingReport validate(JsonNode instance)
65+
throws ProcessingException;
12766

12867
/**
12968
* Validate an instance and return a processing report (unchecked version)
@@ -148,12 +87,8 @@ public ProcessingReport validate(final JsonNode instance)
14887
*
14988
* @since 2.1.8
15089
*/
151-
public ProcessingReport validateUnchecked(final JsonNode instance,
152-
final boolean deepCheck)
153-
{
154-
return doValidateUnchecked(instance, deepCheck);
155-
}
156-
90+
ProcessingReport validateUnchecked(JsonNode instance,
91+
boolean deepCheck);
15792
/**
15893
* Validate an instance and return a processing report (unchecked version)
15994
*
@@ -164,10 +99,7 @@ public ProcessingReport validateUnchecked(final JsonNode instance,
16499
* @return a report (a {@link ListProcessingReport} if an exception was
165100
* thrown during processing)
166101
*/
167-
public ProcessingReport validateUnchecked(final JsonNode instance)
168-
{
169-
return doValidateUnchecked(instance, false);
170-
}
102+
ProcessingReport validateUnchecked(JsonNode instance);
171103

172104
/**
173105
* Check whether an instance is valid against this schema
@@ -176,11 +108,8 @@ public ProcessingReport validateUnchecked(final JsonNode instance)
176108
* @return true if the instance is valid
177109
* @throws ProcessingException an error occurred during processing
178110
*/
179-
public boolean validInstance(final JsonNode instance)
180-
throws ProcessingException
181-
{
182-
return doValidate(instance, false).isSuccess();
183-
}
111+
boolean validInstance(JsonNode instance)
112+
throws ProcessingException;
184113

185114
/**
186115
* Check whether an instance is valid against this schema (unchecked
@@ -192,8 +121,5 @@ public boolean validInstance(final JsonNode instance)
192121
* @param instance the instance to validate
193122
* @return true if the instance is valid
194123
*/
195-
public boolean validInstanceUnchecked(final JsonNode instance)
196-
{
197-
return doValidateUnchecked(instance, false).isSuccess();
198-
}
124+
boolean validInstanceUnchecked(JsonNode instance);
199125
}

src/main/java/com/github/fge/jsonschema/main/JsonSchemaFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
* <li>a {@link SyntaxValidator}, to validate schemas;</li>
6161
* <li>a {@link JsonValidator}, to validate an instance against a schema;
6262
* </li>
63-
* <li>a {@link JsonSchema}, to validate instances against a fixed schema.
63+
* <li>a {@link JsonSchemaImpl}, to validate instances against a fixed schema.
6464
* </li>
6565
* </ul>
6666
*
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com)
3+
*
4+
* This software is dual-licensed under:
5+
*
6+
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
7+
* later version;
8+
* - the Apache Software License (ASL) version 2.0.
9+
*
10+
* The text of this file and of both licenses is available at the root of this
11+
* project or, if you have the jar distribution, in directory META-INF/, under
12+
* the names LGPL-3.0.txt and ASL-2.0.txt respectively.
13+
*
14+
* Direct link to the sources:
15+
*
16+
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
17+
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
18+
*/
19+
20+
package com.github.fge.jsonschema.main;
21+
22+
import com.fasterxml.jackson.databind.JsonNode;
23+
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
24+
import com.github.fge.jsonschema.core.processing.ProcessingResult;
25+
import com.github.fge.jsonschema.core.processing.Processor;
26+
import com.github.fge.jsonschema.core.report.ListProcessingReport;
27+
import com.github.fge.jsonschema.core.report.MessageProvider;
28+
import com.github.fge.jsonschema.core.report.ProcessingReport;
29+
import com.github.fge.jsonschema.core.report.ReportProvider;
30+
import com.github.fge.jsonschema.core.tree.SchemaTree;
31+
import com.github.fge.jsonschema.core.tree.SimpleJsonTree;
32+
import com.github.fge.jsonschema.processors.data.FullData;
33+
import com.github.fge.jsonschema.processors.validation.ValidationProcessor;
34+
35+
import javax.annotation.concurrent.Immutable;
36+
37+
/**
38+
* Single-schema instance validator
39+
*
40+
* <p>This is the class you will use the most often. It is, in essence, a {@link
41+
* JsonValidator} initialized with a single JSON Schema. Note however that this
42+
* class still retains the ability to resolve JSON References.</p>
43+
*
44+
* <p>It has no public constructors: you should use the appropriate methods in
45+
* {@link JsonSchemaFactory} to obtain an instance of this class.</p>
46+
*/
47+
@Immutable
48+
final class JsonSchemaImpl implements JsonSchema
49+
{
50+
private final ValidationProcessor processor;
51+
private final SchemaTree schema;
52+
private final ReportProvider reportProvider;
53+
54+
/**
55+
* Package private constructor
56+
*
57+
* @param processor the validation processor
58+
* @param schema the schema to bind to this instance
59+
* @param reportProvider the report provider
60+
*/
61+
JsonSchemaImpl(final ValidationProcessor processor, final SchemaTree schema,
62+
final ReportProvider reportProvider)
63+
{
64+
this.processor = processor;
65+
this.schema = schema;
66+
this.reportProvider = reportProvider;
67+
}
68+
69+
private ProcessingReport doValidate(final JsonNode node,
70+
final boolean deepCheck)
71+
throws ProcessingException
72+
{
73+
final FullData data = new FullData(schema, new SimpleJsonTree(node),
74+
deepCheck);
75+
final ProcessingReport report = reportProvider.newReport();
76+
final ProcessingResult<FullData> result
77+
= ProcessingResult.of(processor, report, data);
78+
return result.getReport();
79+
}
80+
81+
private ProcessingReport doValidateUnchecked(final JsonNode node,
82+
final boolean deepCheck)
83+
{
84+
final FullData data = new FullData(schema, new SimpleJsonTree(node),
85+
deepCheck);
86+
final ProcessingReport report = reportProvider.newReport();
87+
final ProcessingResult<FullData> result
88+
= ProcessingResult.uncheckedResult(processor, report, data);
89+
return result.getReport();
90+
}
91+
92+
/**
93+
* {@inheritDoc}
94+
*/
95+
@Override
96+
public ProcessingReport validate(final JsonNode instance,
97+
final boolean deepCheck)
98+
throws ProcessingException
99+
{
100+
return doValidate(instance, deepCheck);
101+
}
102+
103+
/**
104+
* {@inheritDoc}
105+
*/
106+
@Override
107+
public ProcessingReport validate(final JsonNode instance)
108+
throws ProcessingException
109+
{
110+
return validate(instance, false);
111+
}
112+
113+
/**
114+
* {@inheritDoc}
115+
*/
116+
@Override
117+
public ProcessingReport validateUnchecked(final JsonNode instance,
118+
final boolean deepCheck)
119+
{
120+
return doValidateUnchecked(instance, deepCheck);
121+
}
122+
123+
/**
124+
* {@inheritDoc}
125+
*/
126+
@Override
127+
public ProcessingReport validateUnchecked(final JsonNode instance)
128+
{
129+
return doValidateUnchecked(instance, false);
130+
}
131+
132+
/**
133+
* {@inheritDoc}
134+
*/
135+
@Override
136+
public boolean validInstance(final JsonNode instance)
137+
throws ProcessingException
138+
{
139+
return doValidate(instance, false).isSuccess();
140+
}
141+
142+
/**
143+
* {@inheritDoc}
144+
*/
145+
@Override
146+
public boolean validInstanceUnchecked(final JsonNode instance)
147+
{
148+
return doValidateUnchecked(instance, false).isSuccess();
149+
}
150+
}

src/main/java/com/github/fge/jsonschema/main/JsonValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ JsonSchema buildJsonSchema(final JsonNode schema, final JsonPointer pointer)
188188
if (tree.getNode().isMissingNode())
189189
throw new JsonReferenceException(new ProcessingMessage()
190190
.setMessage(BUNDLE.getMessage("danglingRef")));
191-
return new JsonSchema(processor, tree, reportProvider);
191+
return new JsonSchemaImpl(processor, tree, reportProvider);
192192
}
193193

194194
/**
@@ -212,7 +212,7 @@ JsonSchema buildJsonSchema(final String uri)
212212
if (tree.getNode().isMissingNode())
213213
throw new JsonReferenceException(new ProcessingMessage()
214214
.setMessage(BUNDLE.getMessage("danglingRef")));
215-
return new JsonSchema(processor, tree, reportProvider);
215+
return new JsonSchemaImpl(processor, tree, reportProvider);
216216
}
217217

218218
/**

src/main/java/com/github/fge/jsonschema/processors/validation/InstanceValidator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.github.fge.jsonschema.core.tree.JsonTree;
3232
import com.github.fge.jsonschema.core.tree.SchemaTree;
3333
import com.github.fge.jsonschema.keyword.validator.KeywordValidator;
34-
import com.github.fge.jsonschema.main.JsonSchema;
3534
import com.github.fge.jsonschema.main.JsonValidator;
3635
import com.github.fge.jsonschema.processors.data.FullData;
3736
import com.github.fge.jsonschema.processors.data.SchemaContext;

src/main/javadoc/com/github/fge/jsonschema/examples/doc-files/Example9.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.github.fge.jsonschema.library.Library;
4141
import com.github.fge.jsonschema.library.LibraryBuilder;
4242
import com.github.fge.jsonschema.main.JsonSchema;
43+
import com.github.fge.jsonschema.main.JsonSchemaImpl;
4344
import com.github.fge.jsonschema.main.JsonSchemaFactory;
4445
import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle;
4546
import com.github.fge.jsonschema.processors.data.FullData;

0 commit comments

Comments
 (0)