Skip to content

Commit da03af2

Browse files
committed
json validation
1 parent 4e5ec58 commit da03af2

38 files changed

+2207
-26
lines changed

engine/pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@
103103
<artifactId>money-api</artifactId>
104104
<optional>true</optional>
105105
</dependency>
106-
106+
<dependency>
107+
<groupId>javax.json</groupId>
108+
<artifactId>javax.json-api</artifactId>
109+
<optional>true</optional>
110+
</dependency>
107111
<!--
108112
Test dependencies
109113
-->
@@ -147,6 +151,11 @@
147151
<artifactId>moneta</artifactId>
148152
<scope>test</scope>
149153
</dependency>
154+
<dependency>
155+
<groupId>org.glassfish</groupId>
156+
<artifactId>javax.json</artifactId>
157+
<scope>test</scope>
158+
</dependency>
150159
</dependencies>
151160

152161
<build>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator;
8+
9+
import java.util.Set;
10+
11+
import javax.json.JsonObject;
12+
import javax.validation.ConstraintViolation;
13+
14+
/**
15+
* An interface for validating objects like JSON or Map.
16+
*
17+
* @author Marko Bekhta
18+
*/
19+
@Incubating
20+
public interface HibernateFreeFormValidator {
21+
22+
Set<ConstraintViolation<JsonObject>> validateJson(JsonObject json, Class<?> typeToValidate, Class<?>... groups);
23+
}

engine/src/main/java/org/hibernate/validator/HibernateValidatorConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import javax.validation.valueextraction.ValueExtractor;
1919

2020
import org.hibernate.validator.cfg.ConstraintMapping;
21+
import org.hibernate.validator.cfg.json.JsonConstraintMapping;
2122
import org.hibernate.validator.constraints.ParameterScriptAssert;
2223
import org.hibernate.validator.constraints.ScriptAssert;
2324
import org.hibernate.validator.spi.properties.GetterPropertyMatcher;
@@ -333,4 +334,9 @@ public interface HibernateValidatorConfiguration extends Configuration<Hibernate
333334
*/
334335
@Incubating
335336
HibernateValidatorConfiguration getterPropertyMatcher(GetterPropertyMatcher getterPropertyMatcher);
337+
338+
@Incubating
339+
HibernateValidatorConfiguration addJsonMapping(JsonConstraintMapping mapping);
340+
341+
JsonConstraintMapping createJsonConstraintMapping();
336342
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.cfg.json;
8+
9+
/**
10+
* Facet of a constraint mapping creational context which allows to mark the underlying
11+
* element as to be validated in a cascaded way.
12+
*
13+
* @author Gunnar Morling
14+
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
15+
*/
16+
public interface Cascadable<C extends Cascadable<C>> {
17+
18+
/**
19+
* Marks the current element (property, parameter etc.) as cascadable.
20+
*
21+
* @return The current creational context following the method chaining pattern.
22+
*/
23+
C valid();
24+
25+
/**
26+
* Adds a group conversion for this cascadable element. Several conversions may be configured for one element.
27+
*
28+
* @param from the source group of the conversion to be configured
29+
*
30+
* @return a creational context allow to set the target group of the conversion
31+
*/
32+
GroupConversionTargetContext<C> convertGroup(Class<?> from);
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.cfg.json;
8+
9+
import org.hibernate.validator.cfg.ConstraintDef;
10+
11+
/**
12+
* Facet of a constraint mapping creational context which allows to place
13+
* constraints on the underlying element.
14+
*
15+
* @author Gunnar Morling
16+
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
17+
*/
18+
public interface Constrainable<C extends Constrainable<C>> {
19+
/**
20+
* Adds a new constraint.
21+
*
22+
* @param definition The constraint to add.
23+
*
24+
* @return The current creational context following the method chaining pattern.
25+
*/
26+
C constraint(ConstraintDef<?, ?> definition);
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.cfg.json;
8+
9+
/**
10+
* Creational context which allows to set the target group of a group conversion configured via
11+
* {@link Cascadable#convertGroup(Class)}.
12+
*
13+
* @author Gunnar Morling
14+
*/
15+
public interface GroupConversionTargetContext<C> {
16+
17+
/**
18+
* Sets the target group of the conversion to be configured.
19+
*
20+
* @param to the target group of the conversion
21+
*
22+
* @return The current creational context following the method chaining pattern.
23+
*/
24+
C to(Class<?> to);
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.cfg.json;
8+
9+
/**
10+
* Represents a constraint mapping configured via the programmatic API.
11+
*
12+
* @author Hardy Ferentschik
13+
* @author Gunnar Morling
14+
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
15+
* @author Yoann Rodiere
16+
* @author Marko Bekhta
17+
*/
18+
public interface JsonConstraintMapping {
19+
20+
/**
21+
* Starts defining constraints on the specified bean class. Each bean class may only be configured once within all
22+
* constraint mappings used for configuring one validator factory.
23+
*
24+
* @param <C> The type to be configured.
25+
* @param beanClass The bean class on which to define constraints. All constraints defined after calling this method
26+
* are added to the bean of the type {@code beanClass} until the next call of {@code type} or {@code annotation}.
27+
*
28+
* @return Instance allowing for defining constraints on the specified class.
29+
*/
30+
<C> TypeConstraintMappingContext<C> type(Class<C> beanClass);
31+
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.cfg.json;
8+
9+
/**
10+
* Constraint mapping creational context representing a property of a bean. Allows
11+
* to place constraints on the property, mark the property as cascadable and to
12+
* navigate to other constraint targets.
13+
*
14+
* @author Gunnar Morling
15+
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
16+
* @author Marko Bekhta
17+
*/
18+
public interface PropertyConstraintMappingContext extends Constrainable<PropertyConstraintMappingContext>,
19+
PropertyTarget,
20+
Cascadable<PropertyConstraintMappingContext> {
21+
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.cfg.json;
8+
9+
/**
10+
* Facet of a constraint mapping creational context which allows to the select the bean
11+
* property to which the next operations shall apply.
12+
*
13+
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
14+
* @author Gunnar Morling
15+
* @author Marko Bekhta
16+
*/
17+
public interface PropertyTarget {
18+
19+
/**
20+
* Selects a property to which the next operations shall apply.
21+
* <p>
22+
* Until this method is called constraints apply on class level. After calling this method constraints
23+
* apply on the specified property with the given access type.
24+
* </p>
25+
* <p>
26+
* A given property may only be configured once.
27+
* </p>
28+
*
29+
* @param property The property on which to apply the following constraints (Java Bean notation).
30+
*
31+
* @return A creational context representing the selected property.
32+
*/
33+
PropertyConstraintMappingContext property(String property, Class<?> propertyType);
34+
35+
36+
PropertyConstraintMappingContext property(String property);
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.cfg.json;
8+
9+
import javax.json.JsonObject;
10+
11+
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;
12+
13+
/**
14+
* Constraint mapping creational context representing a type. Allows place
15+
* class-level constraints on that type, define its default group sequence (and provider)
16+
* and to navigate to other constraint targets.
17+
*
18+
* @param <C> The type represented by this creational context.
19+
*
20+
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
21+
* @author Gunnar Morling
22+
*/
23+
public interface TypeConstraintMappingContext<C> extends Constrainable<TypeConstraintMappingContext<C>>,
24+
PropertyTarget {
25+
26+
/**
27+
* Defines the default group sequence for current type.
28+
*
29+
* @param defaultGroupSequence the default group sequence.
30+
*
31+
* @return The current creational context following the method chaining pattern.
32+
*/
33+
TypeConstraintMappingContext<C> defaultGroupSequence(Class<?>... defaultGroupSequence);
34+
35+
/**
36+
* Defines the default group sequence provider for the current type.
37+
*
38+
* @param defaultGroupSequenceProviderClass The default group sequence provider class.
39+
*
40+
* @return The current creational context following the method chaining pattern.
41+
*/
42+
TypeConstraintMappingContext<C> defaultGroupSequenceProviderClass(
43+
Class<? extends DefaultGroupSequenceProvider<JsonObject>> defaultGroupSequenceProviderClass);
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.cfg.json;
8+
9+
/**
10+
* Facet of a constraint mapping creational context which allows to the select the bean
11+
* type to which the next operations shall apply.
12+
*
13+
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
14+
* @author Gunnar Morling
15+
*/
16+
public interface TypeTarget {
17+
/**
18+
* Selects the type to which the next operations shall apply. A given type may only be configured once.
19+
*
20+
* @param <C> The type to select.
21+
* @param type The type to select.
22+
*
23+
* @return A creational context representing the selected type.
24+
*/
25+
<C> TypeConstraintMappingContext<C> type(Class<C> type);
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
8+
/**
9+
* <p>Contains facet and creational context interfaces forming the API for programmatic constraint definition for Json validation.</p>
10+
* <p>This package is part of the public Hibernate Validator API.</p>
11+
*/
12+
package org.hibernate.validator.cfg.json;

0 commit comments

Comments
 (0)