Skip to content

Commit

Permalink
Merge pull request #10 from relogiclabs/develop
Browse files Browse the repository at this point in the history
Add Scripting Functionalities
  • Loading branch information
zhossain-info authored Feb 23, 2024
2 parents 4f3d1bd + e40b325 commit 1330860
Show file tree
Hide file tree
Showing 304 changed files with 16,895 additions and 6,233 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.relogiclabs.json.schema;
package com.relogiclabs.jschema;

import com.relogiclabs.json.schema.internal.util.DebugUtilities;
import com.relogiclabs.json.schema.tree.DataTree;
import com.relogiclabs.json.schema.tree.JsonTree;
import com.relogiclabs.json.schema.tree.RuntimeContext;
import com.relogiclabs.json.schema.tree.SchemaTree;
import com.relogiclabs.json.schema.tree.TreeType;
import com.relogiclabs.jschema.internal.util.LogHelper;
import com.relogiclabs.jschema.tree.DataTree;
import com.relogiclabs.jschema.tree.JsonTree;
import com.relogiclabs.jschema.tree.RuntimeContext;
import com.relogiclabs.jschema.tree.SchemaTree;
import com.relogiclabs.jschema.tree.TreeType;
import lombok.Getter;

import static com.relogiclabs.json.schema.message.MessageFormatter.JSON_ASSERTION;
import static com.relogiclabs.json.schema.message.MessageFormatter.SCHEMA_ASSERTION;
import static com.relogiclabs.json.schema.tree.TreeType.JSON_TREE;
import static com.relogiclabs.json.schema.tree.TreeType.SCHEMA_TREE;
import static com.relogiclabs.jschema.message.MessageFormatter.JSON_ASSERTION;
import static com.relogiclabs.jschema.message.MessageFormatter.SCHEMA_ASSERTION;
import static com.relogiclabs.jschema.tree.TreeType.JSON_TREE;
import static com.relogiclabs.jschema.tree.TreeType.SCHEMA_TREE;

/**
* The class provides assertion functionalities to validate JSON documents against
* a Schema or JSON.
* a JSchema or JSON document.
*/
@Getter
public class JsonAssert {
Expand All @@ -24,19 +24,19 @@ public class JsonAssert {

/**
* Initializes a new instance of the {@link JsonAssert} class for the
* specified Schema string.
* @param schema A Schema string for validation or conformation
* specified JSchema string.
* @param schema A JSchema string for validation or conformation
*/
public JsonAssert(String schema) {
this(schema, SCHEMA_TREE);
}

/**
* Initializes a new instance of the {@link JsonAssert} class for the specified
* {@code expected} string, which can be either a Schema or a JSON representation.
* @param expected An expected Schema or JSON string for validation or conformation
* {@code expected} string, which can be either a JSchema or a JSON representation.
* @param expected An expected JSchema or JSON string for validation or conformation
* @param type The type of string provided by {@code expected}, indicating whether it represents
* a Schema or JSON. Use {@link TreeType#SCHEMA_TREE} for Schema and {@link TreeType#JSON_TREE}
* a JSchema or JSON. Use {@link TreeType#SCHEMA_TREE} for JSchema and {@link TreeType#JSON_TREE}
* for JSON.
*/
public JsonAssert(String expected, TreeType type) {
Expand All @@ -50,22 +50,22 @@ public JsonAssert(String expected, TreeType type) {
}

/**
* Tests whether the input JSON string conforms to the expected Schema or JSON
* Tests whether the input JSON string conforms to the expected JSchema or JSON
* specified in the {@link JsonAssert} constructor.
* @param json The actual input JSON to conform or validate
*/
public void isValid(String json) {
runtime.clear();
var jsonTree = new JsonTree(runtime, json);
DebugUtilities.print(expectedTree, jsonTree);
LogHelper.debug(expectedTree, jsonTree);
if(!expectedTree.match(jsonTree))
throw new IllegalStateException("Invalid runtime state");
}

/**
* Tests whether the specified JSON string conforms to the given Schema string
* and throws an exception if the JSON string does not conform to the Schema.
* @param schema The expected Schema to conform or validate
* Tests whether the specified JSON string conforms to the given JSchema string
* and throws an exception if the JSON string does not conform to the JSchema.
* @param schema The expected JSchema to conform or validate
* @param json The actual JSON to conform or validate
*/
public static void isValid(String schema, String json) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.relogiclabs.json.schema;
package com.relogiclabs.jschema;

import com.relogiclabs.json.schema.internal.util.DebugUtilities;
import com.relogiclabs.json.schema.tree.ExceptionRegistry;
import com.relogiclabs.json.schema.tree.JsonTree;
import com.relogiclabs.json.schema.tree.RuntimeContext;
import com.relogiclabs.json.schema.tree.SchemaTree;
import com.relogiclabs.jschema.internal.util.LogHelper;
import com.relogiclabs.jschema.tree.ExceptionRegistry;
import com.relogiclabs.jschema.tree.JsonTree;
import com.relogiclabs.jschema.tree.RuntimeContext;
import com.relogiclabs.jschema.tree.SchemaTree;
import lombok.Getter;

import static com.relogiclabs.json.schema.message.MessageFormatter.SCHEMA_VALIDATION;
import static com.relogiclabs.jschema.message.MessageFormatter.SCHEMA_VALIDATION;

/**
* {@code JsonSchema} provides Schema validation functionalities for JSON document.
* {@code JsonSchema} provides JSchema validation functionalities for JSON document.
*/
@Getter
public class JsonSchema {
Expand All @@ -20,8 +20,8 @@ public class JsonSchema {

/**
* Initializes a new instance of the {@link JsonSchema} class for the
* specified Schema string.
* @param schema A Schema string for validation
* specified JSchema string.
* @param schema A JSchema string for validation
*/
public JsonSchema(String schema) {
runtime = new RuntimeContext(SCHEMA_VALIDATION, false);
Expand All @@ -30,32 +30,35 @@ public JsonSchema(String schema) {
}

/**
* Indicates whether the input JSON string conforms to the Schema specified
* Indicates whether the input JSON string conforms to the JSchema specified
* in the {@link JsonSchema} constructor.
* @param json The JSON to validate with Schema
* @return Returns {@code true} if the JSON string conforms to the Schema and {@code false} otherwise.
* @param json The JSON to validate with JSchema
* @return Returns {@code true} if the JSON string conforms to the JSchema and {@code false} otherwise.
*/
public boolean isValid(String json) {
runtime.clear();
var jsonTree = new JsonTree(runtime, json);
DebugUtilities.print(schemaTree, jsonTree);
LogHelper.debug(schemaTree, jsonTree);
return schemaTree.match(jsonTree);
}

/**
* Writes error messages that occur during Schema validation process, to the
* Writes error messages that occur during JSchema validation process, to the
* standard error output stream.
*/
public void writeError() {
for(var exception : exceptions)
System.err.println(exception.getMessage());
if(exceptions.getCount() == 0) {
System.out.println("No error has occurred");
return;
}
for(var exception : exceptions) System.err.println(exception.getMessage());
}

/**
* Indicates whether the input JSON string conforms to the given Schema string.
* @param schema The Schema string to conform or validate
* Indicates whether the input JSON string conforms to the given JSchema string.
* @param schema The JSchema string to conform or validate
* @param json The JSON string to conform or validate
* @return Returns {@code true} if the JSON string conforms to the Schema and {@code false} otherwise.
* @return Returns {@code true} if the JSON string conforms to the JSchema and {@code false} otherwise.
*/
public static boolean isValid(String schema, String json) {
var jsonSchema = new JsonSchema(schema);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.relogiclabs.json.schema.collection;
package com.relogiclabs.jschema.collection;

import java.util.Collection;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.relogiclabs.json.schema.collection;
package com.relogiclabs.jschema.collection;

public interface Keyable<TK> {
TK getKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;

public class ClassInstantiationException extends CommonException {
public ClassInstantiationException(ErrorDetail detail, Throwable cause) {
super(detail, cause);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

import static java.util.Arrays.copyOfRange;

public class CommonException extends RuntimeException {
private static final String FAIL_METHOD_PREFIX = "fail";
@Getter private final String code;
private Map<String, String> attributes;

public CommonException(String code, String message, Throwable cause) {
super(message, cause);
this.code = code;
formatStackTrace();
}

public CommonException(String code, String message) {
Expand All @@ -36,4 +40,20 @@ public void setAttribute(String name, String value) {
if(attributes == null) attributes = new HashMap<>(5);
attributes.put(name, value);
}

@Override
public synchronized Throwable fillInStackTrace() {
var result = super.fillInStackTrace();
formatStackTrace();
return result;
}

private void formatStackTrace() {
StackTraceElement[] stackTrace = getStackTrace();
int offset = 0;
for(var e : stackTrace)
if(e.getMethodName().startsWith(FAIL_METHOD_PREFIX)) offset++;
else break;
setStackTrace(copyOfRange(stackTrace, offset, stackTrace.length));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

public class DateTimeLexerException extends CommonException {
public DateTimeLexerException(String code, String message, Throwable cause) {
super(code, message, cause);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;

public class DefinitionNotFoundException extends CommonException {
public DefinitionNotFoundException(ErrorDetail detail) {
super(detail);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;

public class DuplicateDefinitionException extends CommonException {
public DuplicateDefinitionException(ErrorDetail detail) {
super(detail);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.relogiclabs.jschema.exception;

import com.relogiclabs.jschema.message.ErrorDetail;

public class DuplicateImportException extends CommonException {
public DuplicateImportException(ErrorDetail detail) {
super(detail);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;

public class DuplicatePragmaException extends CommonException {
public DuplicatePragmaException(ErrorDetail detail) {
super(detail);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;

public class DuplicatePropertyKeyException extends CommonException {
public DuplicatePropertyKeyException(ErrorDetail detail) {
super(detail);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;

public class FunctionNotFoundException extends CommonException {
public FunctionNotFoundException(ErrorDetail detail) {
super(detail);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;

public class InvalidDataTypeException extends CommonException {
public InvalidDataTypeException(ErrorDetail detail) {
super(detail);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

public class InvalidDateTimeException extends CommonException {
public InvalidDateTimeException(String code, String message) {
super(code, message);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;

public class InvalidFunctionException extends CommonException {
public InvalidFunctionException(String code, String message) {
Expand All @@ -10,4 +10,4 @@ public InvalidFunctionException(String code, String message) {
public InvalidFunctionException(ErrorDetail detail) {
super(detail);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.relogiclabs.jschema.exception;

import com.relogiclabs.jschema.message.ErrorDetail;

public class InvalidImportException extends CommonException {
public InvalidImportException(ErrorDetail detail) {
super(detail);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.relogiclabs.json.schema.exception;
package com.relogiclabs.jschema.exception;

import com.relogiclabs.json.schema.message.ErrorDetail;
import com.relogiclabs.jschema.message.ErrorDetail;

public class InvalidPragmaValueException extends CommonException {
public InvalidPragmaValueException(ErrorDetail detail) {
super(detail);
}
}
}
Loading

0 comments on commit 1330860

Please sign in to comment.