Browse files

validator object added

  • Loading branch information...
1 parent 481c2e6 commit 67eb13e46c9a9caab530d30b40e02f573760e223 @michelegonella committed Nov 9, 2012
View
9 ...ject/src/main/java/com/nominanuda/dataobject/schema/DefaultPrimitiveValidatorFactory.java
@@ -17,10 +17,13 @@
import com.nominanuda.lang.Fun1;
+import static com.nominanuda.dataobject.DataStructHelper.STRUCT;
+
//TODO existential qualifiers
public class DefaultPrimitiveValidatorFactory implements
PrimitiveValidatorFactory {
+
@Override
public Fun1<Object, String> create(String primitiveTypeDef) {
if("n".equals(primitiveTypeDef)) {
@@ -47,6 +50,12 @@ public String apply(Object param) {
return param instanceof Boolean ? null : "not a boolean";
}
};
+ } else if(DefaultPrimitiveValidatorFactory.ANYPRIMITIVE.equals(primitiveTypeDef)) {
+ return new Fun1<Object, String>() {
+ public String apply(Object param) {
+ return STRUCT.isPrimitiveOrNull(param) ? null : "not a boolean";
+ }
+ };
} else {
throw new IllegalArgumentException(primitiveTypeDef+" not found");//TODO better msg
}
View
60 zen-dataobject/src/main/java/com/nominanuda/dataobject/schema/JclValidator.java
@@ -0,0 +1,60 @@
+package com.nominanuda.dataobject.schema;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+import java.net.URL;
+
+import com.nominanuda.code.Nullable;
+import com.nominanuda.dataobject.DataStruct;
+import com.nominanuda.dataobject.transform.DevNullJsonContentHandler;
+import com.nominanuda.dataobject.transform.JsonPipeline;
+import com.nominanuda.dataobject.transform.JsonTransformer;
+import com.nominanuda.io.IOHelper;
+import com.nominanuda.lang.Fun1;
+import com.nominanuda.lang.ObjectFactory;
+
+public class JclValidator {
+ private JclValidatorFactory jvf;
+
+ public Fun1<DataStruct, Boolean> buildValidatorFor(DataStruct ds, @Nullable String type) {
+ final JsonPipeline p = new JsonPipeline()
+ .add(buildValidatorTransformerFactory(type));
+ return new Fun1<DataStruct, Boolean>() {
+ public Boolean apply(final DataStruct param) {
+ try {
+ p.build(param, new DevNullJsonContentHandler()).run();
+ return true;
+ } catch(Exception e) {
+ return false;
+ }
+ }
+ };
+ }
+ public JsonPipeline buildValidatorPipe(@Nullable String type) {
+ JsonPipeline p = new JsonPipeline()
+ .add(buildValidatorTransformerFactory(type));
+ return p;
+ }
+ public ObjectFactory<JsonTransformer> buildValidatorTransformerFactory(
+ @Nullable String type) {
+ return jvf.buildValidatorFactory(type);
+ }
+ public void setSchema(InputStream schema) throws Exception {
+ setSchema(new InputStreamReader(schema));
+ }
+ public void setSchema(String schema) throws Exception {
+ jvf = new JclValidatorFactory(schema);
+ }
+ public void setSchemaUrl(String schemaUrl) throws Exception {
+ InputStream is = new URL(schemaUrl).openStream();
+ setSchema(is);
+ }
+ public void setSchema(Reader schema) throws IOException, Exception {
+ setSchema(IOHelper.IO.readAndClose(schema));
+
+ }
+
+}
View
14 zen-dataobject/src/main/java/com/nominanuda/dataobject/schema/JclValidatorFactory.java
@@ -54,7 +54,11 @@ public JclValidatorFactory(String jclExpr) throws Exception {
CommonTree tree = (CommonTree)parser.program().getTree();
root = tree;
Tree cur = root;
- if(cur.getText() != null) {
+ if(cur.getType() == TYPEDEF) {
+ String tname = cur.getChild(0).getText();
+ tMap.put(tname, cur.getChild(1));
+ tMap.put("", cur.getChild(1));
+ } else if(cur.getText() != null) {
tMap.put("", cur);
} else {
int len = cur.getChildCount();
@@ -164,9 +168,11 @@ private EventConsumer makeConsumer(Tree node, Stack<EventConsumer> stack) {
case PRIMITIVE:
return new PrimitiveConsumer(stack, makePrimitiveValidator(node));
case ARRAYVAL:
- Tree primitive = node.getChild(0);
- Tree existential = node.getChild(1);
- return new PrimitiveConsumer(stack, makePrimitiveValidator(primitive), tokenToPredicate(existential));
+ Tree value = node.getChild(0);
+ Tree pred = node.getChild(1);
+ EventConsumer c = makeConsumer(value, stack);
+ c.setPredicate(tokenToPredicate(pred));
+ return c;
case TYPEREF:
String tname = node.getChild(0).getText();
Tree typedef = Check.illegalargument.assertNotNull(
View
35 zen-dataobject/src/test/java/com/nominanuda/dataobject/schema/FullValidatorTest.java
@@ -0,0 +1,35 @@
+package com.nominanuda.dataobject.schema;
+
+import static org.junit.Assert.*;
+
+import java.io.InputStream;
+import java.io.StringReader;
+
+import org.junit.Test;
+
+import com.nominanuda.dataobject.DataStruct;
+import com.nominanuda.dataobject.DataStructHelper;
+import com.nominanuda.dataobject.transform.DevNullJsonContentHandler;
+import com.nominanuda.dataobject.transform.JsonPipeline;
+
+public class FullValidatorTest {
+
+ @Test
+ public void test() throws Exception {
+ InputStream schema = getClass().getResourceAsStream("sampleschema.jcl");
+ JclValidator validator = new JclValidator();
+ validator.setSchema(schema);
+ String[] jsons = {
+ "User::{name:'foo',password:'bar'}",
+ "User::{name:'foo',password:'bar',addr:{city:null}}",
+ };
+ for(String json : jsons) {
+ String type = json.split("::")[0];
+ DataStruct ds = DataStructHelper.STRUCT.parse(json.split("::")[1], true);
+ JsonPipeline p = validator.buildValidatorPipe(type).withLooseParser().complete();
+ p.build(new StringReader(json.split("::")[1]), new DevNullJsonContentHandler()).run();
+ assertTrue(validator.buildValidatorFor(ds, type).apply(ds));
+ }
+ }
+
+}
View
5 zen-dataobject/src/test/resources/com/nominanuda/dataobject/schema/sampleschema.jcl
@@ -0,0 +1,5 @@
+IdObj @ {id!}
+User @ {name!:s,password!:s,hobbies?:[{*}], addr?:@Address}
+Address @ {city,country?,line1?,line2?,postCode?}
+Blog @ {owner:@IdObj/*of type User*/, title!:s, posts:[@Post]}
+Post @ {text,title}

0 comments on commit 67eb13e

Please sign in to comment.