-
Notifications
You must be signed in to change notification settings - Fork 579
Explore JSON validation #949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cd139d0
805431b
804660b
9ef7773
9b72630
4e5ec58
da03af2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.cdi.internal.util; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Type; | ||
|
||
import javax.validation.ValidatorFactory; | ||
|
||
import org.hibernate.validator.HibernateValidatorFactory; | ||
import org.hibernate.validator.internal.properties.DefaultGetterPropertyMatcher; | ||
import org.hibernate.validator.spi.properties.ConstrainableExecutable; | ||
import org.hibernate.validator.spi.properties.GetterPropertyMatcher; | ||
|
||
/** | ||
* A wrapper around {@link GetterPropertyMatcher}. | ||
* | ||
* @author Marko Bekhta | ||
*/ | ||
public class GetterPropertyMatcherHelper { | ||
|
||
private final GetterPropertyMatcher getterPropertyMatcher; | ||
|
||
private GetterPropertyMatcherHelper(GetterPropertyMatcher getterPropertyMatcher) { | ||
this.getterPropertyMatcher = getterPropertyMatcher; | ||
} | ||
|
||
public boolean isProperty(Method method) { | ||
return getterPropertyMatcher.isProperty( new ConstrainableMethod( method ) ); | ||
} | ||
|
||
public String getPropertyName(Method method) { | ||
return getterPropertyMatcher.getPropertyName( new ConstrainableMethod( method ) ); | ||
} | ||
|
||
public static GetterPropertyMatcherHelper forValidationFactory(ValidatorFactory factory) { | ||
GetterPropertyMatcher getterPropertyMatcher; | ||
if ( factory instanceof HibernateValidatorFactory ) { | ||
getterPropertyMatcher = factory.unwrap( HibernateValidatorFactory.class ).getGetterPropertyMatcher(); | ||
} | ||
else { | ||
getterPropertyMatcher = new DefaultGetterPropertyMatcher(); | ||
} | ||
return new GetterPropertyMatcherHelper( getterPropertyMatcher ); | ||
} | ||
|
||
private static class ConstrainableMethod implements ConstrainableExecutable { | ||
|
||
private final Method method; | ||
|
||
private ConstrainableMethod(Method method) { | ||
this.method = method; | ||
} | ||
|
||
@Override public Class<?> getReturnType() { | ||
return method.getReturnType(); | ||
} | ||
|
||
@Override public String getName() { | ||
return method.getName(); | ||
} | ||
|
||
@Override public Type[] getParameterTypes() { | ||
return method.getParameterTypes(); | ||
} | ||
|
||
public Method getMethod() { | ||
return method; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator; | ||
|
||
import java.util.Set; | ||
|
||
import javax.json.JsonObject; | ||
import javax.validation.ConstraintViolation; | ||
|
||
/** | ||
* An interface for validating objects like JSON or Map. | ||
* | ||
* @author Marko Bekhta | ||
*/ | ||
@Incubating | ||
public interface HibernateFreeFormValidator { | ||
|
||
Set<ConstraintViolation<JsonObject>> validateJson(JsonObject json, Class<?> typeToValidate, Class<?>... groups); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.cfg.json; | ||
|
||
/** | ||
* Facet of a constraint mapping creational context which allows to mark the underlying | ||
* element as to be validated in a cascaded way. | ||
* | ||
* @author Gunnar Morling | ||
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI | ||
*/ | ||
public interface Cascadable<C extends Cascadable<C>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it that all these interfaces are copied for JSON? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the issue is that when declaring something as cascading, we need to define the new JSON type that will be used. Maybe we could try to use some more generics in the DSL and have this difference managed this way (e.g. having a generics for the Cascadable DSL and inject it where it makes sense). Not sure it's doable nor very future proof. |
||
|
||
/** | ||
* Marks the current element (property, parameter etc.) as cascadable. | ||
* | ||
* @return The current creational context following the method chaining pattern. | ||
*/ | ||
C valid(); | ||
|
||
/** | ||
* Adds a group conversion for this cascadable element. Several conversions may be configured for one element. | ||
* | ||
* @param from the source group of the conversion to be configured | ||
* | ||
* @return a creational context allow to set the target group of the conversion | ||
*/ | ||
GroupConversionTargetContext<C> convertGroup(Class<?> from); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.cfg.json; | ||
|
||
import org.hibernate.validator.cfg.ConstraintDef; | ||
|
||
/** | ||
* Facet of a constraint mapping creational context which allows to place | ||
* constraints on the underlying element. | ||
* | ||
* @author Gunnar Morling | ||
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI | ||
*/ | ||
public interface Constrainable<C extends Constrainable<C>> { | ||
/** | ||
* Adds a new constraint. | ||
* | ||
* @param definition The constraint to add. | ||
* | ||
* @return The current creational context following the method chaining pattern. | ||
*/ | ||
C constraint(ConstraintDef<?, ?> definition); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.cfg.json; | ||
|
||
/** | ||
* Creational context which allows to set the target group of a group conversion configured via | ||
* {@link Cascadable#convertGroup(Class)}. | ||
* | ||
* @author Gunnar Morling | ||
*/ | ||
public interface GroupConversionTargetContext<C> { | ||
|
||
/** | ||
* Sets the target group of the conversion to be configured. | ||
* | ||
* @param to the target group of the conversion | ||
* | ||
* @return The current creational context following the method chaining pattern. | ||
*/ | ||
C to(Class<?> to); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.cfg.json; | ||
|
||
/** | ||
* Represents a constraint mapping configured via the programmatic API. | ||
* | ||
* @author Hardy Ferentschik | ||
* @author Gunnar Morling | ||
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI | ||
* @author Yoann Rodiere | ||
* @author Marko Bekhta | ||
*/ | ||
public interface JsonConstraintMapping { | ||
|
||
/** | ||
* Starts defining constraints on the specified bean class. Each bean class may only be configured once within all | ||
* constraint mappings used for configuring one validator factory. | ||
* | ||
* @param <C> The type to be configured. | ||
* @param beanClass The bean class on which to define constraints. All constraints defined after calling this method | ||
* are added to the bean of the type {@code beanClass} until the next call of {@code type} or {@code annotation}. | ||
* | ||
* @return Instance allowing for defining constraints on the specified class. | ||
*/ | ||
<C> TypeConstraintMappingContext<C> type(Class<C> beanClass); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.cfg.json; | ||
|
||
/** | ||
* Constraint mapping creational context representing a property of a bean. Allows | ||
* to place constraints on the property, mark the property as cascadable and to | ||
* navigate to other constraint targets. | ||
* | ||
* @author Gunnar Morling | ||
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI | ||
* @author Marko Bekhta | ||
*/ | ||
public interface PropertyConstraintMappingContext extends Constrainable<PropertyConstraintMappingContext>, | ||
PropertyTarget, | ||
Cascadable<PropertyConstraintMappingContext> { | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is
typeToValidate
? Also the scope of the interface is a bit blurry to me. The class comment describes it as a generic on, but then the method signature is tied to JSON. Can we make it truly generic?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this first experiment, Marko did use a class as a way to define the JSON type, thus this parameter here and the
beanClass
attribute you mention a bit later.We already decided we don't want that so it will be changed.