Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JsonSchemaUtils.java
Go to the documentation of this file.
1/*-
2 * #%L
3 * ether-json
4 * %%
5 * Copyright (C) 2025 Raúl Eduardo González Argote
6 * %%
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 * #L%
25 */
26
27package dev.rafex.ether.json;
28
29import java.util.List;
30import java.util.Objects;
31import java.util.Set;
32import java.util.stream.Collectors;
33
34import com.fasterxml.jackson.core.type.TypeReference;
35import com.fasterxml.jackson.databind.JsonNode;
36import com.networknt.schema.JsonSchema;
37import com.networknt.schema.JsonSchemaFactory;
38import com.networknt.schema.SpecVersion;
39
40public final class JsonSchemaUtils {
41
42 private static final JsonSchemaFactory FACTORY = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
43
44 private JsonSchemaUtils() {
45 }
46
47 public static List<String> validate(final JsonNode schemaNode, final JsonNode documentNode) {
48 Objects.requireNonNull(schemaNode, "schemaNode");
49 Objects.requireNonNull(documentNode, "documentNode");
50
51 final JsonSchema schema = FACTORY.getSchema(schemaNode);
52 final Set<com.networknt.schema.ValidationMessage> errors = schema.validate(documentNode);
53
54 return errors.stream().map(com.networknt.schema.ValidationMessage::getMessage).sorted().toList();
55 }
56
57 public static List<String> validate(final String schemaJson, final String documentJson) {
58 return validate(JsonUtils.parseTree(schemaJson), JsonUtils.parseTree(documentJson));
59 }
60
61 public static void validateOrThrow(final JsonNode schemaNode, final JsonNode documentNode) {
62 final var violations = validate(schemaNode, documentNode);
63 if (!violations.isEmpty()) {
64 throw new JsonSchemaValidationException("El JSON no cumple el schema", violations);
65 }
66 }
67
68 public static void validateOrThrow(final String schemaJson, final String documentJson) {
69 validateOrThrow(JsonUtils.parseTree(schemaJson), JsonUtils.parseTree(documentJson));
70 }
71
72 public static <T> T parseAndValidateOrThrow(final String schemaJson, final String documentJson,
73 final TypeReference<T> typeReference) {
74 validateOrThrow(schemaJson, documentJson);
75 return JsonUtils.fromJson(documentJson, typeReference);
76 }
77
78 public static String violationsAsText(final List<String> violations) {
79 Objects.requireNonNull(violations, "violations");
80 return violations.stream().collect(Collectors.joining("; "));
81 }
82}
static List< String > validate(final String schemaJson, final String documentJson)
static List< String > validate(final JsonNode schemaNode, final JsonNode documentNode)
static void validateOrThrow(final String schemaJson, final String documentJson)
static< T > T parseAndValidateOrThrow(final String schemaJson, final String documentJson, final TypeReference< T > typeReference)
static void validateOrThrow(final JsonNode schemaNode, final JsonNode documentNode)
static String violationsAsText(final List< String > violations)
static JsonNode parseTree(final String json)
static< T > T fromJson(final String json, final Class< T > clazz)