2

Is it really possible that there is no JSR-303 compatible validator for Android around?

Hibernate validator seems not compatible with Android (ClassNotFound Exception) and then... I could not find anything else.

I found Metawidget - but on their Website they say "Metawidget comes with a UI component native to your existing front-end" - I don't need additional UI components.

Here is what I have on the backend:

    final String wrongJson = "{\"name\":\"notavalidemail\"}";
    final Gson gson = new Gson();
    final Project projectFromJson = gson.fromJson(wrongJson, Project.class);

    final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    final Validator validator = factory.getValidator();

    final Set<ConstraintViolation<Project>> constraintViolations = validator.validate(projectFromJson);
    assertNotNull(constraintViolations);

    for (final ConstraintViolation<Project> violation : constraintViolations) {
        logger.debug("Errormessage: {}",violation.getMessage());
    }

To avoid a bunch of ifs I want the same on Android - any hints?

[Update] Sorry - it's not ClassNotFound it is

javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath. at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:271) at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110) at at.mikemitterer.mobiad.android.util.ProjectTest.testWithWrongJsonString(ProjectTest.java:90) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

[Update II] Yes - hibernate-validator-5.1.0.Alpha1.jar is in my lib folder

Mike Mitterer
  • 6,810
  • 4
  • 41
  • 62

2 Answers2

0

Whiche CNF did you exactly get? If it is about java.beans.Introspector, this is fixed as of HV 5.1.0.Alpha1 which doesn't import this type anymore and thus should run on Android. If it is about other APIs it would be very interesting to know which ones.

Gunnar
  • 18,095
  • 1
  • 53
  • 73
  • I'm no expert on Android, but according to [this](http://stackoverflow.com/questions/5760607/using-serviceloader-on-android) there are issues with the `ServiceLoader` in this environment (which BV uses to load providers). You might try to explicitly specify your provider like so: `Validation.byProvider(HibernateValidator.class).configure().buidValidatorFactory()`; – Gunnar Oct 24 '13 at 06:57
  • I tried this a few mins ago - same result. I also tried it with ApacheValidationProvider, the same. I think it's a more general problem. Android seems to fail if it should load these Validator classes at runtime. – Mike Mitterer Oct 24 '13 at 07:05
  • 1
    Try plugging in your own `ValidationProviderResolver` (via `providerResolver()`) which only returns `HibernateValidator`. This should circumvent the service loader used by the default provider resolver. – Gunnar Oct 24 '13 at 10:36
0

I came to this question, following my quest for some integration of JSR 343 and Angular.js , which should be more trickier than in your case , as well, with Android you develop with Java.

If you look at valdr you will see a framework with model centric approach to validation in angular.js and you will see there a plugin to support JSR 303. The plugin is composed of several components, you might want to consider "imitating" in Android, for client-server applications and have one "central place" that defines the rules of validation. For this you will probably need the following:

  1. Define the classes at server side to scan for JSR 303 annotations
  2. Which classes that your android app are mapped to these java beans
  3. A way to get the scanning result and dynmically construct a set of rules
  4. Use each rule when a setter on an object that was mapped is being activated.

If this is an android app with no server, then I guess you should try to port the source for JSR 303 to be use with Android

user229044
  • 232,980
  • 40
  • 330
  • 338
Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27