From c2c394301fe591b3d66c86fcf5a1edcfd353c4a3 Mon Sep 17 00:00:00 2001 From: Zahid Hossain <60911932+zhossain-info@users.noreply.github.com> Date: Mon, 29 Jul 2024 08:51:42 +0600 Subject: [PATCH 1/7] Add and update exception types --- ...ption.java => AliasNotFoundException.java} | 4 +- .../exception/ArgumentTypeException.java | 28 +++++++++++ .../exception/ArgumentValueException.java | 28 +++++++++++ .../ArrayIndexOutOfBoundsException.java | 9 ++++ ...ception.java => BaseRuntimeException.java} | 37 ++++++--------- .../ClassInstantiationException.java | 2 +- .../DataTypeValidationException.java | 22 +++++++++ .../exception/DateTimeLexerException.java | 2 +- ...tion.java => DuplicateAliasException.java} | 4 +- .../exception/DuplicateFunctionException.java | 23 ++++++++++ .../exception/DuplicateImportException.java | 2 +- .../DuplicateParameterException.java | 9 ++++ .../exception/DuplicatePragmaException.java | 2 +- .../DuplicatePropertyKeyException.java | 2 +- .../exception/DuplicateVariableException.java | 23 ++++++++++ .../FunctionArgumentTypeException.java | 24 ++++++++++ .../FunctionArgumentValueException.java | 28 +++++++++++ .../exception/FunctionNotFoundException.java | 2 +- .../FunctionValidationException.java | 22 +++++++++ .../exception/IndexOutOfBoundsException.java | 9 ++++ .../exception/InvalidArgumentException.java | 41 +++++++++++++++++ .../exception/InvalidContextException.java | 31 +++++++++++++ .../exception/InvalidDataTypeException.java | 2 +- .../exception/InvalidDateTimeException.java | 16 +++++-- .../exception/InvalidFunctionException.java | 6 +-- .../exception/InvalidImportException.java | 2 +- .../exception/InvalidLeftValueException.java | 9 ++++ .../InvalidPragmaValueException.java | 2 +- .../InvalidReceiverStateException.java | 2 +- .../exception/InvalidReturnTypeException.java | 13 ++++++ .../exception/InvocationRuntimeException.java | 36 +++++++++++++++ .../jschema/exception/JsonLexerException.java | 2 +- .../exception/JsonParserException.java | 2 +- .../MethodArgumentTypeException.java | 31 +++++++++++++ .../MethodArgumentValueException.java | 28 +++++++++++ .../exception/MethodNotFoundException.java | 29 ++++++++++++ .../exception/MisplacedOptionalException.java | 2 +- .../exception/MultilevelRuntimeException.java | 46 +++++++++++++++++++ .../exception/NoClassFoundException.java | 2 +- .../exception/PragmaNotFoundException.java | 2 +- .../exception/PropertyNotFoundException.java | 9 ++++ .../exception/PropertyOrderException.java | 14 ++++++ .../exception/ReceiverNotFoundException.java | 2 +- .../exception/SchemaLexerException.java | 2 +- .../exception/SchemaParserException.java | 2 +- .../exception/ScriptArgumentException.java | 7 --- .../exception/ScriptArrayInsertException.java | 23 ++++++++++ .../exception/ScriptCommonException.java | 21 --------- .../exception/ScriptFunctionException.java | 7 --- .../exception/ScriptInitiatedException.java | 13 ------ .../exception/ScriptInvocationException.java | 20 -------- .../exception/ScriptIteratorException.java | 23 ++++++++++ .../exception/ScriptOperationException.java | 2 +- .../exception/ScriptRuntimeException.java | 22 --------- .../exception/ScriptTemplateException.java | 24 ---------- .../ScriptThrowInitiatedException.java | 9 ++++ .../exception/ScriptVariableException.java | 7 --- .../StringIndexOutOfBoundsException.java | 9 ++++ .../exception/SystemOperationException.java | 2 +- .../exception/TargetInvocationException.java | 2 +- .../UpdateNotSupportedException.java | 22 ++++++++- ...xception.java => ValidationException.java} | 22 ++++++--- .../exception/ValueValidationException.java | 17 +++++++ .../exception/VariableNotFoundException.java | 9 ++++ .../jschema/internal/util/ExceptionLevel.java | 5 ++ 65 files changed, 696 insertions(+), 185 deletions(-) rename src/main/java/com/relogiclabs/jschema/exception/{DefinitionNotFoundException.java => AliasNotFoundException.java} (50%) create mode 100644 src/main/java/com/relogiclabs/jschema/exception/ArgumentTypeException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/ArgumentValueException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/ArrayIndexOutOfBoundsException.java rename src/main/java/com/relogiclabs/jschema/exception/{CommonException.java => BaseRuntimeException.java} (52%) create mode 100644 src/main/java/com/relogiclabs/jschema/exception/DataTypeValidationException.java rename src/main/java/com/relogiclabs/jschema/exception/{DuplicateDefinitionException.java => DuplicateAliasException.java} (50%) create mode 100644 src/main/java/com/relogiclabs/jschema/exception/DuplicateFunctionException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/DuplicateParameterException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/DuplicateVariableException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/FunctionArgumentTypeException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/FunctionArgumentValueException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/FunctionValidationException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/IndexOutOfBoundsException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/InvalidArgumentException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/InvalidContextException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/InvalidLeftValueException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/InvalidReturnTypeException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/InvocationRuntimeException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/MethodArgumentTypeException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/MethodArgumentValueException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/MethodNotFoundException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/MultilevelRuntimeException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/PropertyNotFoundException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/PropertyOrderException.java delete mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptArgumentException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptArrayInsertException.java delete mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptCommonException.java delete mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptFunctionException.java delete mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptInitiatedException.java delete mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptInvocationException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptIteratorException.java delete mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptRuntimeException.java delete mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptTemplateException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptThrowInitiatedException.java delete mode 100644 src/main/java/com/relogiclabs/jschema/exception/ScriptVariableException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/StringIndexOutOfBoundsException.java rename src/main/java/com/relogiclabs/jschema/exception/{JsonSchemaException.java => ValidationException.java} (54%) create mode 100644 src/main/java/com/relogiclabs/jschema/exception/ValueValidationException.java create mode 100644 src/main/java/com/relogiclabs/jschema/exception/VariableNotFoundException.java create mode 100644 src/main/java/com/relogiclabs/jschema/internal/util/ExceptionLevel.java diff --git a/src/main/java/com/relogiclabs/jschema/exception/DefinitionNotFoundException.java b/src/main/java/com/relogiclabs/jschema/exception/AliasNotFoundException.java similarity index 50% rename from src/main/java/com/relogiclabs/jschema/exception/DefinitionNotFoundException.java rename to src/main/java/com/relogiclabs/jschema/exception/AliasNotFoundException.java index c5e729f..2f6a850 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/DefinitionNotFoundException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/AliasNotFoundException.java @@ -2,8 +2,8 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class DefinitionNotFoundException extends CommonException { - public DefinitionNotFoundException(ErrorDetail detail) { +public class AliasNotFoundException extends BaseRuntimeException { + public AliasNotFoundException(ErrorDetail detail) { super(detail); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ArgumentTypeException.java b/src/main/java/com/relogiclabs/jschema/exception/ArgumentTypeException.java new file mode 100644 index 0000000..ac4236f --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/ArgumentTypeException.java @@ -0,0 +1,28 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.type.EValue; + +public class ArgumentTypeException extends InvalidArgumentException { + public ArgumentTypeException(String message) { + super(message); + } + + public ArgumentTypeException(String code, String message, Throwable cause) { + super(code, message, cause); + } + + public ArgumentTypeException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public FunctionArgumentTypeException failWithFunctionException() { + return new FunctionArgumentTypeException(this); + } + + @Override + public MethodArgumentTypeException failWithMethodException(EValue self) { + return new MethodArgumentTypeException(self, this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ArgumentValueException.java b/src/main/java/com/relogiclabs/jschema/exception/ArgumentValueException.java new file mode 100644 index 0000000..20cfde1 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/ArgumentValueException.java @@ -0,0 +1,28 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.type.EValue; + +public class ArgumentValueException extends InvalidArgumentException { + public ArgumentValueException(String message) { + super(message); + } + + public ArgumentValueException(String code, String message, Throwable cause) { + super(code, message, cause); + } + + public ArgumentValueException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public InvalidArgumentException failWithFunctionException() { + return new FunctionArgumentValueException(this); + } + + @Override + public InvalidArgumentException failWithMethodException(EValue self) { + return new MethodArgumentValueException(self, this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ArrayIndexOutOfBoundsException.java b/src/main/java/com/relogiclabs/jschema/exception/ArrayIndexOutOfBoundsException.java new file mode 100644 index 0000000..ab3440a --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/ArrayIndexOutOfBoundsException.java @@ -0,0 +1,9 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; + +public class ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException { + public ArrayIndexOutOfBoundsException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/CommonException.java b/src/main/java/com/relogiclabs/jschema/exception/BaseRuntimeException.java similarity index 52% rename from src/main/java/com/relogiclabs/jschema/exception/CommonException.java rename to src/main/java/com/relogiclabs/jschema/exception/BaseRuntimeException.java index 6ac946c..695e47b 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/CommonException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/BaseRuntimeException.java @@ -2,43 +2,36 @@ import com.relogiclabs.jschema.message.ErrorDetail; import lombok.Getter; - -import java.util.HashMap; -import java.util.Map; +import lombok.Setter; import static java.util.Arrays.copyOfRange; -public class CommonException extends RuntimeException { +@Getter @Setter +public class BaseRuntimeException extends RuntimeException { + // Exception creation method private static final String FAIL_METHOD_PREFIX = "fail"; - @Getter private final String code; - private Map attributes; + private String code; - public CommonException(String code, String message, Throwable cause) { - super(message, cause); - this.code = code; - formatStackTrace(); + public BaseRuntimeException(String message) { + this(null, message, null); } - public CommonException(String code, String message) { + public BaseRuntimeException(String code, String message) { this(code, message, null); } - public CommonException(ErrorDetail detail, Throwable cause) { - this(detail.getCode(), detail.getMessage(), cause); + public BaseRuntimeException(String code, String message, Throwable cause) { + super(message, cause); + this.code = code; } - public CommonException(ErrorDetail detail) { + public BaseRuntimeException(ErrorDetail detail) { this(detail, null); } - public String getAttribute(String name) { - if(attributes == null) return null; - return attributes.get(name); - } - - public void setAttribute(String name, String value) { - if(attributes == null) attributes = new HashMap<>(5); - attributes.put(name, value); + public BaseRuntimeException(ErrorDetail detail, Throwable cause) { + super(detail.getMessage(), cause); + this.code = detail.getCode(); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/exception/ClassInstantiationException.java b/src/main/java/com/relogiclabs/jschema/exception/ClassInstantiationException.java index b1fdc8f..b3650cc 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/ClassInstantiationException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/ClassInstantiationException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class ClassInstantiationException extends CommonException { +public class ClassInstantiationException extends BaseRuntimeException { public ClassInstantiationException(ErrorDetail detail, Throwable cause) { super(detail, cause); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/DataTypeValidationException.java b/src/main/java/com/relogiclabs/jschema/exception/DataTypeValidationException.java new file mode 100644 index 0000000..d5ef41c --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/DataTypeValidationException.java @@ -0,0 +1,22 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ActualDetail; +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.message.ExpectedDetail; +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public class DataTypeValidationException extends ValidationException { + private String typeBaseName; + + public DataTypeValidationException(ErrorDetail error, ExpectedDetail expected, + ActualDetail actual) { + super(error, expected, actual); + } + + public DataTypeValidationException(ErrorDetail error, ExpectedDetail expected, + ActualDetail actual, Throwable cause) { + super(error, expected, actual, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/DateTimeLexerException.java b/src/main/java/com/relogiclabs/jschema/exception/DateTimeLexerException.java index 9cab4a1..b4be57b 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/DateTimeLexerException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/DateTimeLexerException.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.exception; -public class DateTimeLexerException extends CommonException { +public class DateTimeLexerException extends BaseRuntimeException { public DateTimeLexerException(String code, String message, Throwable cause) { super(code, message, cause); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/DuplicateDefinitionException.java b/src/main/java/com/relogiclabs/jschema/exception/DuplicateAliasException.java similarity index 50% rename from src/main/java/com/relogiclabs/jschema/exception/DuplicateDefinitionException.java rename to src/main/java/com/relogiclabs/jschema/exception/DuplicateAliasException.java index 24dd131..fcfb239 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/DuplicateDefinitionException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/DuplicateAliasException.java @@ -2,8 +2,8 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class DuplicateDefinitionException extends CommonException { - public DuplicateDefinitionException(ErrorDetail detail) { +public class DuplicateAliasException extends BaseRuntimeException { + public DuplicateAliasException(ErrorDetail detail) { super(detail); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/DuplicateFunctionException.java b/src/main/java/com/relogiclabs/jschema/exception/DuplicateFunctionException.java new file mode 100644 index 0000000..0c659c2 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/DuplicateFunctionException.java @@ -0,0 +1,23 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +public class DuplicateFunctionException extends MultilevelRuntimeException { + public DuplicateFunctionException(String code, String message) { + super(code, message); + } + + public DuplicateFunctionException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel()) return this; + return new DuplicateFunctionException(formatForSchema(getCode(), + getMessage(), token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/DuplicateImportException.java b/src/main/java/com/relogiclabs/jschema/exception/DuplicateImportException.java index 8a6968b..f4c33de 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/DuplicateImportException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/DuplicateImportException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class DuplicateImportException extends CommonException { +public class DuplicateImportException extends BaseRuntimeException { public DuplicateImportException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/DuplicateParameterException.java b/src/main/java/com/relogiclabs/jschema/exception/DuplicateParameterException.java new file mode 100644 index 0000000..3125472 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/DuplicateParameterException.java @@ -0,0 +1,9 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; + +public class DuplicateParameterException extends BaseRuntimeException { + public DuplicateParameterException(ErrorDetail detail) { + super(detail); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/DuplicatePragmaException.java b/src/main/java/com/relogiclabs/jschema/exception/DuplicatePragmaException.java index bdd9f6b..730d8a8 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/DuplicatePragmaException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/DuplicatePragmaException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class DuplicatePragmaException extends CommonException { +public class DuplicatePragmaException extends BaseRuntimeException { public DuplicatePragmaException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/DuplicatePropertyKeyException.java b/src/main/java/com/relogiclabs/jschema/exception/DuplicatePropertyKeyException.java index c1f9817..48d4f47 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/DuplicatePropertyKeyException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/DuplicatePropertyKeyException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class DuplicatePropertyKeyException extends CommonException { +public class DuplicatePropertyKeyException extends BaseRuntimeException { public DuplicatePropertyKeyException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/DuplicateVariableException.java b/src/main/java/com/relogiclabs/jschema/exception/DuplicateVariableException.java new file mode 100644 index 0000000..f9bb765 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/DuplicateVariableException.java @@ -0,0 +1,23 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +public class DuplicateVariableException extends MultilevelRuntimeException { + public DuplicateVariableException(String code, String message) { + super(code, message); + } + + public DuplicateVariableException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel()) return this; + return new DuplicateVariableException(formatForSchema(getCode(), + getMessage(), token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/FunctionArgumentTypeException.java b/src/main/java/com/relogiclabs/jschema/exception/FunctionArgumentTypeException.java new file mode 100644 index 0000000..7495770 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/FunctionArgumentTypeException.java @@ -0,0 +1,24 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +public class FunctionArgumentTypeException extends ArgumentTypeException { + FunctionArgumentTypeException(ArgumentTypeException cause) { + super(cause.getCode(), cause.getMessage(), cause); + setParameter(cause.getParameter()); + } + + public FunctionArgumentTypeException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel() || getSubject() == null) return this; + return new FunctionArgumentTypeException(formatForSchema(getCode(), + formatFunctionMessage(), token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/FunctionArgumentValueException.java b/src/main/java/com/relogiclabs/jschema/exception/FunctionArgumentValueException.java new file mode 100644 index 0000000..2250dee --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/FunctionArgumentValueException.java @@ -0,0 +1,28 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +public class FunctionArgumentValueException extends ArgumentValueException { + FunctionArgumentValueException(ArgumentValueException cause) { + super(cause.getCode(), cause.getMessage(), cause); + setParameter(cause.getParameter()); + } + + public FunctionArgumentValueException(ErrorDetail detail) { + super(detail, null); + } + + public FunctionArgumentValueException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public MultilevelRuntimeException translate(Token token) { + if(isHighLevel() || getSubject() == null) return this; + return new FunctionArgumentValueException(formatForSchema(getCode(), + formatFunctionMessage(), token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/FunctionNotFoundException.java b/src/main/java/com/relogiclabs/jschema/exception/FunctionNotFoundException.java index 2dc29d6..f2095da 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/FunctionNotFoundException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/FunctionNotFoundException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class FunctionNotFoundException extends CommonException { +public class FunctionNotFoundException extends BaseRuntimeException { public FunctionNotFoundException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/FunctionValidationException.java b/src/main/java/com/relogiclabs/jschema/exception/FunctionValidationException.java new file mode 100644 index 0000000..2cdb450 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/FunctionValidationException.java @@ -0,0 +1,22 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ActualDetail; +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.message.ExpectedDetail; +import com.relogiclabs.jschema.node.JNode; + +public class FunctionValidationException extends ValidationException { + public FunctionValidationException(ErrorDetail error, JNode caller, JNode target) { + super(error, caller, target); + } + + public FunctionValidationException(ErrorDetail error, ExpectedDetail expected, + ActualDetail actual) { + super(error, expected, actual); + } + + public FunctionValidationException(ErrorDetail error, ExpectedDetail expected, + ActualDetail actual, Throwable cause) { + super(error, expected, actual, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/IndexOutOfBoundsException.java b/src/main/java/com/relogiclabs/jschema/exception/IndexOutOfBoundsException.java new file mode 100644 index 0000000..4859ca1 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/IndexOutOfBoundsException.java @@ -0,0 +1,9 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; + +public class IndexOutOfBoundsException extends BaseRuntimeException { + public IndexOutOfBoundsException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidArgumentException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidArgumentException.java new file mode 100644 index 0000000..d1a2e3c --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidArgumentException.java @@ -0,0 +1,41 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.type.EValue; +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public abstract class InvalidArgumentException extends InvocationRuntimeException { + private String parameter; + + public InvalidArgumentException(String message) { + super(message); + } + + public InvalidArgumentException(String code, String message, Throwable cause) { + super(code, message, cause); + } + + public InvalidArgumentException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + public void setContext(String code, String parameter) { + setCode(code); + this.parameter = parameter; + } + + public abstract InvalidArgumentException failWithFunctionException(); + public abstract InvalidArgumentException failWithMethodException(EValue self); + + protected String formatMethodMessage(EValue self) { + return getMessage() + " for parameter '" + parameter + + "' in method '" + getSubject() + "' of " + self.getType(); + } + + protected String formatFunctionMessage() { + return getMessage() + " for parameter '" + parameter + + "' of function '" + getSubject() + "'"; + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidContextException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidContextException.java new file mode 100644 index 0000000..cdba1a0 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidContextException.java @@ -0,0 +1,31 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import lombok.Getter; +import lombok.Setter; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.internal.util.CommonHelper.nonNullFrom; +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +@Getter @Setter +public class InvalidContextException extends InvocationRuntimeException { + private Token mainToken; + + public InvalidContextException(String code, String message, Token mainToken) { + super(code, message); + this.mainToken = mainToken; + } + + public InvalidContextException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public MultilevelRuntimeException translate(Token token) { + if(isHighLevel() || getSubject() == null) return this; + return new InvalidContextException(formatForSchema(getCode(), + getMessage() + " for function '" + getSubject() + "'", + nonNullFrom(mainToken, token)), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidDataTypeException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidDataTypeException.java index e59fd94..05df944 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/InvalidDataTypeException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidDataTypeException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class InvalidDataTypeException extends CommonException { +public class InvalidDataTypeException extends BaseRuntimeException { public InvalidDataTypeException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidDateTimeException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidDateTimeException.java index 092357a..8191421 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/InvalidDateTimeException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidDateTimeException.java @@ -1,11 +1,19 @@ package com.relogiclabs.jschema.exception; -public class InvalidDateTimeException extends CommonException { - public InvalidDateTimeException(String code, String message) { - super(code, message); +import com.relogiclabs.jschema.internal.time.DateTimeContext; +import lombok.Getter; + +@Getter +public class InvalidDateTimeException extends BaseRuntimeException { + private final DateTimeContext context; + + public InvalidDateTimeException(String code, String message, DateTimeContext context) { + this(code, message, context, null); } - public InvalidDateTimeException(String code, String message, Throwable cause) { + public InvalidDateTimeException(String code, String message, DateTimeContext context, + Throwable cause) { super(code, message, cause); + this.context = context; } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidFunctionException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidFunctionException.java index 236d916..2cbd163 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/InvalidFunctionException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidFunctionException.java @@ -2,11 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class InvalidFunctionException extends CommonException { - public InvalidFunctionException(String code, String message) { - super(code, message); - } - +public class InvalidFunctionException extends BaseRuntimeException { public InvalidFunctionException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidImportException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidImportException.java index 0146262..584825b 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/InvalidImportException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidImportException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class InvalidImportException extends CommonException { +public class InvalidImportException extends BaseRuntimeException { public InvalidImportException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidLeftValueException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidLeftValueException.java new file mode 100644 index 0000000..87c1bb5 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidLeftValueException.java @@ -0,0 +1,9 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; + +public class InvalidLeftValueException extends BaseRuntimeException { + public InvalidLeftValueException(ErrorDetail detail) { + super(detail); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidPragmaValueException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidPragmaValueException.java index d7985c3..3475d06 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/InvalidPragmaValueException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidPragmaValueException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class InvalidPragmaValueException extends CommonException { +public class InvalidPragmaValueException extends BaseRuntimeException { public InvalidPragmaValueException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidReceiverStateException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidReceiverStateException.java index a1ec6cf..5ad4798 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/InvalidReceiverStateException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidReceiverStateException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class InvalidReceiverStateException extends CommonException { +public class InvalidReceiverStateException extends BaseRuntimeException { public InvalidReceiverStateException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvalidReturnTypeException.java b/src/main/java/com/relogiclabs/jschema/exception/InvalidReturnTypeException.java new file mode 100644 index 0000000..ab6b6d9 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/InvalidReturnTypeException.java @@ -0,0 +1,13 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; + +public class InvalidReturnTypeException extends BaseRuntimeException { + public InvalidReturnTypeException(String code, String message) { + super(code, message); + } + + public InvalidReturnTypeException(ErrorDetail detail) { + super(detail); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/InvocationRuntimeException.java b/src/main/java/com/relogiclabs/jschema/exception/InvocationRuntimeException.java new file mode 100644 index 0000000..f093c0d --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/InvocationRuntimeException.java @@ -0,0 +1,36 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import lombok.Getter; +import lombok.Setter; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +@Getter @Setter +public class InvocationRuntimeException extends MultilevelRuntimeException { + private String subject; + + public InvocationRuntimeException(String message) { + super(message); + } + + public InvocationRuntimeException(String code, String message) { + super(code, message); + } + + public InvocationRuntimeException(String code, String message, Throwable cause) { + super(code, message, cause); + } + + public InvocationRuntimeException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel() || subject == null) return this; + return new InvocationRuntimeException(formatForSchema(getCode(), + getMessage() + " '" + subject + "'", token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/JsonLexerException.java b/src/main/java/com/relogiclabs/jschema/exception/JsonLexerException.java index 4e037d8..907c36b 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/JsonLexerException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/JsonLexerException.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.exception; -public class JsonLexerException extends CommonException { +public class JsonLexerException extends BaseRuntimeException { public JsonLexerException(String code, String message, Throwable cause) { super(code, message, cause); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/JsonParserException.java b/src/main/java/com/relogiclabs/jschema/exception/JsonParserException.java index c184a91..95df37d 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/JsonParserException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/JsonParserException.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.exception; -public class JsonParserException extends CommonException { +public class JsonParserException extends BaseRuntimeException { public JsonParserException(String code, String message, Throwable cause) { super(code, message, cause); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/MethodArgumentTypeException.java b/src/main/java/com/relogiclabs/jschema/exception/MethodArgumentTypeException.java new file mode 100644 index 0000000..368bd0b --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/MethodArgumentTypeException.java @@ -0,0 +1,31 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.type.EValue; +import lombok.Getter; +import lombok.Setter; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +@Getter @Setter +public class MethodArgumentTypeException extends ArgumentTypeException { + private EValue self; + + MethodArgumentTypeException(EValue self, ArgumentTypeException cause) { + super(cause.getCode(), cause.getMessage(), cause); + setParameter(cause.getParameter()); + this.self = self; + } + + public MethodArgumentTypeException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel() || getSubject() == null) return this; + return new MethodArgumentTypeException(formatForSchema(getCode(), + formatMethodMessage(self), token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/MethodArgumentValueException.java b/src/main/java/com/relogiclabs/jschema/exception/MethodArgumentValueException.java new file mode 100644 index 0000000..ecbdb94 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/MethodArgumentValueException.java @@ -0,0 +1,28 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.type.EValue; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +public class MethodArgumentValueException extends ArgumentValueException { + private EValue self; + + MethodArgumentValueException(EValue self, ArgumentValueException cause) { + super(cause.getCode(), cause.getMessage(), cause); + setParameter(cause.getParameter()); + this.self = self; + } + + public MethodArgumentValueException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel() || getSubject() == null) return this; + return new MethodArgumentValueException(formatForSchema(getCode(), + formatMethodMessage(self), token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/MethodNotFoundException.java b/src/main/java/com/relogiclabs/jschema/exception/MethodNotFoundException.java new file mode 100644 index 0000000..fe44ad8 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/MethodNotFoundException.java @@ -0,0 +1,29 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.type.EType; +import lombok.Getter; +import lombok.Setter; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +@Getter @Setter +public class MethodNotFoundException extends MultilevelRuntimeException { + private EType type; + + public MethodNotFoundException(String code, String message) { + super(code, message); + } + + public MethodNotFoundException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel() || type == null) return this; + return new MethodNotFoundException(formatForSchema(getCode(), + getMessage() + " for " + type, token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/MisplacedOptionalException.java b/src/main/java/com/relogiclabs/jschema/exception/MisplacedOptionalException.java index 5e2e98c..77344bc 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/MisplacedOptionalException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/MisplacedOptionalException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class MisplacedOptionalException extends CommonException { +public class MisplacedOptionalException extends BaseRuntimeException { public MisplacedOptionalException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/MultilevelRuntimeException.java b/src/main/java/com/relogiclabs/jschema/exception/MultilevelRuntimeException.java new file mode 100644 index 0000000..d17a862 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/MultilevelRuntimeException.java @@ -0,0 +1,46 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.internal.util.ExceptionLevel; +import com.relogiclabs.jschema.message.ErrorDetail; +import lombok.Getter; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.internal.util.ExceptionLevel.HIGH_LEVEL; +import static com.relogiclabs.jschema.internal.util.ExceptionLevel.LOW_LEVEL; + +@Getter +public abstract class MultilevelRuntimeException extends BaseRuntimeException { + private final ExceptionLevel level; + + public MultilevelRuntimeException(String message) { + this(null, message, null); + } + + public MultilevelRuntimeException(String code, String message) { + this(code, message, null); + } + + public MultilevelRuntimeException(String code, String message, Throwable cause) { + super(code, message, cause); + this.level = LOW_LEVEL; + } + + public MultilevelRuntimeException(ErrorDetail detail) { + this(detail, null); + } + + public MultilevelRuntimeException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + this.level = HIGH_LEVEL; + } + + public abstract RuntimeException translate(Token token); + + public boolean isLowLevel() { + return level == LOW_LEVEL; + } + + public boolean isHighLevel() { + return level == HIGH_LEVEL; + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/NoClassFoundException.java b/src/main/java/com/relogiclabs/jschema/exception/NoClassFoundException.java index ca504e6..b0b25da 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/NoClassFoundException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/NoClassFoundException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class NoClassFoundException extends CommonException { +public class NoClassFoundException extends BaseRuntimeException { public NoClassFoundException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/PragmaNotFoundException.java b/src/main/java/com/relogiclabs/jschema/exception/PragmaNotFoundException.java index 533ce21..77077fa 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/PragmaNotFoundException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/PragmaNotFoundException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class PragmaNotFoundException extends CommonException { +public class PragmaNotFoundException extends BaseRuntimeException { public PragmaNotFoundException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/PropertyNotFoundException.java b/src/main/java/com/relogiclabs/jschema/exception/PropertyNotFoundException.java new file mode 100644 index 0000000..80db7cb --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/PropertyNotFoundException.java @@ -0,0 +1,9 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; + +public class PropertyNotFoundException extends BaseRuntimeException { + public PropertyNotFoundException(ErrorDetail detail) { + super(detail); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/PropertyOrderException.java b/src/main/java/com/relogiclabs/jschema/exception/PropertyOrderException.java new file mode 100644 index 0000000..b26effb --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/PropertyOrderException.java @@ -0,0 +1,14 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.node.JProperty; +import lombok.Getter; + +@Getter +public class PropertyOrderException extends BaseRuntimeException { + private final JProperty failOn; + + public PropertyOrderException(String code, String message, JProperty failOn) { + super(code, message); + this.failOn = failOn; + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ReceiverNotFoundException.java b/src/main/java/com/relogiclabs/jschema/exception/ReceiverNotFoundException.java index 950d6ea..0d97870 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/ReceiverNotFoundException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/ReceiverNotFoundException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class ReceiverNotFoundException extends CommonException { +public class ReceiverNotFoundException extends BaseRuntimeException { public ReceiverNotFoundException(ErrorDetail detail) { super(detail); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/SchemaLexerException.java b/src/main/java/com/relogiclabs/jschema/exception/SchemaLexerException.java index 27744f0..8a9b99a 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/SchemaLexerException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/SchemaLexerException.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.exception; -public class SchemaLexerException extends CommonException { +public class SchemaLexerException extends BaseRuntimeException { public SchemaLexerException(String code, String message, Throwable cause) { super(code, message, cause); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/SchemaParserException.java b/src/main/java/com/relogiclabs/jschema/exception/SchemaParserException.java index 5f958ab..bf4c8a6 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/SchemaParserException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/SchemaParserException.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.exception; -public class SchemaParserException extends CommonException { +public class SchemaParserException extends BaseRuntimeException { public SchemaParserException(String code, String message, Throwable cause) { super(code, message, cause); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptArgumentException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptArgumentException.java deleted file mode 100644 index 2133cb3..0000000 --- a/src/main/java/com/relogiclabs/jschema/exception/ScriptArgumentException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.relogiclabs.jschema.exception; - -public class ScriptArgumentException extends ScriptTemplateException { - public ScriptArgumentException(String code, String template) { - super(code, template); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptArrayInsertException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptArrayInsertException.java new file mode 100644 index 0000000..5d9f273 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/ScriptArrayInsertException.java @@ -0,0 +1,23 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +public class ScriptArrayInsertException extends MultilevelRuntimeException { + public ScriptArrayInsertException(String code, String message) { + super(code, message); + } + + public ScriptArrayInsertException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel()) return this; + return new ScriptArrayInsertException(formatForSchema(getCode(), + getMessage(), token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptCommonException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptCommonException.java deleted file mode 100644 index 4eeb258..0000000 --- a/src/main/java/com/relogiclabs/jschema/exception/ScriptCommonException.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.relogiclabs.jschema.exception; - -import com.relogiclabs.jschema.message.ErrorDetail; - -public class ScriptCommonException extends CommonException { - public ScriptCommonException(String code, String message, Throwable cause) { - super(code, message, cause); - } - - public ScriptCommonException(String code, String message) { - super(code, message); - } - - public ScriptCommonException(ErrorDetail detail, Throwable cause) { - super(detail, cause); - } - - public ScriptCommonException(ErrorDetail detail) { - super(detail); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptFunctionException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptFunctionException.java deleted file mode 100644 index b73a78f..0000000 --- a/src/main/java/com/relogiclabs/jschema/exception/ScriptFunctionException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.relogiclabs.jschema.exception; - -public class ScriptFunctionException extends ScriptCommonException { - public ScriptFunctionException(String code, String message) { - super(code, message); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptInitiatedException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptInitiatedException.java deleted file mode 100644 index cf23e9b..0000000 --- a/src/main/java/com/relogiclabs/jschema/exception/ScriptInitiatedException.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.relogiclabs.jschema.exception; - -import com.relogiclabs.jschema.message.ErrorDetail; - -public class ScriptInitiatedException extends ScriptRuntimeException { - public ScriptInitiatedException(String code, String message) { - super(code, message); - } - - public ScriptInitiatedException(ErrorDetail detail) { - super(detail); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptInvocationException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptInvocationException.java deleted file mode 100644 index d79e1cf..0000000 --- a/src/main/java/com/relogiclabs/jschema/exception/ScriptInvocationException.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.relogiclabs.jschema.exception; - -import org.antlr.v4.runtime.Token; - -public class ScriptInvocationException extends ScriptTemplateException { - private final Token token; - - public ScriptInvocationException(String code, String template, Token token) { - super(code, template); - this.token = token; - } - - public ScriptInvocationException(String code, String template) { - this(code, template, null); - } - - public Token getToken(Token defaultToken) { - return token != null ? token : defaultToken; - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptIteratorException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptIteratorException.java new file mode 100644 index 0000000..6112e82 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/ScriptIteratorException.java @@ -0,0 +1,23 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +public class ScriptIteratorException extends MultilevelRuntimeException { + public ScriptIteratorException(String code, String message) { + super(code, message); + } + + public ScriptIteratorException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel()) return this; + return new ScriptIteratorException(formatForSchema(getCode(), + getMessage(), token), this); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptOperationException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptOperationException.java index 359224f..54ef8c7 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/ScriptOperationException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/ScriptOperationException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class ScriptOperationException extends ScriptRuntimeException { +public class ScriptOperationException extends BaseRuntimeException { public ScriptOperationException(ErrorDetail detail, Throwable cause) { super(detail, cause); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptRuntimeException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptRuntimeException.java deleted file mode 100644 index 722cff1..0000000 --- a/src/main/java/com/relogiclabs/jschema/exception/ScriptRuntimeException.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.relogiclabs.jschema.exception; - -import com.relogiclabs.jschema.message.ErrorDetail; - -public class ScriptRuntimeException extends ScriptCommonException { - - public ScriptRuntimeException(String code, String message) { - super(code, message); - } - - public ScriptRuntimeException(String code, String message, Throwable cause) { - super(code, message, cause); - } - - public ScriptRuntimeException(ErrorDetail detail) { - super(detail); - } - - public ScriptRuntimeException(ErrorDetail detail, Throwable cause) { - super(detail, cause); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptTemplateException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptTemplateException.java deleted file mode 100644 index f712317..0000000 --- a/src/main/java/com/relogiclabs/jschema/exception/ScriptTemplateException.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.relogiclabs.jschema.exception; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class ScriptTemplateException extends ScriptCommonException { - private static final Pattern TEMPLATE_PATTERN = Pattern.compile(" '?%s'?"); - private final String template; - - public ScriptTemplateException(String code, String template) { - super(code, toMessage(template)); - this.template = template; - } - - public String getMessage(Object... args) { - return template.formatted(args); - } - - private static String toMessage(String template) { - Matcher matcher = TEMPLATE_PATTERN.matcher(template); - if(matcher.find()) return matcher.replaceAll(""); - return template; - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptThrowInitiatedException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptThrowInitiatedException.java new file mode 100644 index 0000000..185116d --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/ScriptThrowInitiatedException.java @@ -0,0 +1,9 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; + +public class ScriptThrowInitiatedException extends BaseRuntimeException { + public ScriptThrowInitiatedException(ErrorDetail detail) { + super(detail); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ScriptVariableException.java b/src/main/java/com/relogiclabs/jschema/exception/ScriptVariableException.java deleted file mode 100644 index ee22f15..0000000 --- a/src/main/java/com/relogiclabs/jschema/exception/ScriptVariableException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.relogiclabs.jschema.exception; - -public class ScriptVariableException extends ScriptCommonException { - public ScriptVariableException(String code, String message) { - super(code, message); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/StringIndexOutOfBoundsException.java b/src/main/java/com/relogiclabs/jschema/exception/StringIndexOutOfBoundsException.java new file mode 100644 index 0000000..3cd5cd1 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/StringIndexOutOfBoundsException.java @@ -0,0 +1,9 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; + +public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException { + public StringIndexOutOfBoundsException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/SystemOperationException.java b/src/main/java/com/relogiclabs/jschema/exception/SystemOperationException.java index 8ae35d7..1504e66 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/SystemOperationException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/SystemOperationException.java @@ -2,7 +2,7 @@ import com.relogiclabs.jschema.message.ErrorDetail; -public class SystemOperationException extends ScriptRuntimeException { +public class SystemOperationException extends BaseRuntimeException { public SystemOperationException(ErrorDetail detail, Throwable cause) { super(detail, cause); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/TargetInvocationException.java b/src/main/java/com/relogiclabs/jschema/exception/TargetInvocationException.java index 6dba481..4b8e7aa 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/TargetInvocationException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/TargetInvocationException.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.exception; -public class TargetInvocationException extends CommonException { +public class TargetInvocationException extends BaseRuntimeException { public TargetInvocationException(String code, String message, Throwable cause) { super(code, message, cause); } diff --git a/src/main/java/com/relogiclabs/jschema/exception/UpdateNotSupportedException.java b/src/main/java/com/relogiclabs/jschema/exception/UpdateNotSupportedException.java index 6fd4c85..14f52eb 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/UpdateNotSupportedException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/UpdateNotSupportedException.java @@ -1,7 +1,27 @@ package com.relogiclabs.jschema.exception; -public class UpdateNotSupportedException extends CommonException { +import com.relogiclabs.jschema.message.ErrorDetail; +import org.antlr.v4.runtime.Token; + +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +public class UpdateNotSupportedException extends MultilevelRuntimeException { public UpdateNotSupportedException(String code, String message) { super(code, message); } + + public UpdateNotSupportedException(ErrorDetail detail) { + super(detail); + } + + public UpdateNotSupportedException(ErrorDetail detail, Throwable cause) { + super(detail, cause); + } + + @Override + public RuntimeException translate(Token token) { + if(isHighLevel()) return this; + return new UpdateNotSupportedException(formatForSchema(getCode(), + getMessage(), token), this); + } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/JsonSchemaException.java b/src/main/java/com/relogiclabs/jschema/exception/ValidationException.java similarity index 54% rename from src/main/java/com/relogiclabs/jschema/exception/JsonSchemaException.java rename to src/main/java/com/relogiclabs/jschema/exception/ValidationException.java index 2a07e0b..8a2d3db 100644 --- a/src/main/java/com/relogiclabs/jschema/exception/JsonSchemaException.java +++ b/src/main/java/com/relogiclabs/jschema/exception/ValidationException.java @@ -3,23 +3,31 @@ import com.relogiclabs.jschema.message.ActualDetail; import com.relogiclabs.jschema.message.ErrorDetail; import com.relogiclabs.jschema.message.ExpectedDetail; -import com.relogiclabs.jschema.tree.Context; +import com.relogiclabs.jschema.node.JNode; import lombok.Getter; import static com.relogiclabs.jschema.internal.util.CommonHelper.nonNullFrom; @Getter -public class JsonSchemaException extends ScriptRuntimeException { +public class ValidationException extends BaseRuntimeException { private final ErrorDetail error; private final ExpectedDetail expected; private final ActualDetail actual; - public JsonSchemaException(ErrorDetail error, ExpectedDetail expected, ActualDetail actual) { + public ValidationException(ErrorDetail error, JNode schemaNode, JNode jsonNode) { + super(error); + this.error = error; + this.expected = new ExpectedDetail(schemaNode, null); + this.actual = new ActualDetail(jsonNode, null); + } + + public ValidationException(ErrorDetail error, ExpectedDetail expected, + ActualDetail actual) { this(error, expected, actual, null); } - public JsonSchemaException(ErrorDetail error, ExpectedDetail expected, - ActualDetail actual, Throwable cause) { + public ValidationException(ErrorDetail error, ExpectedDetail expected, + ActualDetail actual, Throwable cause) { super(error.getCode(), formatMessage(error, expected, actual), cause); this.error = error; this.expected = expected; @@ -27,8 +35,8 @@ public JsonSchemaException(ErrorDetail error, ExpectedDetail expected, } private static String formatMessage(ErrorDetail error, ExpectedDetail expected, - ActualDetail actual) { - Context context = nonNullFrom(expected.getContext(), actual.getContext()); + ActualDetail actual) { + var context = nonNullFrom(expected.getContext(), actual.getContext()); return context.getRuntime().getMessageFormatter().format(error, expected, actual); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/ValueValidationException.java b/src/main/java/com/relogiclabs/jschema/exception/ValueValidationException.java new file mode 100644 index 0000000..9c3f397 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/ValueValidationException.java @@ -0,0 +1,17 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ActualDetail; +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.message.ExpectedDetail; + +public class ValueValidationException extends ValidationException { + public ValueValidationException(ErrorDetail error, ExpectedDetail expected, + ActualDetail actual) { + super(error, expected, actual); + } + + public ValueValidationException(ErrorDetail error, ExpectedDetail expected, + ActualDetail actual, Throwable cause) { + super(error, expected, actual, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/exception/VariableNotFoundException.java b/src/main/java/com/relogiclabs/jschema/exception/VariableNotFoundException.java new file mode 100644 index 0000000..b895bcb --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/exception/VariableNotFoundException.java @@ -0,0 +1,9 @@ +package com.relogiclabs.jschema.exception; + +import com.relogiclabs.jschema.message.ErrorDetail; + +public class VariableNotFoundException extends BaseRuntimeException { + public VariableNotFoundException(ErrorDetail detail) { + super(detail); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/util/ExceptionLevel.java b/src/main/java/com/relogiclabs/jschema/internal/util/ExceptionLevel.java new file mode 100644 index 0000000..f7f8299 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/internal/util/ExceptionLevel.java @@ -0,0 +1,5 @@ +package com.relogiclabs.jschema.internal.util; + +public enum ExceptionLevel { + LOW_LEVEL, HIGH_LEVEL +} \ No newline at end of file From 779acd14f3d0fed9e197d0ed4c0eed884de73df4 Mon Sep 17 00:00:00 2001 From: Zahid Hossain <60911932+zhossain-info@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:51:00 +0600 Subject: [PATCH 2/7] Update exception handling for script --- .../internal/engine/ScriptErrorHelper.java | 164 ++++++------ .../jschema/internal/engine/ScriptScope.java | 34 +-- .../internal/engine/ScriptTreeVisitor1.java | 82 +++--- .../internal/engine/ScriptTreeVisitor2.java | 238 +++++++++--------- .../internal/engine/ScriptTreeVisitor3.java | 194 +++++++------- .../{ArrayLibrary.java => ArrayMethods.java} | 51 ++-- .../internal/library/CommonLibrary.java | 67 ----- .../internal/library/CommonMethods.java | 55 ++++ .../internal/library/LibraryFunctions1.java | 41 +++ .../internal/library/LibraryFunctions2.java | 214 ++++++++++++++++ ...ptLibrary3.java => LibraryFunctions3.java} | 34 +-- .../internal/library/LibraryHelper.java | 57 +++++ ...{NumberLibrary.java => NumberMethods.java} | 38 +-- ...{ObjectLibrary.java => ObjectMethods.java} | 18 +- .../internal/library/ScriptLibrary1.java | 73 ------ .../internal/library/ScriptLibrary2.java | 109 -------- .../internal/library/StringLibrary.java | 67 ----- .../internal/library/StringMethods.java | 77 ++++++ .../jschema/internal/script/GArray.java | 16 +- .../jschema/internal/script/GFunction.java | 4 +- .../jschema/internal/script/GIterator.java | 6 +- .../jschema/internal/script/GObject.java | 4 +- 22 files changed, 890 insertions(+), 753 deletions(-) rename src/main/java/com/relogiclabs/jschema/internal/library/{ArrayLibrary.java => ArrayMethods.java} (63%) delete mode 100644 src/main/java/com/relogiclabs/jschema/internal/library/CommonLibrary.java create mode 100644 src/main/java/com/relogiclabs/jschema/internal/library/CommonMethods.java create mode 100644 src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions1.java create mode 100644 src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions2.java rename src/main/java/com/relogiclabs/jschema/internal/library/{ScriptLibrary3.java => LibraryFunctions3.java} (57%) create mode 100644 src/main/java/com/relogiclabs/jschema/internal/library/LibraryHelper.java rename src/main/java/com/relogiclabs/jschema/internal/library/{NumberLibrary.java => NumberMethods.java} (66%) rename src/main/java/com/relogiclabs/jschema/internal/library/{ObjectLibrary.java => ObjectMethods.java} (65%) delete mode 100644 src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary1.java delete mode 100644 src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary2.java delete mode 100644 src/main/java/com/relogiclabs/jschema/internal/library/StringLibrary.java create mode 100644 src/main/java/com/relogiclabs/jschema/internal/library/StringMethods.java diff --git a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptErrorHelper.java b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptErrorHelper.java index 961933e..fd975c8 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptErrorHelper.java +++ b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptErrorHelper.java @@ -1,10 +1,18 @@ package com.relogiclabs.jschema.internal.engine; -import com.relogiclabs.jschema.exception.InvalidFunctionException; -import com.relogiclabs.jschema.exception.ScriptInvocationException; +import com.relogiclabs.jschema.exception.ArrayIndexOutOfBoundsException; +import com.relogiclabs.jschema.exception.DuplicateParameterException; +import com.relogiclabs.jschema.exception.FunctionNotFoundException; +import com.relogiclabs.jschema.exception.InvalidContextException; +import com.relogiclabs.jschema.exception.InvalidLeftValueException; +import com.relogiclabs.jschema.exception.InvalidReturnTypeException; +import com.relogiclabs.jschema.exception.InvocationRuntimeException; +import com.relogiclabs.jschema.exception.PropertyNotFoundException; import com.relogiclabs.jschema.exception.ScriptOperationException; -import com.relogiclabs.jschema.exception.ScriptRuntimeException; +import com.relogiclabs.jschema.exception.StringIndexOutOfBoundsException; import com.relogiclabs.jschema.exception.SystemOperationException; +import com.relogiclabs.jschema.exception.UpdateNotSupportedException; +import com.relogiclabs.jschema.exception.VariableNotFoundException; import com.relogiclabs.jschema.internal.script.GRange; import com.relogiclabs.jschema.type.EArray; import com.relogiclabs.jschema.type.EInteger; @@ -14,21 +22,21 @@ import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; -import static com.relogiclabs.jschema.message.ErrorCode.AIDX01; -import static com.relogiclabs.jschema.message.ErrorCode.AIDX02; -import static com.relogiclabs.jschema.message.ErrorCode.ARNG01; -import static com.relogiclabs.jschema.message.ErrorCode.ARNG02; -import static com.relogiclabs.jschema.message.ErrorCode.ARNG03; -import static com.relogiclabs.jschema.message.ErrorCode.CALR01; -import static com.relogiclabs.jschema.message.ErrorCode.FNVK01; -import static com.relogiclabs.jschema.message.ErrorCode.FPRM01; -import static com.relogiclabs.jschema.message.ErrorCode.RETN01; -import static com.relogiclabs.jschema.message.ErrorCode.SIDX01; -import static com.relogiclabs.jschema.message.ErrorCode.SIDX02; -import static com.relogiclabs.jschema.message.ErrorCode.SRNG01; -import static com.relogiclabs.jschema.message.ErrorCode.SRNG02; -import static com.relogiclabs.jschema.message.ErrorCode.SRNG03; -import static com.relogiclabs.jschema.message.ErrorCode.TRGT01; +import static com.relogiclabs.jschema.message.ErrorCode.ARRIDX01; +import static com.relogiclabs.jschema.message.ErrorCode.ARRIDX02; +import static com.relogiclabs.jschema.message.ErrorCode.ARRRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.ARRRNG02; +import static com.relogiclabs.jschema.message.ErrorCode.ARRRNG03; +import static com.relogiclabs.jschema.message.ErrorCode.CALRSE01; +import static com.relogiclabs.jschema.message.ErrorCode.FNSNVK01; +import static com.relogiclabs.jschema.message.ErrorCode.PARMSF01; +import static com.relogiclabs.jschema.message.ErrorCode.RETNSE02; +import static com.relogiclabs.jschema.message.ErrorCode.STRIDX01; +import static com.relogiclabs.jschema.message.ErrorCode.STRIDX02; +import static com.relogiclabs.jschema.message.ErrorCode.STRRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.STRRNG02; +import static com.relogiclabs.jschema.message.ErrorCode.STRRNG03; +import static com.relogiclabs.jschema.message.ErrorCode.TRGTSE01; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; public final class ScriptErrorHelper { @@ -59,29 +67,29 @@ private ScriptErrorHelper() { throw new UnsupportedOperationException("This class is not intended for instantiation"); } - static InvalidFunctionException failOnDuplicateParameterName(TerminalNode node) { - return new InvalidFunctionException(formatForSchema(FPRM01, + static DuplicateParameterException failOnDuplicateParameterName(TerminalNode node) { + return new DuplicateParameterException(formatForSchema(PARMSF01, "Duplicate parameter name '" + node.getText() + "'", node.getSymbol())); } - static ScriptRuntimeException failOnInvalidReturnType(EValue value, Token token) { - return new ScriptRuntimeException(formatForSchema(RETN01, + static InvalidReturnTypeException failOnInvalidReturnType(EValue value, Token token) { + return new InvalidReturnTypeException(formatForSchema(RETNSE02, "Invalid return type " + value.getType() + " for constraint function", token)); } - static ScriptRuntimeException failOnPropertyNotExist(String code, EObject object, + static PropertyNotFoundException failOnPropertyNotFound(String code, EObject object, String property, Token token) { - return new ScriptRuntimeException(formatForSchema(code, "Property '" + property - + "' not exist in " + object.getType(), token)); + return new PropertyNotFoundException(formatForSchema(code, "Property '" + property + + "' not found in " + object.getType(), token)); } static RuntimeException failOnIndexOutOfBounds(EString string, EInteger index, Token token, RuntimeException cause) { var indexValue = index.getValue(); - if(indexValue < 0) return new ScriptRuntimeException(formatForSchema(SIDX02, + if(indexValue < 0) return new StringIndexOutOfBoundsException(formatForSchema(STRIDX02, "Invalid negative index " + index + " for string starts at 0", token), cause); var length = string.length(); - if(indexValue >= length) return new ScriptRuntimeException(formatForSchema(SIDX01, + if(indexValue >= length) return new StringIndexOutOfBoundsException(formatForSchema(STRIDX01, "Index " + index + " out of bounds for string length " + length, token), cause); return cause; } @@ -89,89 +97,89 @@ static RuntimeException failOnIndexOutOfBounds(EString string, EInteger index, static RuntimeException failOnIndexOutOfBounds(EArray array, EInteger index, Token token, RuntimeException cause) { var indexValue = index.getValue(); - if(indexValue < 0) return new ScriptRuntimeException(formatForSchema(AIDX02, + if(indexValue < 0) return new ArrayIndexOutOfBoundsException(formatForSchema(ARRIDX02, "Invalid negative index " + index + " for array starts at 0", token), cause); var size = array.size(); - if(indexValue >= size) return new ScriptRuntimeException(formatForSchema(AIDX01, + if(indexValue >= size) return new ArrayIndexOutOfBoundsException(formatForSchema(ARRIDX01, "Index " + index + " out of bounds for array length " + size, token), cause); return cause; } - static RuntimeException failOnInvalidRangeIndex(EString string, GRange range, Token token, + static RuntimeException failOnInvalidIndexRange(EString string, GRange range, Token token, RuntimeException cause) { int length = string.length(), start = range.getStart(length), end = range.getEnd(length); - if(start < 0 || start > length) throw new ScriptRuntimeException(formatForSchema(SRNG01, - "Range start index " + start + " out of bounds for string length " + length, - token), cause); - if(end < 0 || end > length) return new ScriptRuntimeException(formatForSchema(SRNG02, - "Range end index " + end + " out of bounds for string length " + length, - token), cause); - if(start > end) return new ScriptRuntimeException(formatForSchema(SRNG03, + if(start < 0 || start > length) + return new StringIndexOutOfBoundsException(formatForSchema(STRRNG01, + "Range start index " + start + " out of bounds for string length " + length, + token), cause); + if(end < 0 || end > length) + return new StringIndexOutOfBoundsException(formatForSchema(STRRNG02, + "Range end index " + end + " out of bounds for string length " + length, + token), cause); + if(start > end) return new StringIndexOutOfBoundsException(formatForSchema(STRRNG03, "Range start index " + start + " > end index " + end + " for string length " + length, token), cause); return cause; } - static RuntimeException failOnInvalidRangeIndex(EArray array, GRange range, Token token, + static RuntimeException failOnInvalidIndexRange(EArray array, GRange range, Token token, RuntimeException cause) { int size = array.size(), start = range.getStart(size), end = range.getEnd(size); - if(start < 0 || start > size) throw new ScriptRuntimeException(formatForSchema(ARNG01, - "Range start index " + start + " out of bounds for array length " + size, - token), cause); - if(end < 0 || end > size) return new ScriptRuntimeException(formatForSchema(ARNG02, - "Range end index " + end + " out of bounds for array length " + size, token), cause); - if(start > end) return new ScriptRuntimeException(formatForSchema(ARNG03, + if(start < 0 || start > size) + return new ArrayIndexOutOfBoundsException(formatForSchema(ARRRNG01, + "Range start index " + start + " out of bounds for array length " + size, + token), cause); + if(end < 0 || end > size) + return new ArrayIndexOutOfBoundsException(formatForSchema(ARRRNG02, + "Range end index " + end + " out of bounds for array length " + size, + token), cause); + if(start > end) return new ArrayIndexOutOfBoundsException(formatForSchema(ARRRNG03, "Range start index " + start + " > end index " + end + " for array length " + size, token), cause); return cause; } - static ScriptRuntimeException failOnInvalidLeftValueIncrement(String code, Token token) { - return new ScriptRuntimeException(formatForSchema(code, + static InvalidLeftValueException failOnInvalidLeftValueIncrement(String code, Token token) { + return new InvalidLeftValueException(formatForSchema(code, "Invalid l-value for increment (readonly)", token)); } - static ScriptRuntimeException failOnInvalidLeftValueDecrement(String code, Token token) { - return new ScriptRuntimeException(formatForSchema(code, + static InvalidLeftValueException failOnInvalidLeftValueDecrement(String code, Token token) { + return new InvalidLeftValueException(formatForSchema(code, "Invalid l-value for decrement (readonly)", token)); } - static ScriptRuntimeException failOnInvalidLeftValueAssignment(String code, Token token) { - return new ScriptRuntimeException(formatForSchema(code, + static InvalidLeftValueException failOnInvalidLeftValueAssignment(String code, Token token) { + return new InvalidLeftValueException(formatForSchema(code, "Invalid l-value for assignment (readonly)", token)); } - static ScriptInvocationException failOnTargetNotFound(Token token) { - return new ScriptInvocationException(TRGT01, "Target not found for function '%s'", token); + static InvalidContextException failOnTargetNotFound(Token token) { + return new InvalidContextException(TRGTSE01, "Target not found", token); } - static ScriptInvocationException failOnCallerNotFound(Token token) { - return new ScriptInvocationException(CALR01, "Caller not found for function '%s'", token); + static InvalidContextException failOnCallerNotFound(Token token) { + return new InvalidContextException(CALRSE01, "Caller not found", token); } - static ScriptRuntimeException failOnIdentifierNotFound(String code, Token identifier) { - return new ScriptRuntimeException(formatForSchema(code, - "Identifier '" + identifier.getText() + "' not found", identifier)); + static VariableNotFoundException failOnVariableNotFound(String code, Token identifier) { + return new VariableNotFoundException(formatForSchema(code, + "Variable '" + identifier.getText() + "' not found", identifier)); } - static ScriptRuntimeException failOnFunctionNotFound(String name, int argCount, Token token) { - return new ScriptRuntimeException(formatForSchema(FNVK01, + static FunctionNotFoundException failOnFunctionNotFound(String name, int argCount, Token token) { + return new FunctionNotFoundException(formatForSchema(FNSNVK01, "Function '" + name + "' with " + argCount + " parameter(s) not found", token)); } - static ScriptRuntimeException failOnRuntime(String code, String message, - Token token, Throwable cause) { - return new ScriptRuntimeException(formatForSchema(code, message, token), cause); - } - - static ScriptInvocationException failOnVariadicArity(String code) { - return new ScriptInvocationException(code, - "Too few arguments for invocation of variadic function '%s'"); + static InvocationRuntimeException failOnVariadicArity(String code) { + return new InvocationRuntimeException(code, + "Too few arguments for invocation of variadic function"); } - static ScriptInvocationException failOnFixedArity(String code) { - return new ScriptInvocationException(code, - "Invalid number of arguments for invocation of function '%s'"); + static InvocationRuntimeException failOnFixedArity(String code) { + return new InvocationRuntimeException(code, + "Invalid number of arguments for invocation of function"); } static SystemOperationException failOnSystemException(String code, @@ -180,36 +188,36 @@ static SystemOperationException failOnSystemException(String code, token), exception); } - static ScriptRuntimeException failOnOperation(String code, String operation, + static ScriptOperationException failOnOperation(String code, String operation, EValue value, Token token, Throwable cause) { return new ScriptOperationException(formatForSchema(code, "Invalid " + operation + " operation on type " + value.getType(), token), cause); } - static ScriptRuntimeException failOnOperation(String code, String operation, + static ScriptOperationException failOnOperation(String code, String operation, EValue value, TerminalNode node) { return failOnOperation(code, operation, value, node.getSymbol(), null); } - static ScriptRuntimeException failOnOperation(String code, String operation, + static ScriptOperationException failOnOperation(String code, String operation, EValue value1, EValue value2, Token token, Throwable cause) { return new ScriptOperationException(formatForSchema(code, "Invalid " + operation + " operation on types " + value1.getType() + " and " + value2.getType(), token), cause); } - static ScriptRuntimeException failOnOperation(String code, String operation, + static ScriptOperationException failOnOperation(String code, String operation, EValue value1, EValue value2, TerminalNode node) { return failOnOperation(code, operation, value1, value2, node.getSymbol(), null); } - static ScriptRuntimeException failOnStringUpdate(String code, TerminalNode node) { - return new ScriptRuntimeException(formatForSchema(code, + static UpdateNotSupportedException failOnStringUpdate(String code, TerminalNode node) { + return new UpdateNotSupportedException(formatForSchema(code, "Immutable string characters cannot be updated", node.getSymbol())); } - static ScriptRuntimeException failOnArrayRangeUpdate(String code, TerminalNode node) { - return new ScriptRuntimeException(formatForSchema(code, + static UpdateNotSupportedException failOnArrayRangeUpdate(String code, TerminalNode node) { + return new UpdateNotSupportedException(formatForSchema(code, "Update a range of elements in array is not supported", node.getSymbol())); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptScope.java b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptScope.java index 21bbcdf..2add54b 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptScope.java +++ b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptScope.java @@ -1,7 +1,7 @@ package com.relogiclabs.jschema.internal.engine; -import com.relogiclabs.jschema.exception.ScriptFunctionException; -import com.relogiclabs.jschema.exception.ScriptVariableException; +import com.relogiclabs.jschema.exception.DuplicateFunctionException; +import com.relogiclabs.jschema.exception.DuplicateVariableException; import com.relogiclabs.jschema.internal.script.GFunction; import com.relogiclabs.jschema.internal.script.GLeftValue; import com.relogiclabs.jschema.tree.RuntimeContext; @@ -11,11 +11,11 @@ import java.util.HashMap; import java.util.Map; -import static com.relogiclabs.jschema.internal.library.ScriptLibrary3.resolveStatic; +import static com.relogiclabs.jschema.internal.library.LibraryFunctions3.resolveStatic; import static com.relogiclabs.jschema.internal.tree.EFunction.CONSTRAINT_PREFIX; -import static com.relogiclabs.jschema.message.ErrorCode.FUND01; -import static com.relogiclabs.jschema.message.ErrorCode.FUND02; -import static com.relogiclabs.jschema.message.ErrorCode.VARD01; +import static com.relogiclabs.jschema.message.ErrorCode.FNSDUP01; +import static com.relogiclabs.jschema.message.ErrorCode.FNSDUP02; +import static com.relogiclabs.jschema.message.ErrorCode.VARDUP01; import static org.apache.commons.lang3.StringUtils.substringBefore; public class ScriptScope { @@ -31,22 +31,22 @@ public GLeftValue addVariable(String name, EValue value) { var lvalue = new GLeftValue(value); var oldValue = symbols.put(name, lvalue); if(oldValue == null) return lvalue; - throw new ScriptVariableException(VARD01, - "Variable '" + name + "' already defined in the scope"); + throw new DuplicateVariableException(VARDUP01, + "Variable '" + name + "' already declared in the scope"); } - public void addFunction(String name, GFunction function) { - var oldValue = symbols.put(name, function); + public void addFunction(String functionId, GFunction function) { + var oldValue = symbols.put(functionId, function); if(oldValue == null) return; - if(name.startsWith(CONSTRAINT_PREFIX)) - throw failOnDuplicateDefinition(FUND01, "Constraint", name); - else throw failOnDuplicateDefinition(FUND02, "Subroutine", name); + if(functionId.startsWith(CONSTRAINT_PREFIX)) + throw failOnDuplicateFunction(FNSDUP01, "Constraint", functionId); + else throw failOnDuplicateFunction(FNSDUP02, "Subroutine", functionId); } - private static ScriptFunctionException failOnDuplicateDefinition(String code, - String functionType, String name) { - return new ScriptFunctionException(code, functionType + " function '" - + substringBefore(name, '#') + "' with matching parameter(s) already defined"); + private static DuplicateFunctionException failOnDuplicateFunction(String code, + String functionType, String functionId) { + return new DuplicateFunctionException(code, functionType + " function '" + + substringBefore(functionId, '#') + "' with matching parameter(s) already defined"); } public EValue resolve(String name) { diff --git a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor1.java b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor1.java index 6b536c6..f1a078c 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor1.java +++ b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor1.java @@ -1,8 +1,7 @@ package com.relogiclabs.jschema.internal.engine; -import com.relogiclabs.jschema.exception.CommonException; -import com.relogiclabs.jschema.exception.ScriptRuntimeException; -import com.relogiclabs.jschema.exception.ScriptTemplateException; +import com.relogiclabs.jschema.exception.BaseRuntimeException; +import com.relogiclabs.jschema.exception.MultilevelRuntimeException; import com.relogiclabs.jschema.internal.antlr.SchemaParser.ArrayLiteralContext; import com.relogiclabs.jschema.internal.antlr.SchemaParser.BlockStatementContext; import com.relogiclabs.jschema.internal.antlr.SchemaParser.BreakStatementContext; @@ -47,7 +46,6 @@ import static com.relogiclabs.jschema.internal.antlr.SchemaLexer.G_STRING; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnInvalidReturnType; -import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnRuntime; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnSystemException; import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.dereference; import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.getFunctionMode; @@ -58,23 +56,23 @@ import static com.relogiclabs.jschema.internal.script.GControl.BREAK; import static com.relogiclabs.jschema.internal.util.CollectionHelper.subList; import static com.relogiclabs.jschema.internal.util.StringHelper.toEncoded; -import static com.relogiclabs.jschema.message.ErrorCode.ARRL01; -import static com.relogiclabs.jschema.message.ErrorCode.BLOK01; -import static com.relogiclabs.jschema.message.ErrorCode.EXPR01; -import static com.relogiclabs.jschema.message.ErrorCode.EXPR02; -import static com.relogiclabs.jschema.message.ErrorCode.FORS01; -import static com.relogiclabs.jschema.message.ErrorCode.FREC01; -import static com.relogiclabs.jschema.message.ErrorCode.FUND03; -import static com.relogiclabs.jschema.message.ErrorCode.IFST01; -import static com.relogiclabs.jschema.message.ErrorCode.IFST02; -import static com.relogiclabs.jschema.message.ErrorCode.OBJL01; -import static com.relogiclabs.jschema.message.ErrorCode.RETN02; -import static com.relogiclabs.jschema.message.ErrorCode.RETN03; -import static com.relogiclabs.jschema.message.ErrorCode.SRPT01; -import static com.relogiclabs.jschema.message.ErrorCode.SRPT02; -import static com.relogiclabs.jschema.message.ErrorCode.VARD02; -import static com.relogiclabs.jschema.message.ErrorCode.VARD03; -import static com.relogiclabs.jschema.message.ErrorCode.WHIL01; +import static com.relogiclabs.jschema.message.ErrorCode.ARRLIT01; +import static com.relogiclabs.jschema.message.ErrorCode.BLOKSE01; +import static com.relogiclabs.jschema.message.ErrorCode.EXPRSE01; +import static com.relogiclabs.jschema.message.ErrorCode.EXPRSE02; +import static com.relogiclabs.jschema.message.ErrorCode.FNSDEC01; +import static com.relogiclabs.jschema.message.ErrorCode.FORECH01; +import static com.relogiclabs.jschema.message.ErrorCode.FORSTM01; +import static com.relogiclabs.jschema.message.ErrorCode.IFSTMT01; +import static com.relogiclabs.jschema.message.ErrorCode.IFSTMT02; +import static com.relogiclabs.jschema.message.ErrorCode.OBJLIT01; +import static com.relogiclabs.jschema.message.ErrorCode.RETNSE01; +import static com.relogiclabs.jschema.message.ErrorCode.RETNSE03; +import static com.relogiclabs.jschema.message.ErrorCode.SRPTSE01; +import static com.relogiclabs.jschema.message.ErrorCode.SRPTSE02; +import static com.relogiclabs.jschema.message.ErrorCode.VARDEC01; +import static com.relogiclabs.jschema.message.ErrorCode.VARDEC02; +import static com.relogiclabs.jschema.message.ErrorCode.WHILSE01; import static com.relogiclabs.jschema.type.ENull.NULL; import static com.relogiclabs.jschema.type.EUndefined.UNDEFINED; import static com.relogiclabs.jschema.type.EValue.VOID; @@ -106,7 +104,7 @@ public Evaluator visitCompleteSchema(CompleteSchemaContext ctx) { return tryCatch(scope -> { for(var s : scripts) s.evaluate(scope); return VOID; - }, SRPT01, ctx); + }, SRPTSE01, ctx); } private Evaluator processScript(ParserRuleContext context) { @@ -126,7 +124,7 @@ public Evaluator visitScriptNode(ScriptNodeContext ctx) { return tryCatch(scope -> { for(var s : statements) s.evaluate(scope); return VOID; - }, SRPT02, ctx); + }, SRPTSE02, ctx); } @Override @@ -145,7 +143,7 @@ public Evaluator visitFunctionDeclaration(FunctionDeclarationContext ctx) { if(constraint) runtime.getFunctions() .addFunction(new ScriptFunction(baseName, function)); return VOID; - }, FUND03, ctx); + }, FNSDEC01, ctx); } @Override @@ -154,7 +152,7 @@ public Evaluator visitVarStatement(VarStatementContext ctx) { return tryCatch(scope -> { for(var d : varDeclarations) d.evaluate(scope); return VOID; - }, VARD03, ctx); + }, VARDEC02, ctx); } @Override @@ -164,7 +162,7 @@ public Evaluator visitVarDeclaration(VarDeclarationContext ctx) { return tryCatch(scope -> { scope.addVariable(varName, dereference(expression.evaluate(scope))); return VOID; - }, VARD02, ctx); + }, VARDEC01, ctx); } @Override @@ -173,7 +171,7 @@ public Evaluator visitExpressionStatement(ExpressionStatementContext ctx) { return tryCatch(scope -> { expression.evaluate(scope); return VOID; - }, EXPR01, ctx); + }, EXPRSE01, ctx); } @Override @@ -184,14 +182,14 @@ public Evaluator visitIfStatement(IfStatementContext ctx) { if(condition.evaluate(scope).toBoolean()) return thenStatement.evaluate(scope); return VOID; - }, IFST01, ctx); + }, IFSTMT01, ctx); var elseStatement = visit(ctx.statement(1)); return tryCatch(scope -> { if(condition.evaluate(scope).toBoolean()) return thenStatement.evaluate(scope); else return elseStatement.evaluate(scope); - }, IFST02, ctx); + }, IFSTMT02, ctx); } @Override @@ -204,7 +202,7 @@ public Evaluator visitWhileStatement(WhileStatementContext ctx) { if(result instanceof GControl ctrl) return ctrl.toIteration(); } return VOID; - }, WHIL01, ctx); + }, WHILSE01, ctx); } @Override @@ -223,7 +221,7 @@ public Evaluator visitForStatement(ForStatementContext ctx) { if(result instanceof GControl ctrl) return ctrl.toIteration(); } return VOID; - }, FORS01, ctx); + }, FORSTM01, ctx); } @Override @@ -232,7 +230,7 @@ public Evaluator visitExpressionList(ExpressionListContext ctx) { return tryCatch(scope -> { for(var e : expressions) e.evaluate(scope); return VOID; - }, EXPR02, ctx); + }, EXPRSE02, ctx); } @Override @@ -249,21 +247,21 @@ public Evaluator visitForeachStatement(ForeachStatementContext ctx) { if(result instanceof GControl ctrl) return ctrl.toIteration(); } return VOID; - }, FREC01, ctx); + }, FORECH01, ctx); } @Override public Evaluator visitReturnStatement(ReturnStatementContext ctx) { var expression = visit(ctx.expression()); if(returnType == null) return tryCatch(scope -> GControl.ofReturn( - expression.evaluate(scope)), RETN02, ctx); + expression.evaluate(scope)), RETNSE01, ctx); var thisReturnType = returnType; return tryCatch(scope -> { var v1 = expression.evaluate(scope); if(!thisReturnType.isInstance(v1)) throw failOnInvalidReturnType(v1, ctx.expression().getStart()); return GControl.ofReturn(v1); - }, RETN03, ctx); + }, RETNSE03, ctx); } @Override @@ -281,7 +279,7 @@ public Evaluator visitBlockStatement(BlockStatementContext ctx) { if(result instanceof GControl ctrl) return ctrl; } return VOID; - }, BLOK01, ctx); + }, BLOKSE01, ctx); } @Override @@ -326,7 +324,7 @@ public Evaluator visitStringLiteral(StringLiteralContext ctx) { public Evaluator visitArrayLiteral(ArrayLiteralContext ctx) { var list = ctx.expression().stream().map(this::visit).toList(); return tryCatch(scope -> new GArray(list.stream().map(e - -> dereference(e.evaluate(scope))).toList()), ARRL01, ctx); + -> dereference(e.evaluate(scope))).toList()), ARRLIT01, ctx); } @Override @@ -335,19 +333,19 @@ public Evaluator visitObjectLiteral(ObjectLiteralContext ctx) { ? toEncoded(k.getText()) : k.getText()).toList(); var values = ctx.values.stream().map(this::visit).toList(); return tryCatch(scope -> new GObject(keys, values.stream().map(v - -> dereference(v.evaluate(scope))).toList()), OBJL01, ctx); + -> dereference(v.evaluate(scope))).toList()), OBJLIT01, ctx); } static Evaluator tryCatch(Evaluator evaluator, String code, ParserRuleContext ctx) { return scope -> { try { return evaluator.evaluate(scope); - } catch(ScriptRuntimeException | ScriptTemplateException e) { + } catch(MultilevelRuntimeException e) { + throw e.translate(ctx.getStart()); + } catch(BaseRuntimeException e) { throw e; - } catch(CommonException e) { - throw failOnRuntime(e.getCode(), e.getMessage(), ctx.start, e); } catch(Exception e) { - throw failOnSystemException(code, e, ctx.start); + throw failOnSystemException(code, e, ctx.getStart()); } }; } diff --git a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor2.java b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor2.java index ca6a995..f256d76 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor2.java +++ b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor2.java @@ -1,13 +1,14 @@ package com.relogiclabs.jschema.internal.engine; -import com.relogiclabs.jschema.exception.ScriptArgumentException; -import com.relogiclabs.jschema.exception.ScriptInitiatedException; -import com.relogiclabs.jschema.exception.ScriptInvocationException; +import com.relogiclabs.jschema.exception.InvocationRuntimeException; +import com.relogiclabs.jschema.exception.MultilevelRuntimeException; +import com.relogiclabs.jschema.exception.ScriptThrowInitiatedException; import com.relogiclabs.jschema.internal.antlr.SchemaParser.ExpressionContext; import com.relogiclabs.jschema.internal.antlr.SchemaParser.InvokeFunctionExpressionContext; import com.relogiclabs.jschema.internal.antlr.SchemaParser.InvokeMethodExpressionContext; import com.relogiclabs.jschema.internal.antlr.SchemaParser.PostIncDecExpressionContext; import com.relogiclabs.jschema.internal.antlr.SchemaParser.PreIncDecExpressionContext; +import com.relogiclabs.jschema.internal.antlr.SchemaParser.VariableExpressionContext; import com.relogiclabs.jschema.internal.script.GArray; import com.relogiclabs.jschema.internal.script.GLeftValue; import com.relogiclabs.jschema.internal.script.GRange; @@ -30,7 +31,6 @@ import static com.relogiclabs.jschema.internal.antlr.SchemaParser.G_IDENTIFIER; import static com.relogiclabs.jschema.internal.antlr.SchemaParser.G_INC; import static com.relogiclabs.jschema.internal.antlr.SchemaParser.G_LBRACKET; -import static com.relogiclabs.jschema.internal.antlr.SchemaParser.IdentifierExpressionContext; import static com.relogiclabs.jschema.internal.antlr.SchemaParser.MemberBracketExpressionContext; import static com.relogiclabs.jschema.internal.antlr.SchemaParser.MemberDotExpressionContext; import static com.relogiclabs.jschema.internal.antlr.SchemaParser.RangeBothExpressionContext; @@ -49,16 +49,15 @@ import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnArrayRangeUpdate; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnCallerNotFound; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnFunctionNotFound; -import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnIdentifierNotFound; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnIndexOutOfBounds; +import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnInvalidIndexRange; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnInvalidLeftValueDecrement; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnInvalidLeftValueIncrement; -import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnInvalidRangeIndex; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnOperation; -import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnPropertyNotExist; -import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnRuntime; +import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnPropertyNotFound; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnStringUpdate; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnTargetNotFound; +import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnVariableNotFound; import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.createTryofMonad; import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.decrement; import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.dereference; @@ -70,56 +69,56 @@ import static com.relogiclabs.jschema.internal.util.CollectionHelper.subList; import static com.relogiclabs.jschema.internal.util.CommonHelper.getToken; import static com.relogiclabs.jschema.internal.util.CommonHelper.hasToken; -import static com.relogiclabs.jschema.message.ErrorCode.ARAS01; -import static com.relogiclabs.jschema.message.ErrorCode.ARUD01; -import static com.relogiclabs.jschema.message.ErrorCode.BKTA01; -import static com.relogiclabs.jschema.message.ErrorCode.BKTR01; -import static com.relogiclabs.jschema.message.ErrorCode.BKTR02; -import static com.relogiclabs.jschema.message.ErrorCode.BKTU01; -import static com.relogiclabs.jschema.message.ErrorCode.BKTU02; -import static com.relogiclabs.jschema.message.ErrorCode.CALR02; -import static com.relogiclabs.jschema.message.ErrorCode.DECE01; -import static com.relogiclabs.jschema.message.ErrorCode.DECE02; -import static com.relogiclabs.jschema.message.ErrorCode.DECE03; -import static com.relogiclabs.jschema.message.ErrorCode.DECT01; -import static com.relogiclabs.jschema.message.ErrorCode.DECT02; -import static com.relogiclabs.jschema.message.ErrorCode.DECT03; -import static com.relogiclabs.jschema.message.ErrorCode.FNVK04; -import static com.relogiclabs.jschema.message.ErrorCode.IDEN01; -import static com.relogiclabs.jschema.message.ErrorCode.IDEN02; -import static com.relogiclabs.jschema.message.ErrorCode.IDXA01; -import static com.relogiclabs.jschema.message.ErrorCode.IDXR01; -import static com.relogiclabs.jschema.message.ErrorCode.IDXU01; -import static com.relogiclabs.jschema.message.ErrorCode.INCE01; -import static com.relogiclabs.jschema.message.ErrorCode.INCE02; -import static com.relogiclabs.jschema.message.ErrorCode.INCE03; -import static com.relogiclabs.jschema.message.ErrorCode.INCT01; -import static com.relogiclabs.jschema.message.ErrorCode.INCT02; -import static com.relogiclabs.jschema.message.ErrorCode.INCT03; -import static com.relogiclabs.jschema.message.ErrorCode.MNVK02; -import static com.relogiclabs.jschema.message.ErrorCode.PRPT01; -import static com.relogiclabs.jschema.message.ErrorCode.PRPT02; -import static com.relogiclabs.jschema.message.ErrorCode.PRPT03; -import static com.relogiclabs.jschema.message.ErrorCode.PRPT04; -import static com.relogiclabs.jschema.message.ErrorCode.RNGA01; -import static com.relogiclabs.jschema.message.ErrorCode.RNGR01; -import static com.relogiclabs.jschema.message.ErrorCode.RNGT01; -import static com.relogiclabs.jschema.message.ErrorCode.RNGT02; -import static com.relogiclabs.jschema.message.ErrorCode.RNGT03; -import static com.relogiclabs.jschema.message.ErrorCode.RNGT04; -import static com.relogiclabs.jschema.message.ErrorCode.RNGT05; -import static com.relogiclabs.jschema.message.ErrorCode.RNGT06; -import static com.relogiclabs.jschema.message.ErrorCode.RNGU01; -import static com.relogiclabs.jschema.message.ErrorCode.SASN01; -import static com.relogiclabs.jschema.message.ErrorCode.SUPD01; -import static com.relogiclabs.jschema.message.ErrorCode.THRO01; -import static com.relogiclabs.jschema.message.ErrorCode.THRO02; -import static com.relogiclabs.jschema.message.ErrorCode.TRGT02; +import static com.relogiclabs.jschema.message.ErrorCode.ARRASN01; +import static com.relogiclabs.jschema.message.ErrorCode.ARRUPD01; +import static com.relogiclabs.jschema.message.ErrorCode.BKTASN01; +import static com.relogiclabs.jschema.message.ErrorCode.BKTUPD01; +import static com.relogiclabs.jschema.message.ErrorCode.BKTUPD02; +import static com.relogiclabs.jschema.message.ErrorCode.CALRSE02; +import static com.relogiclabs.jschema.message.ErrorCode.DECPRE01; +import static com.relogiclabs.jschema.message.ErrorCode.DECPRE02; +import static com.relogiclabs.jschema.message.ErrorCode.DECPRE03; +import static com.relogiclabs.jschema.message.ErrorCode.DECPST01; +import static com.relogiclabs.jschema.message.ErrorCode.DECPST02; +import static com.relogiclabs.jschema.message.ErrorCode.DECPST03; +import static com.relogiclabs.jschema.message.ErrorCode.FNSNVK04; +import static com.relogiclabs.jschema.message.ErrorCode.IDXASN01; +import static com.relogiclabs.jschema.message.ErrorCode.IDXUPD01; +import static com.relogiclabs.jschema.message.ErrorCode.INCPRE01; +import static com.relogiclabs.jschema.message.ErrorCode.INCPRE02; +import static com.relogiclabs.jschema.message.ErrorCode.INCPRE03; +import static com.relogiclabs.jschema.message.ErrorCode.INCPST01; +import static com.relogiclabs.jschema.message.ErrorCode.INCPST02; +import static com.relogiclabs.jschema.message.ErrorCode.INCPST03; +import static com.relogiclabs.jschema.message.ErrorCode.MTHNVK02; +import static com.relogiclabs.jschema.message.ErrorCode.OPPRTY01; +import static com.relogiclabs.jschema.message.ErrorCode.OPPRTY02; +import static com.relogiclabs.jschema.message.ErrorCode.OPPRTY03; +import static com.relogiclabs.jschema.message.ErrorCode.OPPRTY04; +import static com.relogiclabs.jschema.message.ErrorCode.OPRNGT01; +import static com.relogiclabs.jschema.message.ErrorCode.OPRNGT02; +import static com.relogiclabs.jschema.message.ErrorCode.OPRNGT03; +import static com.relogiclabs.jschema.message.ErrorCode.OPRNGT04; +import static com.relogiclabs.jschema.message.ErrorCode.OPRNGT05; +import static com.relogiclabs.jschema.message.ErrorCode.OPRNGT06; +import static com.relogiclabs.jschema.message.ErrorCode.REDBKT01; +import static com.relogiclabs.jschema.message.ErrorCode.REDBKT02; +import static com.relogiclabs.jschema.message.ErrorCode.REDIDX01; +import static com.relogiclabs.jschema.message.ErrorCode.REDRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.RNGASN01; +import static com.relogiclabs.jschema.message.ErrorCode.RNGUPD01; +import static com.relogiclabs.jschema.message.ErrorCode.STRASN01; +import static com.relogiclabs.jschema.message.ErrorCode.STRUPD01; +import static com.relogiclabs.jschema.message.ErrorCode.THRODF01; +import static com.relogiclabs.jschema.message.ErrorCode.THROSE01; +import static com.relogiclabs.jschema.message.ErrorCode.TRGTSE02; +import static com.relogiclabs.jschema.message.ErrorCode.VARRES01; +import static com.relogiclabs.jschema.message.ErrorCode.VARRES02; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; import static com.relogiclabs.jschema.type.EUndefined.UNDEFINED; public abstract class ScriptTreeVisitor2 extends ScriptTreeVisitor1 { - private static final Evaluator THROW_CODE_SUPPLIER = s -> GString.from(THRO01); + private static final Evaluator THROW_CODE_SUPPLIER = s -> GString.from(THRODF01); public ScriptTreeVisitor2(RuntimeContext runtime) { super(runtime); @@ -136,14 +135,14 @@ public Evaluator visitMemberDotExpression(MemberDotExpressionContext ctx) { } @Override - public Evaluator visitIdentifierExpression(IdentifierExpressionContext ctx) { - return handleIdentifierExpression(ctx); + public Evaluator visitVariableExpression(VariableExpressionContext ctx) { + return handleVariableExpression(ctx); } Evaluator handleLeftValueUpdateExpression(ParserRuleContext ctx) { if(hasToken(ctx, G_LBRACKET)) return handleBracketUpdateExpression(ctx); if(hasToken(ctx, G_DOT)) return handleDotExpression(ctx); - return handleIdentifierExpression(ctx); + return handleVariableExpression(ctx); } private Evaluator handleBracketReadExpression(ParserRuleContext ctx) { @@ -163,10 +162,12 @@ private Evaluator handleBracketReadExpression(ParserRuleContext ctx) { if(v3 != null) return v3; v3 = evaluateObjectProperty(v1, v2, expressions.get(1)); if(v3 != null) return v3; - if(v2 instanceof EInteger) throw failOnOperation(IDXR01, OpIndex, v1, getToken(ctx, G_LBRACKET)); - if(v2 instanceof GRange) throw failOnOperation(RNGR01, OpRange, v1, getToken(ctx, G_LBRACKET)); - throw failOnOperation(BKTR01, OpBracket, v1, v2, getToken(ctx, G_LBRACKET)); - }, BKTR02, expressions.get(0)); + if(v2 instanceof EInteger) throw failOnOperation(REDIDX01, + OpIndex, v1, getToken(ctx, G_LBRACKET)); + if(v2 instanceof GRange) throw failOnOperation(REDRNG01, + OpRange, v1, getToken(ctx, G_LBRACKET)); + throw failOnOperation(REDBKT01, OpBracket, v1, v2, getToken(ctx, G_LBRACKET)); + }, REDBKT02, expressions.get(0)); } private Evaluator handleBracketUpdateExpression(ParserRuleContext ctx) { @@ -182,23 +183,23 @@ private Evaluator handleBracketUpdateExpression(ParserRuleContext ctx) { if(v3 != null) return v3; throwBracketUpdateException(v1, v2, ctx); throw new IllegalStateException("Invalid runtime state"); - }, BKTU02, expressions.get(0)); + }, BKTUPD02, expressions.get(0)); } static void throwBracketUpdateException(EValue v1, EValue v2, ParserRuleContext ctx) { if(v1 instanceof EString) throw failOnStringUpdate(hasToken(ctx, G_ASSIGN) - ? SASN01 : SUPD01, getToken(ctx, G_LBRACKET)); + ? STRASN01 : STRUPD01, getToken(ctx, G_LBRACKET)); if(v1 instanceof EArray && v2 instanceof GRange) throw failOnArrayRangeUpdate(hasToken(ctx, G_ASSIGN) - ? ARAS01 : ARUD01, getToken(ctx, G_LBRACKET)); + ? ARRASN01 : ARRUPD01, getToken(ctx, G_LBRACKET)); if(v2 instanceof EInteger) throw failOnOperation(hasToken(ctx, G_ASSIGN) - ? IDXA01 : IDXU01, OpIndex, v1, getToken(ctx, G_LBRACKET)); + ? IDXASN01 : IDXUPD01, OpIndex, v1, getToken(ctx, G_LBRACKET)); if(v2 instanceof GRange) throw failOnOperation(hasToken(ctx, G_ASSIGN) - ? RNGA01 : RNGU01, OpRange, v1, getToken(ctx, G_LBRACKET)); + ? RNGASN01 : RNGUPD01, OpRange, v1, getToken(ctx, G_LBRACKET)); - if(hasToken(ctx, G_ASSIGN)) throw failOnOperation(BKTA01, - OpBracketedAssignment, v1, v2, getToken(ctx, G_LBRACKET)); - throw failOnOperation(BKTU01, OpBracket, v1, v2, getToken(ctx, G_LBRACKET)); + if(hasToken(ctx, G_ASSIGN)) throw failOnOperation(BKTASN01, + OpBracketedAssignment, v1, v2, getToken(ctx, G_LBRACKET)); + throw failOnOperation(BKTUPD01, OpBracket, v1, v2, getToken(ctx, G_LBRACKET)); } private Evaluator handleDotExpression(ParserRuleContext ctx) { @@ -208,22 +209,22 @@ private Evaluator handleDotExpression(ParserRuleContext ctx) { var key2 = identifier.getText(); return tryCatch(scope -> { var v1 = dereference(object1.evaluate(scope)); - if(!(v1 instanceof EObject o1)) throw failOnOperation(PRPT01, OpProperty, + if(!(v1 instanceof EObject o1)) throw failOnOperation(OPPRTY01, OpProperty, v1, getToken(ctx, G_DOT)); var v2 = o1.get(key2); - if(v2 == null) throw failOnPropertyNotExist(PRPT02, o1, key2, identifier.getSymbol()); + if(v2 == null) throw failOnPropertyNotFound(OPPRTY02, o1, key2, identifier.getSymbol()); return v2; - }, PRPT04, expressions.get(0)); + }, OPPRTY03, expressions.get(0)); } - private static Evaluator handleIdentifierExpression(ParserRuleContext ctx) { + private static Evaluator handleVariableExpression(ParserRuleContext ctx) { var identifier = getToken(ctx, G_IDENTIFIER); var name = identifier.getText(); return tryCatch(scope -> { var v1 = scope.resolve(name); - if(v1 == null) throw failOnIdentifierNotFound(IDEN01, identifier.getSymbol()); + if(v1 == null) throw failOnVariableNotFound(VARRES01, identifier.getSymbol()); return v1; - }, IDEN02, ctx); + }, VARRES02, ctx); } private static EValue evaluateArrayIndex(EValue v1, EValue v2, ExpressionContext ctx) { @@ -240,14 +241,14 @@ private static GArray evaluateArrayRange(EValue v1, EValue v2, ExpressionContext try { return GArray.from(a1, r2); } catch(IndexOutOfBoundsException | IllegalArgumentException e) { - throw failOnInvalidRangeIndex(a1, r2, ctx.getStart(), e); + throw failOnInvalidIndexRange(a1, r2, ctx.getStart(), e); } } private static EValue evaluateObjectProperty(EValue v1, EValue v2, ExpressionContext ctx) { if(!(v1 instanceof EObject o1) || !(v2 instanceof EString s2)) return null; var v3 = o1.get(s2.getValue()); - if(v3 == null) throw failOnPropertyNotExist(PRPT03, o1, s2.getValue(), ctx.getStart()); + if(v3 == null) throw failOnPropertyNotFound(OPPRTY04, o1, s2.getValue(), ctx.getStart()); return v3; } @@ -265,7 +266,7 @@ private static GString evaluateStringRange(EValue v1, EValue v2, ExpressionConte try { return GString.from(s1, r2); } catch(IndexOutOfBoundsException | IllegalArgumentException e) { - throw failOnInvalidRangeIndex(s1, r2, ctx.getStart(), e); + throw failOnInvalidIndexRange(s1, r2, ctx.getStart(), e); } } @@ -281,15 +282,14 @@ public Evaluator visitInvokeFunctionExpression(InvokeFunctionExpressionContext c var v1 = scope.resolve(fid1); if(v1 == null) v1 = scope.resolve(fid2); if(!(v1 instanceof RFunction f1)) throw failOnFunctionNotFound(name1, - param2.size(), fn1.getSymbol()); + param2.size(), fn1.getSymbol()); var p2 = param2.stream().map(a -> dereference(a.evaluate(scope))).toList(); return f1.invoke(f1.bind(scope, p2), p2); - } catch(ScriptInvocationException e) { - throw failOnRuntime(e.getCode(), e.getMessage(name1), e.getToken(fn1.getSymbol()), e); - } catch(ScriptArgumentException e) { - throw failOnRuntime(e.getCode(), e.getMessage(name1), fn1.getSymbol(), e); + } catch(InvocationRuntimeException e) { + e.setSubject(name1); + throw e.translate(fn1.getSymbol()); } - }, FNVK04, ctx); + }, FNSNVK04, ctx); } @Override @@ -299,11 +299,16 @@ public Evaluator visitInvokeMethodExpression(InvokeMethodExpressionContext ctx) var name2 = method2.getText(); var param3 = subList(ctx.expression(), 1).stream().map(this::visit).toList(); return tryCatch(scope -> { - var s1 = dereference(self1.evaluate(scope)); - var m2 = s1.getMethod(name2, param3.size()); - var p3 = param3.stream().map(p -> dereference(p.evaluate(scope))).toList(); - return m2.evaluate(s1, p3, scope); - }, MNVK02, ctx); + try { + var s1 = dereference(self1.evaluate(scope)); + var m2 = s1.getMethod(name2, param3.size()); + var p3 = param3.stream().map(p -> dereference(p.evaluate(scope))).toList(); + return m2.evaluate(s1, p3, scope); + } catch(InvocationRuntimeException e) { + e.setSubject(name2); + throw e.translate(method2.getSymbol()); + } + }, MTHNVK02, ctx); } @Override @@ -324,52 +329,52 @@ private Evaluator handlePostIncrementExpression(ParserRuleContext ctx) { var lvalue1 = handleLeftValueUpdateExpression(ctx); return tryCatch(scope -> { var v1 = lvalue1.evaluate(scope); - if(!(v1 instanceof GLeftValue l1)) throw failOnInvalidLeftValueIncrement(INCT01, + if(!(v1 instanceof GLeftValue l1)) throw failOnInvalidLeftValueIncrement(INCPST01, getToken(ctx, G_INC).getSymbol()); - if(!(l1.getValue() instanceof ENumber n1)) throw failOnOperation(INCT02, + if(!(l1.getValue() instanceof ENumber n1)) throw failOnOperation(INCPST02, OpIncrement, v1, getToken(ctx, G_INC)); l1.setValue(increment(n1)); return n1; - }, INCT03, ctx); + }, INCPST03, ctx); } private Evaluator handlePreIncrementExpression(ParserRuleContext ctx) { var lvalue1 = handleLeftValueUpdateExpression(ctx); return tryCatch(scope -> { var v1 = lvalue1.evaluate(scope); - if(!(v1 instanceof GLeftValue l1)) throw failOnInvalidLeftValueIncrement(INCE01, + if(!(v1 instanceof GLeftValue l1)) throw failOnInvalidLeftValueIncrement(INCPRE01, getToken(ctx, G_INC).getSymbol()); - if(!(l1.getValue() instanceof ENumber n1)) throw failOnOperation(INCE02, + if(!(l1.getValue() instanceof ENumber n1)) throw failOnOperation(INCPRE02, OpIncrement, v1, getToken(ctx, G_INC)); l1.setValue(n1 = increment(n1)); return n1; - }, INCE03, ctx); + }, INCPRE03, ctx); } private Evaluator handlePostDecrementExpression(ParserRuleContext ctx) { var lvalue1 = handleLeftValueUpdateExpression(ctx); return tryCatch(scope -> { var v1 = lvalue1.evaluate(scope); - if(!(v1 instanceof GLeftValue l1)) throw failOnInvalidLeftValueDecrement(DECT01, + if(!(v1 instanceof GLeftValue l1)) throw failOnInvalidLeftValueDecrement(DECPST01, getToken(ctx, G_DEC).getSymbol()); - if(!(l1.getValue() instanceof ENumber n1)) throw failOnOperation(DECT02, + if(!(l1.getValue() instanceof ENumber n1)) throw failOnOperation(DECPST02, OpDecrement, v1, getToken(ctx, G_DEC)); l1.setValue(decrement(n1)); return n1; - }, DECT03, ctx); + }, DECPST03, ctx); } private Evaluator handlePreDecrementExpression(ParserRuleContext ctx) { var lvalue1 = handleLeftValueUpdateExpression(ctx); return tryCatch(scope -> { var v1 = lvalue1.evaluate(scope); - if(!(v1 instanceof GLeftValue l1)) throw failOnInvalidLeftValueDecrement(DECE01, + if(!(v1 instanceof GLeftValue l1)) throw failOnInvalidLeftValueDecrement(DECPRE01, getToken(ctx, G_DEC).getSymbol()); - if(!(l1.getValue() instanceof ENumber n1)) throw failOnOperation(DECE02, + if(!(l1.getValue() instanceof ENumber n1)) throw failOnOperation(DECPRE02, OpDecrement, v1, getToken(ctx, G_DEC)); l1.setValue(n1 = decrement(n1)); return n1; - }, DECE03, ctx); + }, DECPRE03, ctx); } @Override @@ -379,9 +384,12 @@ public Evaluator visitTryofExpression(TryofExpressionContext ctx) { try { var value = dereference(expression.evaluate(scope)); return createTryofMonad(value, UNDEFINED); - } catch(Exception e) { - LogHelper.log(e, ctx.expression().getStart()); - return createTryofMonad(UNDEFINED, GString.from(e.getMessage())); + } catch(Exception exInstance) { + var exception = exInstance; + if(exception instanceof MultilevelRuntimeException ex) + exception = ex.translate(ctx.expression().getStart()); + LogHelper.log(exception, ctx.expression().getStart()); + return createTryofMonad(UNDEFINED, GString.from(exception.getMessage())); } }; } @@ -394,9 +402,9 @@ public Evaluator visitThrowExpression(ThrowExpressionContext ctx) { return tryCatch(scope -> { var c1 = stringify(dereference(code.evaluate(scope))); var m2 = stringify(dereference(message.evaluate(scope))); - throw new ScriptInitiatedException(formatForSchema(c1, m2, - ctx.G_THROW().getSymbol())); - }, THRO02, ctx); + throw new ScriptThrowInitiatedException(formatForSchema(c1, m2, + ctx.G_THROW().getSymbol())); + }, THROSE01, ctx); } @Override @@ -405,7 +413,7 @@ public Evaluator visitTargetExpression(TargetExpressionContext ctx) { var v1 = scope.resolve(TARGET_HVAR); if(v1 == null) throw failOnTargetNotFound(ctx.G_TARGET().getSymbol()); return v1; - }, TRGT02, ctx); + }, TRGTSE02, ctx); } @Override @@ -414,7 +422,7 @@ public Evaluator visitCallerExpression(CallerExpressionContext ctx) { var v1 = scope.resolve(CALLER_HVAR); if(v1 == null) throw failOnCallerNotFound(ctx.G_CALLER().getSymbol()); return v1; - }, CALR02, ctx); + }, CALRSE02, ctx); } @Override @@ -423,18 +431,18 @@ public Evaluator visitRangeBothExpression(RangeBothExpressionContext ctx) { var expr2 = visit(ctx.expression(1), VOID_SUPPLIER); if(expr2 == VOID_SUPPLIER) return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); - if(!(v1 instanceof EInteger i1)) throw failOnOperation(RNGT01, + if(!(v1 instanceof EInteger i1)) throw failOnOperation(OPRNGT01, OpRangeSetup, v1, ctx.G_RANGE()); return GRange.from(i1, null); - }, RNGT02, ctx); + }, OPRNGT02, ctx); return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); var v2 = dereference(expr2.evaluate(scope)); if(!(v1 instanceof EInteger i1) || !(v2 instanceof EInteger i2)) - throw failOnOperation(RNGT03, OpRangeSetup, v1, v2, ctx.G_RANGE()); + throw failOnOperation(OPRNGT03, OpRangeSetup, v1, v2, ctx.G_RANGE()); return GRange.from(i1, i2); - }, RNGT04, ctx); + }, OPRNGT04, ctx); } @Override @@ -442,9 +450,9 @@ public Evaluator visitRangeEndExpression(RangeEndExpressionContext ctx) { var expr2 = visit(ctx.expression()); return tryCatch(scope -> { var v2 = dereference(expr2.evaluate(scope)); - if(!(v2 instanceof EInteger i2)) throw failOnOperation(RNGT05, + if(!(v2 instanceof EInteger i2)) throw failOnOperation(OPRNGT05, OpRangeSetup, v2, ctx.G_RANGE()); return GRange.from(null, i2); - }, RNGT06, ctx); + }, OPRNGT06, ctx); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor3.java b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor3.java index 235fc0c..7cba09a 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor3.java +++ b/src/main/java/com/relogiclabs/jschema/internal/engine/ScriptTreeVisitor3.java @@ -39,63 +39,63 @@ import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.OpSubtractionAssignment; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.OpUnaryMinus; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.OpUnaryPlus; -import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnIdentifierNotFound; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnInvalidLeftValueAssignment; import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnOperation; +import static com.relogiclabs.jschema.internal.engine.ScriptErrorHelper.failOnVariableNotFound; import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.areEqual; import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.dereference; import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.stringify; import static com.relogiclabs.jschema.internal.script.GBoolean.FALSE; import static com.relogiclabs.jschema.internal.script.GBoolean.TRUE; import static com.relogiclabs.jschema.internal.util.CollectionHelper.getLast; -import static com.relogiclabs.jschema.message.ErrorCode.ADDN01; -import static com.relogiclabs.jschema.message.ErrorCode.ADDN02; -import static com.relogiclabs.jschema.message.ErrorCode.ADDN03; -import static com.relogiclabs.jschema.message.ErrorCode.ADDT01; -import static com.relogiclabs.jschema.message.ErrorCode.ADDT02; -import static com.relogiclabs.jschema.message.ErrorCode.ANDL01; -import static com.relogiclabs.jschema.message.ErrorCode.ASIN01; -import static com.relogiclabs.jschema.message.ErrorCode.ASIN02; -import static com.relogiclabs.jschema.message.ErrorCode.ASIN03; -import static com.relogiclabs.jschema.message.ErrorCode.ASIN04; -import static com.relogiclabs.jschema.message.ErrorCode.ASIN05; -import static com.relogiclabs.jschema.message.ErrorCode.DIVD01; -import static com.relogiclabs.jschema.message.ErrorCode.DIVD02; -import static com.relogiclabs.jschema.message.ErrorCode.DIVN01; -import static com.relogiclabs.jschema.message.ErrorCode.DIVN02; -import static com.relogiclabs.jschema.message.ErrorCode.DIVN03; -import static com.relogiclabs.jschema.message.ErrorCode.EQUL01; -import static com.relogiclabs.jschema.message.ErrorCode.IDEN03; -import static com.relogiclabs.jschema.message.ErrorCode.MINS01; -import static com.relogiclabs.jschema.message.ErrorCode.MINS02; -import static com.relogiclabs.jschema.message.ErrorCode.MODN01; -import static com.relogiclabs.jschema.message.ErrorCode.MODN02; -import static com.relogiclabs.jschema.message.ErrorCode.MODN03; -import static com.relogiclabs.jschema.message.ErrorCode.MODU01; -import static com.relogiclabs.jschema.message.ErrorCode.MODU02; -import static com.relogiclabs.jschema.message.ErrorCode.MULN01; -import static com.relogiclabs.jschema.message.ErrorCode.MULN02; -import static com.relogiclabs.jschema.message.ErrorCode.MULN03; -import static com.relogiclabs.jschema.message.ErrorCode.MULT01; -import static com.relogiclabs.jschema.message.ErrorCode.MULT02; -import static com.relogiclabs.jschema.message.ErrorCode.NEQL01; -import static com.relogiclabs.jschema.message.ErrorCode.NOTL01; -import static com.relogiclabs.jschema.message.ErrorCode.ORLG01; -import static com.relogiclabs.jschema.message.ErrorCode.PLUS01; -import static com.relogiclabs.jschema.message.ErrorCode.PLUS02; -import static com.relogiclabs.jschema.message.ErrorCode.RELA01; -import static com.relogiclabs.jschema.message.ErrorCode.RELA02; -import static com.relogiclabs.jschema.message.ErrorCode.RELA03; -import static com.relogiclabs.jschema.message.ErrorCode.RELA04; -import static com.relogiclabs.jschema.message.ErrorCode.RELA05; -import static com.relogiclabs.jschema.message.ErrorCode.RELA06; -import static com.relogiclabs.jschema.message.ErrorCode.RELA07; -import static com.relogiclabs.jschema.message.ErrorCode.RELA08; -import static com.relogiclabs.jschema.message.ErrorCode.SUBN01; -import static com.relogiclabs.jschema.message.ErrorCode.SUBN02; -import static com.relogiclabs.jschema.message.ErrorCode.SUBN03; -import static com.relogiclabs.jschema.message.ErrorCode.SUBT01; -import static com.relogiclabs.jschema.message.ErrorCode.SUBT02; +import static com.relogiclabs.jschema.message.ErrorCode.ADDASN01; +import static com.relogiclabs.jschema.message.ErrorCode.ADDASN02; +import static com.relogiclabs.jschema.message.ErrorCode.ADDASN03; +import static com.relogiclabs.jschema.message.ErrorCode.ASNBKT01; +import static com.relogiclabs.jschema.message.ErrorCode.ASNDOT01; +import static com.relogiclabs.jschema.message.ErrorCode.ASNDOT02; +import static com.relogiclabs.jschema.message.ErrorCode.ASNVAR01; +import static com.relogiclabs.jschema.message.ErrorCode.ASNVAR02; +import static com.relogiclabs.jschema.message.ErrorCode.ASNVAR03; +import static com.relogiclabs.jschema.message.ErrorCode.CMPSGE01; +import static com.relogiclabs.jschema.message.ErrorCode.CMPSGE02; +import static com.relogiclabs.jschema.message.ErrorCode.CMPSGT01; +import static com.relogiclabs.jschema.message.ErrorCode.CMPSGT02; +import static com.relogiclabs.jschema.message.ErrorCode.CMPSLE01; +import static com.relogiclabs.jschema.message.ErrorCode.CMPSLE02; +import static com.relogiclabs.jschema.message.ErrorCode.CMPSLT01; +import static com.relogiclabs.jschema.message.ErrorCode.CMPSLT02; +import static com.relogiclabs.jschema.message.ErrorCode.DIVASN01; +import static com.relogiclabs.jschema.message.ErrorCode.DIVASN02; +import static com.relogiclabs.jschema.message.ErrorCode.DIVASN03; +import static com.relogiclabs.jschema.message.ErrorCode.MODASN01; +import static com.relogiclabs.jschema.message.ErrorCode.MODASN02; +import static com.relogiclabs.jschema.message.ErrorCode.MODASN03; +import static com.relogiclabs.jschema.message.ErrorCode.MULASN01; +import static com.relogiclabs.jschema.message.ErrorCode.MULASN02; +import static com.relogiclabs.jschema.message.ErrorCode.MULASN03; +import static com.relogiclabs.jschema.message.ErrorCode.OPADDT01; +import static com.relogiclabs.jschema.message.ErrorCode.OPADDT02; +import static com.relogiclabs.jschema.message.ErrorCode.OPANDL01; +import static com.relogiclabs.jschema.message.ErrorCode.OPDIVD01; +import static com.relogiclabs.jschema.message.ErrorCode.OPDIVD02; +import static com.relogiclabs.jschema.message.ErrorCode.OPEQUL01; +import static com.relogiclabs.jschema.message.ErrorCode.OPMINS01; +import static com.relogiclabs.jschema.message.ErrorCode.OPMINS02; +import static com.relogiclabs.jschema.message.ErrorCode.OPMODU01; +import static com.relogiclabs.jschema.message.ErrorCode.OPMODU02; +import static com.relogiclabs.jschema.message.ErrorCode.OPMULT01; +import static com.relogiclabs.jschema.message.ErrorCode.OPMULT02; +import static com.relogiclabs.jschema.message.ErrorCode.OPNEQL01; +import static com.relogiclabs.jschema.message.ErrorCode.OPNOTL01; +import static com.relogiclabs.jschema.message.ErrorCode.OPORLG01; +import static com.relogiclabs.jschema.message.ErrorCode.OPPLUS01; +import static com.relogiclabs.jschema.message.ErrorCode.OPPLUS02; +import static com.relogiclabs.jschema.message.ErrorCode.OPSUBT01; +import static com.relogiclabs.jschema.message.ErrorCode.OPSUBT02; +import static com.relogiclabs.jschema.message.ErrorCode.SUBASN01; +import static com.relogiclabs.jschema.message.ErrorCode.SUBASN02; +import static com.relogiclabs.jschema.message.ErrorCode.SUBASN03; public final class ScriptTreeVisitor3 extends ScriptTreeVisitor2 { public ScriptTreeVisitor3(RuntimeContext runtime) { @@ -107,10 +107,10 @@ public Evaluator visitUnaryPlusExpression(UnaryPlusExpressionContext ctx) { var expr1 = visit(ctx.expression()); return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); - if(!(v1 instanceof ENumber)) throw failOnOperation(PLUS01, + if(!(v1 instanceof ENumber)) throw failOnOperation(OPPLUS01, OpUnaryPlus, v1, ctx.G_PLUS()); return v1; - }, PLUS02, ctx); + }, OPPLUS02, ctx); } @Override @@ -120,15 +120,15 @@ public Evaluator visitUnaryMinusExpression(UnaryMinusExpressionContext ctx) { var v1 = dereference(expr1.evaluate(scope)); if(v1 instanceof EInteger i1) return GInteger.from(-i1.getValue()); if(v1 instanceof ENumber n1) return GDouble.from(-n1.toDouble()); - throw failOnOperation(MINS01, OpUnaryMinus, v1, ctx.G_MINUS()); - }, MINS02, ctx); + throw failOnOperation(OPMINS01, OpUnaryMinus, v1, ctx.G_MINUS()); + }, OPMINS02, ctx); } @Override public Evaluator visitLogicalNotExpression(LogicalNotExpressionContext ctx) { var expr1 = visit(ctx.expression()); return tryCatch(scope -> expr1.evaluate(scope).toBoolean() - ? FALSE : TRUE, NOTL01, ctx); + ? FALSE : TRUE, OPNOTL01, ctx); } @Override @@ -142,8 +142,8 @@ public Evaluator visitMultiplicativeExpression(MultiplicativeExpressionContext c return GInteger.from(i1.getValue() * i2.getValue()); if(v1 instanceof ENumber n1 && v2 instanceof ENumber n2) return GDouble.from(n1.toDouble() * n2.toDouble()); - throw failOnOperation(MULT01, OpMultiplication, v1, v2, ctx.G_MUL()); - }, MULT02, ctx); + throw failOnOperation(OPMULT01, OpMultiplication, v1, v2, ctx.G_MUL()); + }, OPMULT02, ctx); if(ctx.G_DIV() != null) return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); @@ -152,8 +152,8 @@ public Evaluator visitMultiplicativeExpression(MultiplicativeExpressionContext c return GInteger.from(i1.getValue() / i2.getValue()); if(v1 instanceof ENumber n1 && v2 instanceof ENumber n2) return GDouble.from(n1.toDouble() / n2.toDouble()); - throw failOnOperation(DIVD01, OpDivision, v1, v2, ctx.G_DIV()); - }, DIVD02, ctx); + throw failOnOperation(OPDIVD01, OpDivision, v1, v2, ctx.G_DIV()); + }, OPDIVD02, ctx); if(ctx.G_MOD() != null) return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); @@ -162,8 +162,8 @@ public Evaluator visitMultiplicativeExpression(MultiplicativeExpressionContext c return GInteger.from(i1.getValue() % i2.getValue()); if(v1 instanceof ENumber n1 && v2 instanceof ENumber n2) return GDouble.from(n1.toDouble() % n2.toDouble()); - throw failOnOperation(MODU01, OpModulus, v1, v2, ctx.G_MOD()); - }, MODU02, ctx); + throw failOnOperation(OPMODU01, OpModulus, v1, v2, ctx.G_MOD()); + }, OPMODU02, ctx); throw new IllegalStateException("Invalid parser state"); } @@ -181,8 +181,8 @@ public Evaluator visitAdditiveExpression(AdditiveExpressionContext ctx) { return GDouble.from(n1.toDouble() + n2.toDouble()); if(v1 instanceof EString || v2 instanceof EString) return GString.from(stringify(v1) + stringify(v2)); - throw failOnOperation(ADDT01, OpAddition, v1, v2, ctx.G_PLUS()); - }, ADDT02, ctx); + throw failOnOperation(OPADDT01, OpAddition, v1, v2, ctx.G_PLUS()); + }, OPADDT02, ctx); if(ctx.G_MINUS() != null) return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); @@ -191,8 +191,8 @@ public Evaluator visitAdditiveExpression(AdditiveExpressionContext ctx) { return GInteger.from(i1.getValue() - i2.getValue()); if(v1 instanceof ENumber n1 && v2 instanceof ENumber n2) return GDouble.from(n1.toDouble() - n2.toDouble()); - throw failOnOperation(SUBT01, OpSubtraction, v1, v2, ctx.G_MINUS()); - }, SUBT02, ctx); + throw failOnOperation(OPSUBT01, OpSubtraction, v1, v2, ctx.G_MINUS()); + }, OPSUBT02, ctx); throw new IllegalStateException("Invalid parser state"); } @@ -205,41 +205,41 @@ public Evaluator visitRelationalExpression(RelationalExpressionContext ctx) { var v1 = dereference(expr1.evaluate(scope)); var v2 = dereference(expr2.evaluate(scope)); if(!(v1 instanceof ENumber n1) || !(v2 instanceof ENumber n2)) - throw failOnOperation(RELA01, OpComparison, v1, v2, ctx.G_GT()); + throw failOnOperation(CMPSGT01, OpComparison, v1, v2, ctx.G_GT()); double d1 = n1.toDouble(), d2 = n2.toDouble(); if(runtime.areEqual(d1, d2)) return FALSE; return d1 > d2 ? TRUE : FALSE; - }, RELA05, ctx); + }, CMPSGT02, ctx); if(ctx.G_GE() != null) return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); var v2 = dereference(expr2.evaluate(scope)); if(!(v1 instanceof ENumber n1) || !(v2 instanceof ENumber n2)) - throw failOnOperation(RELA02, OpComparison, v1, v2, ctx.G_GE()); + throw failOnOperation(CMPSGE01, OpComparison, v1, v2, ctx.G_GE()); double d1 = n1.toDouble(), d2 = n2.toDouble(); if(runtime.areEqual(d1, d2)) return TRUE; return d1 > d2 ? TRUE : FALSE; - }, RELA06, ctx); + }, CMPSGE02, ctx); if(ctx.G_LT() != null) return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); var v2 = dereference(expr2.evaluate(scope)); if(!(v1 instanceof ENumber n1) || !(v2 instanceof ENumber n2)) - throw failOnOperation(RELA03, OpComparison, v1, v2, ctx.G_LT()); + throw failOnOperation(CMPSLT01, OpComparison, v1, v2, ctx.G_LT()); double d1 = n1.toDouble(), d2 = n2.toDouble(); if(runtime.areEqual(d1, d2)) return FALSE; return d1 < d2 ? TRUE : FALSE; - }, RELA07, ctx); + }, CMPSLT02, ctx); if(ctx.G_LE() != null) return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); var v2 = dereference(expr2.evaluate(scope)); if(!(v1 instanceof ENumber n1) || !(v2 instanceof ENumber n2)) - throw failOnOperation(RELA04, OpComparison, v1, v2, ctx.G_LE()); + throw failOnOperation(CMPSLE01, OpComparison, v1, v2, ctx.G_LE()); double d1 = n1.toDouble(), d2 = n2.toDouble(); if(runtime.areEqual(d1, d2)) return TRUE; return d1 < d2 ? TRUE : FALSE; - }, RELA08, ctx); + }, CMPSLE02, ctx); throw new IllegalStateException("Invalid parser state"); } @@ -252,13 +252,13 @@ public Evaluator visitEqualityExpression(EqualityExpressionContext ctx) { var v1 = dereference(expr1.evaluate(scope)); var v2 = dereference(expr2.evaluate(scope)); return areEqual(v1, v2, runtime) ? TRUE : FALSE; - }, EQUL01, ctx); + }, OPEQUL01, ctx); if(ctx.G_NE() != null) return tryCatch(scope -> { var v1 = dereference(expr1.evaluate(scope)); var v2 = dereference(expr2.evaluate(scope)); return areEqual(v1, v2, runtime) ? FALSE : TRUE; - }, NEQL01, ctx); + }, OPNEQL01, ctx); throw new IllegalStateException("Invalid parser state"); } @@ -271,7 +271,7 @@ public Evaluator visitLogicalAndExpression(LogicalAndExpressionContext ctx) { var v1 = expr1.evaluate(scope); if(!v1.toBoolean()) return v1; return expr2.evaluate(scope); - }, ANDL01, ctx); + }, OPANDL01, ctx); } @Override @@ -282,7 +282,7 @@ public Evaluator visitLogicalOrExpression(LogicalOrExpressionContext ctx) { var v1 = expr1.evaluate(scope); if(v1.toBoolean()) return v1; return expr2.evaluate(scope); - }, ORLG01, ctx); + }, OPORLG01, ctx); } @Override @@ -292,7 +292,7 @@ public Evaluator visitAssignmentAugExpression(AssignmentAugExpressionContext ctx if(ctx.G_ADD_ASSIGN() != null) return tryCatch(scope -> { var v2 = dereference(expr2.evaluate(scope)); if(!(lvalue1.evaluate(scope) instanceof GLeftValue l1)) - throw failOnInvalidLeftValueAssignment(ADDN01, ctx.getStart()); + throw failOnInvalidLeftValueAssignment(ADDASN01, ctx.getStart()); var v1 = l1.getValue(); if(v1 instanceof EInteger i1 && v2 instanceof EInteger i2) { var v3 = GInteger.from(i1.getValue() + i2.getValue()); @@ -309,13 +309,13 @@ public Evaluator visitAssignmentAugExpression(AssignmentAugExpressionContext ctx l1.setValue(v3); return v3; } - throw failOnOperation(ADDN02, OpAdditionAssignment, v1, v2, ctx.G_ADD_ASSIGN()); - }, ADDN03, ctx); + throw failOnOperation(ADDASN02, OpAdditionAssignment, v1, v2, ctx.G_ADD_ASSIGN()); + }, ADDASN03, ctx); if(ctx.G_SUB_ASSIGN() != null) return tryCatch(scope -> { var v2 = dereference(expr2.evaluate(scope)); if(!(lvalue1.evaluate(scope) instanceof GLeftValue l1)) - throw failOnInvalidLeftValueAssignment(SUBN01, ctx.getStart()); + throw failOnInvalidLeftValueAssignment(SUBASN01, ctx.getStart()); var v1 = l1.getValue(); if(v1 instanceof EInteger i1 && v2 instanceof EInteger i2) { var v3 = GInteger.from(i1.getValue() - i2.getValue()); @@ -327,13 +327,13 @@ public Evaluator visitAssignmentAugExpression(AssignmentAugExpressionContext ctx l1.setValue(v3); return v3; } - throw failOnOperation(SUBN02, OpSubtractionAssignment, v1, v2, ctx.G_SUB_ASSIGN()); - }, SUBN03, ctx); + throw failOnOperation(SUBASN02, OpSubtractionAssignment, v1, v2, ctx.G_SUB_ASSIGN()); + }, SUBASN03, ctx); if(ctx.G_MUL_ASSIGN() != null) return tryCatch(scope -> { var v2 = dereference(expr2.evaluate(scope)); if(!(lvalue1.evaluate(scope) instanceof GLeftValue l1)) - throw failOnInvalidLeftValueAssignment(MULN01, ctx.getStart()); + throw failOnInvalidLeftValueAssignment(MULASN01, ctx.getStart()); var v1 = l1.getValue(); if(v1 instanceof EInteger i1 && v2 instanceof EInteger i2) { var v3 = GInteger.from(i1.getValue() * i2.getValue()); @@ -345,13 +345,13 @@ public Evaluator visitAssignmentAugExpression(AssignmentAugExpressionContext ctx l1.setValue(v3); return v3; } - throw failOnOperation(MULN02, OpMultiplicationAssignment, v1, v2, ctx.G_MUL_ASSIGN()); - }, MULN03, ctx); + throw failOnOperation(MULASN02, OpMultiplicationAssignment, v1, v2, ctx.G_MUL_ASSIGN()); + }, MULASN03, ctx); if(ctx.G_DIV_ASSIGN() != null) return tryCatch(scope -> { var v2 = dereference(expr2.evaluate(scope)); if(!(lvalue1.evaluate(scope) instanceof GLeftValue l1)) - throw failOnInvalidLeftValueAssignment(DIVN01, ctx.getStart()); + throw failOnInvalidLeftValueAssignment(DIVASN01, ctx.getStart()); var v1 = l1.getValue(); if(v1 instanceof EInteger i1 && v2 instanceof EInteger i2) { var v3 = GInteger.from(i1.getValue() / i2.getValue()); @@ -363,13 +363,13 @@ public Evaluator visitAssignmentAugExpression(AssignmentAugExpressionContext ctx l1.setValue(v3); return v3; } - throw failOnOperation(DIVN02, OpDivisionAssignment, v1, v2, ctx.G_DIV_ASSIGN()); - }, DIVN03, ctx); + throw failOnOperation(DIVASN02, OpDivisionAssignment, v1, v2, ctx.G_DIV_ASSIGN()); + }, DIVASN03, ctx); if(ctx.G_MOD_ASSIGN() != null) return tryCatch(scope -> { var v2 = dereference(expr2.evaluate(scope)); if(!(lvalue1.evaluate(scope) instanceof GLeftValue l1)) - throw failOnInvalidLeftValueAssignment(MODN01, ctx.getStart()); + throw failOnInvalidLeftValueAssignment(MODASN01, ctx.getStart()); var v1 = l1.getValue(); if(v1 instanceof EInteger i1 && v2 instanceof EInteger i2) { var v3 = GInteger.from(i1.getValue() % i2.getValue()); @@ -381,8 +381,8 @@ public Evaluator visitAssignmentAugExpression(AssignmentAugExpressionContext ctx l1.setValue(v3); return v3; } - throw failOnOperation(MODN02, OpModulusAssignment, v1, v2, ctx.G_MOD_ASSIGN()); - }, MODN03, ctx); + throw failOnOperation(MODASN02, OpModulusAssignment, v1, v2, ctx.G_MOD_ASSIGN()); + }, MODASN03, ctx); throw new IllegalStateException("Invalid parser state"); } @@ -406,7 +406,7 @@ public Evaluator visitAssignmentBracketExpression(AssignmentBracketExpressionCon } throwBracketUpdateException(v1, v2, ctx); throw new IllegalStateException("Invalid runtime state"); - }, ASIN01, ctx); + }, ASNBKT01, ctx); } @Override @@ -422,8 +422,8 @@ public Evaluator visitAssignmentDotExpression(AssignmentDotExpressionContext ctx o1.set(ids2, v3); return v3; } - throw failOnOperation(ASIN02, OpPropertyAssignment, v1, ctx.G_DOT()); - }, ASIN03, ctx); + throw failOnOperation(ASNDOT01, OpPropertyAssignment, v1, ctx.G_DOT()); + }, ASNDOT02, ctx); } @Override @@ -434,12 +434,12 @@ public Evaluator visitAssignmentIdExpression(AssignmentIdExpressionContext ctx) return tryCatch(scope -> { var v2 = dereference(expr2.evaluate(scope)); var v1 = scope.resolve(ids1); - if(v1 == null) throw failOnIdentifierNotFound(IDEN03, id1.getSymbol()); + if(v1 == null) throw failOnVariableNotFound(ASNVAR01, id1.getSymbol()); if(!(v1 instanceof GLeftValue l1)) - throw failOnInvalidLeftValueAssignment(ASIN04, id1.getSymbol()); + throw failOnInvalidLeftValueAssignment(ASNVAR02, id1.getSymbol()); l1.setValue(v2); return v2; - }, ASIN05, ctx); + }, ASNVAR03, ctx); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/ArrayLibrary.java b/src/main/java/com/relogiclabs/jschema/internal/library/ArrayMethods.java similarity index 63% rename from src/main/java/com/relogiclabs/jschema/internal/library/ArrayLibrary.java rename to src/main/java/com/relogiclabs/jschema/internal/library/ArrayMethods.java index b95b16a..054d103 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/library/ArrayLibrary.java +++ b/src/main/java/com/relogiclabs/jschema/internal/library/ArrayMethods.java @@ -1,27 +1,25 @@ package com.relogiclabs.jschema.internal.library; +import com.relogiclabs.jschema.exception.InvalidArgumentException; import com.relogiclabs.jschema.internal.engine.ScriptScope; import com.relogiclabs.jschema.internal.script.GArray; import com.relogiclabs.jschema.internal.script.GInteger; import com.relogiclabs.jschema.type.EArray; -import com.relogiclabs.jschema.type.EInteger; -import com.relogiclabs.jschema.type.EType; import com.relogiclabs.jschema.type.EValue; import lombok.Getter; import java.util.List; import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.areEqual; -import static com.relogiclabs.jschema.message.ErrorCode.AFND01; -import static com.relogiclabs.jschema.message.ErrorCode.FILL01; +import static com.relogiclabs.jschema.internal.library.LibraryHelper.asInteger; +import static com.relogiclabs.jschema.message.ErrorCode.FILARR01; +import static com.relogiclabs.jschema.message.ErrorCode.FNDARR01; import static com.relogiclabs.jschema.type.EUndefined.UNDEFINED; -public final class ArrayLibrary extends CommonLibrary { +public final class ArrayMethods extends CommonMethods { private static final String Length_M0 = "length#0"; - private static final String Find_Bn = "find"; private static final String Find_M1 = "find#1"; private static final String Find_M2 = "find#2"; - private static final String Fill_Bn = "fill"; private static final String Fill_M2 = "fill#2"; private static final String Copy_M0 = "copy#0"; @@ -29,19 +27,14 @@ public final class ArrayLibrary extends CommonLibrary { private static final String Length_Id = "length"; @Getter - private static final ArrayLibrary instance = new ArrayLibrary(); + private static final ArrayMethods instance = new ArrayMethods(); - private ArrayLibrary() { - addMethod(Length_M0, ArrayLibrary::lengthMethod); - addMethod(Find_M1, ArrayLibrary::findMethod1); - addMethod(Find_M2, ArrayLibrary::findMethod2); - addMethod(Fill_M2, ArrayLibrary::fillMethod); - addMethod(Copy_M0, ArrayLibrary::copyMethod); - } - - @Override - protected EType getType() { - return EType.ARRAY; + private ArrayMethods() { + addMethod(Length_M0, ArrayMethods::lengthMethod); + addMethod(Find_M1, ArrayMethods::findMethod1); + addMethod(Find_M2, ArrayMethods::findMethod2); + addMethod(Fill_M2, ArrayMethods::fillMethod); + addMethod(Copy_M0, ArrayMethods::copyMethod); } private static GInteger lengthMethod(EValue self, List arguments, ScriptScope scope) { @@ -61,18 +54,26 @@ private static EValue findMethod2(EValue self, List arguments, ScriptSco var runtime = scope.getRuntime(); var array = (EArray) self; var value = arguments.get(0); - if(!(arguments.get(1) instanceof EInteger a1)) throw failOnInvalidArgumentType(AFND01, - arguments.get(1), Find_Bn, Start_Id, self); - var start = (int) a1.getValue(); + int start; + try { + start = asInteger(arguments.get(1)); + } catch(InvalidArgumentException e) { + e.setContext(FNDARR01, Start_Id); + throw e.failWithMethodException(self); + } for(var i = start; i < array.size(); i++) if(areEqual(array.get(i), value, runtime)) return GInteger.from(i); return UNDEFINED; } private static EValue fillMethod(EValue self, List arguments, ScriptScope scope) { - if(!(arguments.get(1) instanceof EInteger a1)) throw failOnInvalidArgumentType(FILL01, - arguments.get(1), Fill_Bn, Length_Id, self); - var length = (int) a1.getValue(); + int length; + try { + length = asInteger(arguments.get(1)); + } catch(InvalidArgumentException e) { + e.setContext(FILARR01, Length_Id); + throw e.failWithMethodException(self); + } return GArray.filledFrom(arguments.get(0), length); } diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/CommonLibrary.java b/src/main/java/com/relogiclabs/jschema/internal/library/CommonLibrary.java deleted file mode 100644 index 57ff141..0000000 --- a/src/main/java/com/relogiclabs/jschema/internal/library/CommonLibrary.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.relogiclabs.jschema.internal.library; - -import com.relogiclabs.jschema.exception.ScriptArgumentException; -import com.relogiclabs.jschema.exception.ScriptCommonException; -import com.relogiclabs.jschema.internal.engine.ScriptScope; -import com.relogiclabs.jschema.internal.script.GString; -import com.relogiclabs.jschema.type.EType; -import com.relogiclabs.jschema.type.EValue; -import lombok.Getter; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.stringify; -import static com.relogiclabs.jschema.internal.tree.EFunction.VARIADIC_ARITY; -import static com.relogiclabs.jschema.message.ErrorCode.MNVK01; - -public class CommonLibrary { - private static final String Type_M0 = "type#0"; - private static final String String_M0 = "string#0"; - private final Map methods; - - @Getter - private static final CommonLibrary instance = new CommonLibrary(); - - CommonLibrary() { - this.methods = new HashMap<>(20); - addMethod(Type_M0, CommonLibrary::typeMethod); - addMethod(String_M0, CommonLibrary::stringMethod); - } - - public MethodEvaluator getMethod(String name, int argCount) { - var method = methods.get(name + "#" + argCount); - if(method != null) return method; - method = methods.get(name + "#" + VARIADIC_ARITY); - if(method != null) return method; - throw failOnMethodNotFound(name, argCount, getType()); - } - - protected void addMethod(String name, MethodEvaluator evaluator) { - methods.put(name, evaluator); - } - - protected EType getType() { - return EType.ANY; - } - - private static GString typeMethod(EValue self, List arguments, ScriptScope scope) { - return GString.from(self.getType().getName()); - } - - private static GString stringMethod(EValue self, List arguments, ScriptScope scope) { - return GString.from(stringify(self)); - } - - private static ScriptCommonException failOnMethodNotFound(String name, int argCount, EType type) { - return new ScriptCommonException(MNVK01, "Method '" + name + "' with " + argCount - + " parameter(s) of " + type + " not found"); - } - - protected static ScriptArgumentException failOnInvalidArgumentType(String code, EValue argument, - String method, String parameter, EValue self) { - return new ScriptArgumentException(code, "Invalid " + argument.getType() + " argument for '" - + parameter + "' parameter in '" + method + "' method of " + self.getType()); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/CommonMethods.java b/src/main/java/com/relogiclabs/jschema/internal/library/CommonMethods.java new file mode 100644 index 0000000..e84bb32 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/internal/library/CommonMethods.java @@ -0,0 +1,55 @@ +package com.relogiclabs.jschema.internal.library; + +import com.relogiclabs.jschema.exception.MethodNotFoundException; +import com.relogiclabs.jschema.internal.engine.ScriptScope; +import com.relogiclabs.jschema.internal.script.GString; +import com.relogiclabs.jschema.type.EValue; +import lombok.Getter; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.stringify; +import static com.relogiclabs.jschema.internal.tree.EFunction.VARIADIC_ARITY; +import static com.relogiclabs.jschema.message.ErrorCode.MTHNVK01; + +public class CommonMethods { + private static final String Type_M0 = "type#0"; + private static final String String_M0 = "string#0"; + private final Map methods; + + @Getter + private static final CommonMethods instance = new CommonMethods(); + + CommonMethods() { + this.methods = new HashMap<>(20); + addMethod(Type_M0, CommonMethods::typeMethod); + addMethod(String_M0, CommonMethods::stringMethod); + } + + public MethodEvaluator getMethod(String name, int argCount) { + var method = methods.get(name + "#" + argCount); + if(method != null) return method; + method = methods.get(name + "#" + VARIADIC_ARITY); + if(method != null) return method; + throw failOnMethodNotFound(name, argCount); + } + + protected void addMethod(String methodId, MethodEvaluator body) { + methods.put(methodId, body); + } + + private static GString typeMethod(EValue self, List arguments, ScriptScope scope) { + return GString.from(self.getType().getName()); + } + + private static GString stringMethod(EValue self, List arguments, ScriptScope scope) { + return GString.from(stringify(self)); + } + + private static MethodNotFoundException failOnMethodNotFound(String name, int argCount) { + return new MethodNotFoundException(MTHNVK01, "Method '" + name + "' with " + argCount + + " parameter(s) not found"); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions1.java b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions1.java new file mode 100644 index 0000000..474687a --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions1.java @@ -0,0 +1,41 @@ +package com.relogiclabs.jschema.internal.library; + +import com.relogiclabs.jschema.internal.engine.ScriptScope; +import com.relogiclabs.jschema.internal.engine.ScriptTreeHelper; +import com.relogiclabs.jschema.internal.script.GInteger; +import com.relogiclabs.jschema.type.EValue; + +import java.util.List; + +import static com.relogiclabs.jschema.type.EValue.VOID; + +public abstract class LibraryFunctions1 { + static final String Print_F1 = "print#1"; + static final String Fail_F1 = "fail#1"; + static final String Fail_F2 = "fail#2"; + static final String Fail_F4 = "fail#4"; + static final String Expected_F1 = "expected#1"; + static final String Expected_F2 = "expected#2"; + static final String Actual_F1 = "actual#1"; + static final String Actual_F2 = "actual#2"; + static final String Ticks_F0 = "ticks#0"; + + static final String Message_Id = "message"; + static final String Code_Id = "code"; + static final String Expected_Id = "expected"; + static final String Actual_Id = "actual"; + static final String Node_Id = "node"; + + static void fail(ScriptScope scope, RuntimeException exception) { + scope.getRuntime().getExceptions().fail(exception); + } + + static EValue printFunction(ScriptScope scope, List arguments) { + System.out.println(ScriptTreeHelper.stringify(arguments.get(0))); + return VOID; + } + + static EValue ticksFunction(ScriptScope scope, List arguments) { + return GInteger.from(System.currentTimeMillis()); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions2.java b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions2.java new file mode 100644 index 0000000..688759e --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions2.java @@ -0,0 +1,214 @@ +package com.relogiclabs.jschema.internal.library; + +import com.relogiclabs.jschema.exception.FunctionValidationException; +import com.relogiclabs.jschema.exception.InvalidArgumentException; +import com.relogiclabs.jschema.internal.engine.ScriptScope; +import com.relogiclabs.jschema.internal.script.GObject; +import com.relogiclabs.jschema.message.ActualDetail; +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.message.ExpectedDetail; +import com.relogiclabs.jschema.node.JNode; +import com.relogiclabs.jschema.type.EObject; +import com.relogiclabs.jschema.type.EString; +import com.relogiclabs.jschema.type.EValue; + +import java.util.List; + +import static com.relogiclabs.jschema.internal.library.LibraryHelper.asString; +import static com.relogiclabs.jschema.internal.library.LibraryHelper.cast; +import static com.relogiclabs.jschema.internal.library.LibraryHelper.castMember; +import static com.relogiclabs.jschema.internal.script.GBoolean.FALSE; +import static com.relogiclabs.jschema.internal.tree.ScriptFunction.CALLER_HVAR; +import static com.relogiclabs.jschema.internal.tree.ScriptFunction.TARGET_HVAR; +import static com.relogiclabs.jschema.internal.util.CommonHelper.nonNullFrom; +import static com.relogiclabs.jschema.message.ErrorCode.ACTLSF01; +import static com.relogiclabs.jschema.message.ErrorCode.ACTLSF02; +import static com.relogiclabs.jschema.message.ErrorCode.ACTLSF03; +import static com.relogiclabs.jschema.message.ErrorCode.EXPTSF01; +import static com.relogiclabs.jschema.message.ErrorCode.EXPTSF02; +import static com.relogiclabs.jschema.message.ErrorCode.EXPTSF03; +import static com.relogiclabs.jschema.message.ErrorCode.FAILBS01; +import static com.relogiclabs.jschema.message.ErrorCode.FAILBS02; +import static com.relogiclabs.jschema.message.ErrorCode.FAILBS03; +import static com.relogiclabs.jschema.message.ErrorCode.FAILDF01; +import static com.relogiclabs.jschema.message.ErrorCode.FAILEX01; +import static com.relogiclabs.jschema.message.ErrorCode.FAILEX02; +import static com.relogiclabs.jschema.message.ErrorCode.FAILEX03; +import static com.relogiclabs.jschema.message.ErrorCode.FAILEX04; +import static com.relogiclabs.jschema.message.ErrorCode.FAILMB01; +import static com.relogiclabs.jschema.message.ErrorCode.FAILMB02; +import static com.relogiclabs.jschema.message.ErrorCode.FAILMB03; +import static com.relogiclabs.jschema.message.ErrorCode.FAILMB04; +import static com.relogiclabs.jschema.message.MessageFormatter.formatForBoth; +import static com.relogiclabs.jschema.type.EValue.VOID; + +public abstract class LibraryFunctions2 extends LibraryFunctions1 { + static EValue failFunction1(ScriptScope scope, List arguments) { + var caller = (JNode) scope.resolve(CALLER_HVAR); + var target = (JNode) scope.resolve(TARGET_HVAR); + String message; + try { + message = asString(arguments.get(0)); + } catch(InvalidArgumentException e) { + e.setContext(FAILBS01, Message_Id); + throw e.failWithFunctionException(); + } + fail(scope, new FunctionValidationException(formatForBoth(FAILDF01, + message, caller, target), caller, target)); + return FALSE; + } + + static EValue failFunction2(ScriptScope scope, List arguments) { + var caller = (JNode) scope.resolve(CALLER_HVAR); + var target = (JNode) scope.resolve(TARGET_HVAR); + String code, message; + try { + code = asString(arguments.get(0)); + } catch(InvalidArgumentException e) { + e.setContext(FAILBS02, Code_Id); + throw e.failWithFunctionException(); + } + try { + message = asString(arguments.get(1)); + } catch(InvalidArgumentException e) { + e.setContext(FAILBS03, Message_Id); + throw e.failWithFunctionException(); + } + fail(scope, new FunctionValidationException(formatForBoth(code, + message, caller, target), caller, target)); + return FALSE; + } + + static EValue failFunction4(ScriptScope scope, List arguments) { + EObject expected, actual; + try { + expected = cast(arguments.get(2), EObject.class); + } catch(InvalidArgumentException e) { + e.setContext(FAILEX01, Expected_Id); + throw e.failWithFunctionException(); + } + try { + actual = cast(arguments.get(3), EObject.class); + } catch(InvalidArgumentException e) { + e.setContext(FAILEX02, Actual_Id); + throw e.failWithFunctionException(); + } + + String code, message; + try { + code = asString(arguments.get(0)); + } catch(InvalidArgumentException e) { + e.setContext(FAILEX03, Code_Id); + throw e.failWithFunctionException(); + } + try { + message = asString(arguments.get(1)); + } catch(InvalidArgumentException e) { + e.setContext(FAILEX04, Message_Id); + throw e.failWithFunctionException(); + } + + JNode expectedNode, actualNode; + String expectedMessage, actualMessage; + try { + expectedNode = castMember(expected, Node_Id, JNode.class); + } catch(InvalidArgumentException e) { + e.setContext(FAILMB01, Expected_Id); + throw e.failWithFunctionException(); + } + try { + expectedMessage = castMember(expected, Message_Id, EString.class).getValue(); + } catch(InvalidArgumentException e) { + e.setContext(FAILMB02, Expected_Id); + throw e.failWithFunctionException(); + } + + try { + actualNode = castMember(actual, Node_Id, JNode.class); + } catch(InvalidArgumentException e) { + e.setContext(FAILMB03, Actual_Id); + throw e.failWithFunctionException(); + } + try { + actualMessage = castMember(actual, Message_Id, EString.class).getValue(); + } catch(InvalidArgumentException e) { + e.setContext(FAILMB04, Actual_Id); + throw e.failWithFunctionException(); + } + fail(scope, new FunctionValidationException( + new ErrorDetail(code, message), + new ExpectedDetail(expectedNode, expectedMessage), + new ActualDetail(actualNode, actualMessage))); + return FALSE; + } + + static EValue expectedFunction1(ScriptScope scope, List arguments) { + var result = new GObject(2); + EString message; + try { + message = cast(arguments.get(0), EString.class); + } catch(InvalidArgumentException e) { + e.setContext(EXPTSF01, Message_Id); + throw e.failWithFunctionException(); + } + result.put(Node_Id, nonNullFrom(scope.resolve(CALLER_HVAR), VOID)); + result.put(Message_Id, message); + return result; + } + + static EValue expectedFunction2(ScriptScope scope, List arguments) { + var result = new GObject(2); + JNode node; + EString message; + try { + node = cast(arguments.get(0), JNode.class); + } catch(InvalidArgumentException e) { + e.setContext(EXPTSF02, Node_Id); + throw e.failWithFunctionException(); + } + try { + message = cast(arguments.get(1), EString.class); + } catch(InvalidArgumentException e) { + e.setContext(EXPTSF03, Message_Id); + throw e.failWithFunctionException(); + } + result.put(Node_Id, node); + result.put(Message_Id, message); + return result; + } + + static EValue actualFunction1(ScriptScope scope, List arguments) { + var result = new GObject(2); + EString message; + try { + message = cast(arguments.get(0), EString.class); + } catch(InvalidArgumentException e) { + e.setContext(ACTLSF01, Message_Id); + throw e.failWithFunctionException(); + } + result.put(Node_Id, nonNullFrom(scope.resolve(TARGET_HVAR), VOID)); + result.put(Message_Id, message); + return result; + } + + static EValue actualFunction2(ScriptScope scope, List arguments) { + var result = new GObject(2); + JNode node; + EString message; + try { + node = cast(arguments.get(0), JNode.class); + } catch(InvalidArgumentException e) { + e.setContext(ACTLSF02, Node_Id); + throw e.failWithFunctionException(); + } + try { + message = cast(arguments.get(1), EString.class); + } catch(InvalidArgumentException e) { + e.setContext(ACTLSF03, Message_Id); + throw e.failWithFunctionException(); + } + result.put(Node_Id, node); + result.put(Message_Id, message); + return result; + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary3.java b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions3.java similarity index 57% rename from src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary3.java rename to src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions3.java index ff06885..033a181 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary3.java +++ b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions3.java @@ -13,19 +13,19 @@ import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.areCompatible; import static com.relogiclabs.jschema.internal.script.RFunction.hasVariadic; -import static com.relogiclabs.jschema.message.ErrorCode.FNVK03; +import static com.relogiclabs.jschema.message.ErrorCode.FNSNVK03; -public final class ScriptLibrary3 extends ScriptLibrary2 { - private static final ScriptLibrary3 LIBRARY = new ScriptLibrary3(); +public final class LibraryFunctions3 extends LibraryFunctions2 { + private static final LibraryFunctions3 LIBRARY = new LibraryFunctions3(); private final Map functions; @Getter - private static class LibraryFunction implements RFunction { + private static class BuiltinFunction implements RFunction { private final GParameter[] parameters; private final boolean variadic; private final FunctionEvaluator body; - public LibraryFunction(FunctionEvaluator body, String... parameters) { + public BuiltinFunction(FunctionEvaluator body, String... parameters) { this.body = body; this.parameters = toParameters(parameters); this.variadic = hasVariadic(this.parameters); @@ -37,7 +37,7 @@ private static GParameter[] toParameters(String... names) { @Override public ScriptScope bind(ScriptScope parentScope, List arguments) { - areCompatible(parameters, arguments, FNVK03); + areCompatible(parameters, arguments, FNSNVK03); return parentScope; } @@ -47,21 +47,21 @@ public EValue invoke(ScriptScope functionScope, List arguments) { } } - private ScriptLibrary3() { + private LibraryFunctions3() { functions = new HashMap<>(10); - addFunction(Print_F1, ScriptLibrary2::printFunction, Message_Id); - addFunction(Fail_F1, ScriptLibrary2::failFunction1, Message_Id); - addFunction(Fail_F2, ScriptLibrary2::failFunction2, Code_Id, Message_Id); - addFunction(Fail_F4, ScriptLibrary2::failFunction4, Code_Id, Message_Id, Expected_Id, Actual_Id); - addFunction(Expected_F1, ScriptLibrary2::expectedFunction1, Message_Id); - addFunction(Expected_F2, ScriptLibrary2::expectedFunction2, Node_Id, Message_Id); - addFunction(Actual_F1, ScriptLibrary2::actualFunction1, Message_Id); - addFunction(Actual_F2, ScriptLibrary2::actualFunction2, Node_Id, Message_Id); - addFunction(Ticks_F0, ScriptLibrary2::ticksFunction); + addFunction(Print_F1, LibraryFunctions2::printFunction, Message_Id); + addFunction(Fail_F1, LibraryFunctions2::failFunction1, Message_Id); + addFunction(Fail_F2, LibraryFunctions2::failFunction2, Code_Id, Message_Id); + addFunction(Fail_F4, LibraryFunctions2::failFunction4, Code_Id, Message_Id, Expected_Id, Actual_Id); + addFunction(Expected_F1, LibraryFunctions2::expectedFunction1, Message_Id); + addFunction(Expected_F2, LibraryFunctions2::expectedFunction2, Node_Id, Message_Id); + addFunction(Actual_F1, LibraryFunctions2::actualFunction1, Message_Id); + addFunction(Actual_F2, LibraryFunctions2::actualFunction2, Node_Id, Message_Id); + addFunction(Ticks_F0, LibraryFunctions2::ticksFunction); } private void addFunction(String functionId, FunctionEvaluator body, String... parameters) { - functions.put(functionId, new LibraryFunction(body, parameters)); + functions.put(functionId, new BuiltinFunction(body, parameters)); } public static EValue resolveStatic(String name) { diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/LibraryHelper.java b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryHelper.java new file mode 100644 index 0000000..cc216af --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryHelper.java @@ -0,0 +1,57 @@ +package com.relogiclabs.jschema.internal.library; + +import com.relogiclabs.jschema.exception.ArgumentTypeException; +import com.relogiclabs.jschema.exception.ArgumentValueException; +import com.relogiclabs.jschema.exception.InvalidArgumentException; +import com.relogiclabs.jschema.type.EInteger; +import com.relogiclabs.jschema.type.ENumber; +import com.relogiclabs.jschema.type.EObject; +import com.relogiclabs.jschema.type.EString; +import com.relogiclabs.jschema.type.EValue; + +import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.dereference; +import static com.relogiclabs.jschema.message.OutlineFormatter.createOutline; + +public class LibraryHelper { + private LibraryHelper() { + throw new UnsupportedOperationException("This class is not intended for instantiation"); + } + + static int asInteger(EValue value) { + return (int) asLong(value); + } + + static long asLong(EValue value) { + if(!(value instanceof EInteger i)) throw failOnInvalidArgumentType(value); + return i.getValue(); + } + + static double asDouble(EValue value) { + if(!(value instanceof ENumber n)) throw failOnInvalidArgumentType(value); + return n.toDouble(); + } + + static String asString(EValue value) { + if(!(value instanceof EString s)) throw failOnInvalidArgumentType(value); + return s.getValue(); + } + + static T cast(EValue value, Class type) { + if(!type.isInstance(value)) throw failOnInvalidArgumentType(value); + return type.cast(value); + } + + static T castMember(EObject object, String key, Class type) { + var value = dereference(object.get(key)); + if(!type.isInstance(value)) throw failOnInvalidArgumentValue(object); + return type.cast(value); + } + + private static InvalidArgumentException failOnInvalidArgumentType(EValue argument) { + return new ArgumentTypeException("Invalid argument type " + argument.getType()); + } + + private static InvalidArgumentException failOnInvalidArgumentValue(EValue argument) { + return new ArgumentValueException("Invalid argument value " + createOutline(argument)); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/NumberLibrary.java b/src/main/java/com/relogiclabs/jschema/internal/library/NumberMethods.java similarity index 66% rename from src/main/java/com/relogiclabs/jschema/internal/library/NumberLibrary.java rename to src/main/java/com/relogiclabs/jschema/internal/library/NumberMethods.java index 2338e4e..415f98d 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/library/NumberLibrary.java +++ b/src/main/java/com/relogiclabs/jschema/internal/library/NumberMethods.java @@ -1,20 +1,20 @@ package com.relogiclabs.jschema.internal.library; +import com.relogiclabs.jschema.exception.InvalidArgumentException; import com.relogiclabs.jschema.internal.engine.ScriptScope; import com.relogiclabs.jschema.internal.script.GDouble; import com.relogiclabs.jschema.internal.script.GInteger; import com.relogiclabs.jschema.type.EInteger; import com.relogiclabs.jschema.type.ENumber; -import com.relogiclabs.jschema.type.EType; import com.relogiclabs.jschema.type.EValue; import lombok.Getter; import java.util.List; -import static com.relogiclabs.jschema.message.ErrorCode.POWR01; +import static com.relogiclabs.jschema.internal.library.LibraryHelper.asDouble; +import static com.relogiclabs.jschema.message.ErrorCode.POWNUM01; -public final class NumberLibrary extends CommonLibrary { - private static final String Pow_Bn = "pow"; +public final class NumberMethods extends CommonMethods { private static final String Pow_M1 = "pow#1"; private static final String Log_M0 = "log#0"; private static final String Ceil_M0 = "ceil#0"; @@ -24,25 +24,25 @@ public final class NumberLibrary extends CommonLibrary { private static final String Value_Id = "value"; @Getter - private static final NumberLibrary instance = new NumberLibrary(); + private static final NumberMethods instance = new NumberMethods(); - private NumberLibrary() { - addMethod(Pow_M1, NumberLibrary::powMethod); - addMethod(Log_M0, NumberLibrary::logMethod); - addMethod(Ceil_M0, NumberLibrary::ceilMethod); - addMethod(Floor_M0, NumberLibrary::floorMethod); - addMethod(Copy_M0, NumberLibrary::copyMethod); - } - - @Override - protected EType getType() { - return EType.NUMBER; + private NumberMethods() { + addMethod(Pow_M1, NumberMethods::powMethod); + addMethod(Log_M0, NumberMethods::logMethod); + addMethod(Ceil_M0, NumberMethods::ceilMethod); + addMethod(Floor_M0, NumberMethods::floorMethod); + addMethod(Copy_M0, NumberMethods::copyMethod); } private static GDouble powMethod(EValue self, List arguments, ScriptScope scope) { - if(!(arguments.get(0) instanceof ENumber a0)) throw failOnInvalidArgumentType(POWR01, - arguments.get(0), Pow_Bn, Value_Id, self); - return GDouble.from(Math.pow(((ENumber) self).toDouble(), a0.toDouble())); + double value; + try { + value = asDouble(arguments.get(0)); + } catch(InvalidArgumentException e) { + e.setContext(POWNUM01, Value_Id); + throw e.failWithMethodException(self); + } + return GDouble.from(Math.pow(((ENumber) self).toDouble(), value)); } private static GDouble logMethod(EValue self, List arguments, ScriptScope scope) { diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/ObjectLibrary.java b/src/main/java/com/relogiclabs/jschema/internal/library/ObjectMethods.java similarity index 65% rename from src/main/java/com/relogiclabs/jschema/internal/library/ObjectLibrary.java rename to src/main/java/com/relogiclabs/jschema/internal/library/ObjectMethods.java index a19a031..2a80d5f 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/library/ObjectLibrary.java +++ b/src/main/java/com/relogiclabs/jschema/internal/library/ObjectMethods.java @@ -4,29 +4,23 @@ import com.relogiclabs.jschema.internal.script.GInteger; import com.relogiclabs.jschema.internal.script.GObject; import com.relogiclabs.jschema.type.EObject; -import com.relogiclabs.jschema.type.EType; import com.relogiclabs.jschema.type.EValue; import lombok.Getter; import java.util.List; -public final class ObjectLibrary extends CommonLibrary { +public final class ObjectMethods extends CommonMethods { private static final String Size_M0 = "size#0"; private static final String Length_M0 = "length#0"; private static final String Copy_M0 = "copy#0"; @Getter - private static final ObjectLibrary instance = new ObjectLibrary(); + private static final ObjectMethods instance = new ObjectMethods(); - private ObjectLibrary() { - addMethod(Size_M0, ObjectLibrary::sizeMethod); - addMethod(Length_M0, ObjectLibrary::sizeMethod); - addMethod(Copy_M0, ObjectLibrary::copyMethod); - } - - @Override - protected EType getType() { - return EType.OBJECT; + private ObjectMethods() { + addMethod(Size_M0, ObjectMethods::sizeMethod); + addMethod(Length_M0, ObjectMethods::sizeMethod); + addMethod(Copy_M0, ObjectMethods::copyMethod); } private static GInteger sizeMethod(EValue self, List arguments, ScriptScope scope) { diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary1.java b/src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary1.java deleted file mode 100644 index b4ea28a..0000000 --- a/src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary1.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.relogiclabs.jschema.internal.library; - -import com.relogiclabs.jschema.exception.ScriptArgumentException; -import com.relogiclabs.jschema.internal.engine.ScriptScope; -import com.relogiclabs.jschema.type.EInteger; -import com.relogiclabs.jschema.type.ENumber; -import com.relogiclabs.jschema.type.EObject; -import com.relogiclabs.jschema.type.EString; -import com.relogiclabs.jschema.type.EValue; - -import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.dereference; -import static com.relogiclabs.jschema.message.OutlineFormatter.createOutline; - -public abstract class ScriptLibrary1 { - static final String Print_F1 = "print#1"; - static final String Fail_F1 = "fail#1"; - static final String Fail_F2 = "fail#2"; - static final String Fail_F4 = "fail#4"; - static final String Expected_F1 = "expected#1"; - static final String Expected_F2 = "expected#2"; - static final String Actual_F1 = "actual#1"; - static final String Actual_F2 = "actual#2"; - static final String Ticks_F0 = "ticks#0"; - - static final String Message_Id = "message"; - static final String Code_Id = "code"; - static final String Expected_Id = "expected"; - static final String Actual_Id = "actual"; - static final String Node_Id = "node"; - - - static long toInteger(EValue value, String parameter, String code) { - if(!(value instanceof EInteger i)) throw failOnInvalidArgumentType(code, value, parameter); - return i.getValue(); - } - - static double toNumber(EValue value, String parameter, String code) { - if(!(value instanceof ENumber n)) throw failOnInvalidArgumentType(code, value, parameter); - return n.toDouble(); - } - - static String toString(EValue value, String parameter, String code) { - if(!(value instanceof EString s)) throw failOnInvalidArgumentType(code, value, parameter); - return s.getValue(); - } - - static T cast(EValue value, Class type, String parameter, String code) { - if(!type.isInstance(value)) throw failOnInvalidArgumentType(code, value, parameter); - return type.cast(value); - } - - static T getMember(EObject object, String key, Class type, String parameter, String code) { - var value = dereference(object.get(key)); - if(!type.isInstance(value)) throw failOnInvalidArgumentValue(code, object, parameter); - return type.cast(value); - } - - static ScriptArgumentException failOnInvalidArgumentType(String code, EValue argument, - String parameter) { - return new ScriptArgumentException(code, "Invalid argument type " + argument.getType() - + " for parameter '" + parameter + "' of function '%s'"); - } - - static ScriptArgumentException failOnInvalidArgumentValue(String code, EValue argument, - String parameter) { - return new ScriptArgumentException(code, "Invalid argument value " + createOutline(argument) - + " for parameter '" + parameter + "' of function '%s'"); - } - - static void fail(ScriptScope scope, RuntimeException exception) { - scope.getRuntime().getExceptions().fail(exception); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary2.java b/src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary2.java deleted file mode 100644 index 79340e0..0000000 --- a/src/main/java/com/relogiclabs/jschema/internal/library/ScriptLibrary2.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.relogiclabs.jschema.internal.library; - -import com.relogiclabs.jschema.exception.JsonSchemaException; -import com.relogiclabs.jschema.exception.ScriptInitiatedException; -import com.relogiclabs.jschema.internal.engine.ScriptScope; -import com.relogiclabs.jschema.internal.engine.ScriptTreeHelper; -import com.relogiclabs.jschema.internal.script.GInteger; -import com.relogiclabs.jschema.internal.script.GObject; -import com.relogiclabs.jschema.message.ActualDetail; -import com.relogiclabs.jschema.message.ErrorDetail; -import com.relogiclabs.jschema.message.ExpectedDetail; -import com.relogiclabs.jschema.node.JNode; -import com.relogiclabs.jschema.type.EObject; -import com.relogiclabs.jschema.type.EString; -import com.relogiclabs.jschema.type.EValue; - -import java.util.List; - -import static com.relogiclabs.jschema.internal.script.GBoolean.FALSE; -import static com.relogiclabs.jschema.internal.tree.ScriptFunction.CALLER_HVAR; -import static com.relogiclabs.jschema.internal.tree.ScriptFunction.TARGET_HVAR; -import static com.relogiclabs.jschema.internal.util.CommonHelper.nonNullFrom; -import static com.relogiclabs.jschema.message.ErrorCode.ACTL01; -import static com.relogiclabs.jschema.message.ErrorCode.ACTL02; -import static com.relogiclabs.jschema.message.ErrorCode.ACTL03; -import static com.relogiclabs.jschema.message.ErrorCode.EXPC01; -import static com.relogiclabs.jschema.message.ErrorCode.EXPC02; -import static com.relogiclabs.jschema.message.ErrorCode.EXPC03; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL01; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL02; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL03; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL04; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL05; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL06; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL07; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL08; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL09; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL10; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL11; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL12; -import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; -import static com.relogiclabs.jschema.type.EValue.VOID; - -public abstract class ScriptLibrary2 extends ScriptLibrary1 { - static EValue printFunction(ScriptScope scope, List arguments) { - System.out.println(ScriptTreeHelper.stringify(arguments.get(0))); - return VOID; - } - - static EValue failFunction1(ScriptScope scope, List arguments) { - var caller = scope.resolve(CALLER_HVAR); - fail(scope, new ScriptInitiatedException(formatForSchema(FAIL01, - toString(arguments.get(0), Message_Id, FAIL02), (JNode) caller))); - return FALSE; - } - - static EValue failFunction2(ScriptScope scope, List arguments) { - var caller = scope.resolve(CALLER_HVAR); - fail(scope, new ScriptInitiatedException(formatForSchema( - toString(arguments.get(0), Code_Id, FAIL03), - toString(arguments.get(1), Message_Id, FAIL04), (JNode) caller))); - return FALSE; - } - - static EValue failFunction4(ScriptScope scope, List arguments) { - var expected = cast(arguments.get(2), EObject.class, Expected_Id, FAIL05); - var actual = cast(arguments.get(3), EObject.class, Actual_Id, FAIL06); - fail(scope, new JsonSchemaException(new ErrorDetail( - toString(arguments.get(0), Code_Id, FAIL07), - toString(arguments.get(1), Message_Id, FAIL08)), - new ExpectedDetail(getMember(expected, Node_Id, JNode.class, Expected_Id, FAIL09), - getMember(expected, Message_Id, EString.class, Expected_Id, FAIL10).getValue()), - new ActualDetail(getMember(actual, Node_Id, JNode.class, Actual_Id, FAIL11), - getMember(actual, Message_Id, EString.class, Actual_Id, FAIL12).getValue()))); - return FALSE; - } - - static EValue expectedFunction1(ScriptScope scope, List arguments) { - var result = new GObject(2); - result.put(Node_Id, nonNullFrom(scope.resolve(CALLER_HVAR), VOID)); - result.put(Message_Id, cast(arguments.get(0), EString.class, Message_Id, EXPC01)); - return result; - } - - static EValue expectedFunction2(ScriptScope scope, List arguments) { - var result = new GObject(2); - result.put(Node_Id, cast(arguments.get(0), JNode.class, Node_Id, EXPC02)); - result.put(Message_Id, cast(arguments.get(1), EString.class, Message_Id, EXPC03)); - return result; - } - - static EValue actualFunction1(ScriptScope scope, List arguments) { - var result = new GObject(2); - result.put(Node_Id, nonNullFrom(scope.resolve(TARGET_HVAR), VOID)); - result.put(Message_Id, cast(arguments.get(0), EString.class, Message_Id, ACTL01)); - return result; - } - - static EValue actualFunction2(ScriptScope scope, List arguments) { - var result = new GObject(2); - result.put(Node_Id, cast(arguments.get(0), JNode.class, Node_Id, ACTL02)); - result.put(Message_Id, cast(arguments.get(1), EString.class, Message_Id, ACTL03)); - return result; - } - - static EValue ticksFunction(ScriptScope scope, List arguments) { - return GInteger.from(System.currentTimeMillis()); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/StringLibrary.java b/src/main/java/com/relogiclabs/jschema/internal/library/StringLibrary.java deleted file mode 100644 index 24d20c0..0000000 --- a/src/main/java/com/relogiclabs/jschema/internal/library/StringLibrary.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.relogiclabs.jschema.internal.library; - -import com.relogiclabs.jschema.internal.engine.ScriptScope; -import com.relogiclabs.jschema.internal.script.GInteger; -import com.relogiclabs.jschema.internal.script.GString; -import com.relogiclabs.jschema.type.EInteger; -import com.relogiclabs.jschema.type.EString; -import com.relogiclabs.jschema.type.EType; -import com.relogiclabs.jschema.type.EValue; -import lombok.Getter; - -import java.util.List; - -import static com.relogiclabs.jschema.message.ErrorCode.SFND01; -import static com.relogiclabs.jschema.message.ErrorCode.SFND02; -import static com.relogiclabs.jschema.message.ErrorCode.SFND03; -import static com.relogiclabs.jschema.type.EUndefined.UNDEFINED; - -public final class StringLibrary extends CommonLibrary { - private static final String Length_M0 = "length#0"; - private static final String Find_Bn = "find"; - private static final String Find_M1 = "find#1"; - private static final String Find_M2 = "find#2"; - private static final String Copy_M0 = "copy#0"; - - private static final String Value_Id = "value"; - private static final String Start_Id = "start"; - - @Getter - private static final StringLibrary instance = new StringLibrary(); - - private StringLibrary() { - addMethod(Length_M0, StringLibrary::lengthMethod); - addMethod(Find_M1, StringLibrary::findMethod1); - addMethod(Find_M2, StringLibrary::findMethod2); - addMethod(Copy_M0, StringLibrary::copyMethod); - } - - @Override - protected EType getType() { - return EType.STRING; - } - - private static GInteger lengthMethod(EValue self, List arguments, ScriptScope scope) { - return GInteger.from(((EString) self).length()); - } - - private static EValue findMethod1(EValue self, List arguments, ScriptScope scope) { - if(!(arguments.get(0) instanceof EString a0)) throw failOnInvalidArgumentType(SFND01, - arguments.get(0), Find_Bn, Value_Id, self); - var index = ((EString) self).getValue().indexOf(a0.getValue()); - return index == -1 ? UNDEFINED : GInteger.from(index); - } - - private static EValue findMethod2(EValue self, List arguments, ScriptScope scope) { - if(!(arguments.get(0) instanceof EString a0)) throw failOnInvalidArgumentType(SFND02, - arguments.get(0), Find_Bn, Value_Id, self); - if(!(arguments.get(1) instanceof EInteger a1)) throw failOnInvalidArgumentType(SFND03, - arguments.get(1), Find_Bn, Start_Id, self); - var index = ((EString) self).getValue().indexOf(a0.getValue(), (int) a1.getValue()); - return index == -1 ? UNDEFINED : GInteger.from(index); - } - - private static EValue copyMethod(EValue self, List arguments, ScriptScope scope) { - return GString.from(((EString) self).getValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/StringMethods.java b/src/main/java/com/relogiclabs/jschema/internal/library/StringMethods.java new file mode 100644 index 0000000..fd95e6f --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/internal/library/StringMethods.java @@ -0,0 +1,77 @@ +package com.relogiclabs.jschema.internal.library; + +import com.relogiclabs.jschema.exception.InvalidArgumentException; +import com.relogiclabs.jschema.internal.engine.ScriptScope; +import com.relogiclabs.jschema.internal.script.GInteger; +import com.relogiclabs.jschema.internal.script.GString; +import com.relogiclabs.jschema.type.EString; +import com.relogiclabs.jschema.type.EValue; +import lombok.Getter; + +import java.util.List; + +import static com.relogiclabs.jschema.internal.library.LibraryHelper.asInteger; +import static com.relogiclabs.jschema.internal.library.LibraryHelper.asString; +import static com.relogiclabs.jschema.message.ErrorCode.FNDSTR01; +import static com.relogiclabs.jschema.message.ErrorCode.FNDSTR02; +import static com.relogiclabs.jschema.message.ErrorCode.FNDSTR03; +import static com.relogiclabs.jschema.type.EUndefined.UNDEFINED; + +public final class StringMethods extends CommonMethods { + private static final String Length_M0 = "length#0"; + private static final String Find_M1 = "find#1"; + private static final String Find_M2 = "find#2"; + private static final String Copy_M0 = "copy#0"; + + private static final String Value_Id = "value"; + private static final String Start_Id = "start"; + + @Getter + private static final StringMethods instance = new StringMethods(); + + private StringMethods() { + addMethod(Length_M0, StringMethods::lengthMethod); + addMethod(Find_M1, StringMethods::findMethod1); + addMethod(Find_M2, StringMethods::findMethod2); + addMethod(Copy_M0, StringMethods::copyMethod); + } + + private static GInteger lengthMethod(EValue self, List arguments, ScriptScope scope) { + return GInteger.from(((EString) self).length()); + } + + private static EValue findMethod1(EValue self, List arguments, ScriptScope scope) { + String value; + try { + value = asString(arguments.get(0)); + } catch(InvalidArgumentException e) { + e.setContext(FNDSTR01, Value_Id); + throw e.failWithMethodException(self); + } + var index = ((EString) self).getValue().indexOf(value); + return index == -1 ? UNDEFINED : GInteger.from(index); + } + + private static EValue findMethod2(EValue self, List arguments, ScriptScope scope) { + String value; + try { + value = asString(arguments.get(0)); + } catch(InvalidArgumentException e) { + e.setContext(FNDSTR02, Value_Id); + throw e.failWithMethodException(self); + } + int start; + try { + start = asInteger(arguments.get(1)); + } catch(InvalidArgumentException e) { + e.setContext(FNDSTR03, Start_Id); + throw e.failWithMethodException(self); + } + var index = ((EString) self).getValue().indexOf(value, start); + return index == -1 ? UNDEFINED : GInteger.from(index); + } + + private static EValue copyMethod(EValue self, List arguments, ScriptScope scope) { + return GString.from(((EString) self).getValue()); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/script/GArray.java b/src/main/java/com/relogiclabs/jschema/internal/script/GArray.java index 7e19086..e4b8eea 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/script/GArray.java +++ b/src/main/java/com/relogiclabs/jschema/internal/script/GArray.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.internal.script; -import com.relogiclabs.jschema.exception.ScriptCommonException; +import com.relogiclabs.jschema.exception.ScriptArrayInsertException; import com.relogiclabs.jschema.exception.UpdateNotSupportedException; import com.relogiclabs.jschema.type.EArray; import com.relogiclabs.jschema.type.EValue; @@ -12,9 +12,9 @@ import java.util.List; import static com.relogiclabs.jschema.internal.util.StringHelper.joinWith; -import static com.relogiclabs.jschema.message.ErrorCode.AUPD03; -import static com.relogiclabs.jschema.message.ErrorCode.AWRT01; -import static com.relogiclabs.jschema.message.ErrorCode.AWRT02; +import static com.relogiclabs.jschema.message.ErrorCode.ARNSRT01; +import static com.relogiclabs.jschema.message.ErrorCode.ARNSRT02; +import static com.relogiclabs.jschema.message.ErrorCode.ROARRY03; @Getter @EqualsAndHashCode @@ -51,16 +51,16 @@ public EValue get(int index) { @Override public void set(int index, EValue value) { var size = elements.size(); - if(index > MAX_LIMIT) throw new ScriptCommonException(AWRT02, + if(index > MAX_LIMIT) throw new ScriptArrayInsertException(ARNSRT01, "Array index " + index + " exceeds maximum size limit"); if(index == size) elements.add(new GLeftValue(value)); else if(index < size) { if(!(elements.get(index) instanceof GLeftValue l)) - throw new UpdateNotSupportedException(AUPD03, + throw new UpdateNotSupportedException(ROARRY03, "Readonly array index " + index + " cannot be updated"); l.setValue(value); - } else throw new ScriptCommonException(AWRT01, - "Index " + index + " is out of bounds for writing to array length " + size); + } else throw new ScriptArrayInsertException(ARNSRT02, + "Index " + index + " is out of bounds for inserting to array length " + size); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/internal/script/GFunction.java b/src/main/java/com/relogiclabs/jschema/internal/script/GFunction.java index b178719..7798810 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/script/GFunction.java +++ b/src/main/java/com/relogiclabs/jschema/internal/script/GFunction.java @@ -12,7 +12,7 @@ import static com.relogiclabs.jschema.internal.script.RFunction.hasVariadic; import static com.relogiclabs.jschema.internal.util.CollectionHelper.subList; import static com.relogiclabs.jschema.internal.util.CommonHelper.hasFlag; -import static com.relogiclabs.jschema.message.ErrorCode.FNVK02; +import static com.relogiclabs.jschema.message.ErrorCode.FNSNVK02; @Getter @RequiredArgsConstructor @@ -35,7 +35,7 @@ public GFunction(GParameter[] parameters, Evaluator body, int mode) { @Override public ScriptScope bind(ScriptScope parentScope, List arguments) { - areCompatible(parameters, arguments, FNVK02); + areCompatible(parameters, arguments, FNSNVK02); var scope = new ScriptScope(parentScope); var i = 0; for(var p : parameters) scope.addVariable(p.getName(), p.isVariadic() diff --git a/src/main/java/com/relogiclabs/jschema/internal/script/GIterator.java b/src/main/java/com/relogiclabs/jschema/internal/script/GIterator.java index 4155544..7b96ea7 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/script/GIterator.java +++ b/src/main/java/com/relogiclabs/jschema/internal/script/GIterator.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.internal.script; -import com.relogiclabs.jschema.exception.ScriptCommonException; +import com.relogiclabs.jschema.exception.ScriptIteratorException; import com.relogiclabs.jschema.type.EArray; import com.relogiclabs.jschema.type.EObject; import com.relogiclabs.jschema.type.EString; @@ -10,7 +10,7 @@ import java.util.Iterator; -import static com.relogiclabs.jschema.message.ErrorCode.ITER01; +import static com.relogiclabs.jschema.message.ErrorCode.ITERSE01; @Getter @RequiredArgsConstructor @@ -22,7 +22,7 @@ public Iterator iterator() { if(iterable instanceof EArray array) return iterator(array); if(iterable instanceof EObject object) return iterator(object); if(iterable instanceof EString string) return iterator(string); - throw new ScriptCommonException(ITER01, "Invalid type " + throw new ScriptIteratorException(ITERSE01, "Invalid type " + iterable.getType() + " for iteration"); } diff --git a/src/main/java/com/relogiclabs/jschema/internal/script/GObject.java b/src/main/java/com/relogiclabs/jschema/internal/script/GObject.java index 269f0c6..0b02c91 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/script/GObject.java +++ b/src/main/java/com/relogiclabs/jschema/internal/script/GObject.java @@ -13,7 +13,7 @@ import static com.relogiclabs.jschema.internal.util.StringHelper.joinWith; import static com.relogiclabs.jschema.internal.util.StringHelper.quote; -import static com.relogiclabs.jschema.message.ErrorCode.OUPD02; +import static com.relogiclabs.jschema.message.ErrorCode.ROOBJT02; @Getter @EqualsAndHashCode @@ -47,7 +47,7 @@ public void set(String key, EValue value) { properties.put(key, new GLeftValue(value)); return; } - if(!(oldValue instanceof GLeftValue l)) throw new UpdateNotSupportedException(OUPD02, + if(!(oldValue instanceof GLeftValue l)) throw new UpdateNotSupportedException(ROOBJT02, "Readonly object property '" + key + "' cannot be updated"); l.setValue(value); } From 99292fe51f67b47252394ff03ce8422ef3177d40 Mon Sep 17 00:00:00 2001 From: Zahid Hossain <60911932+zhossain-info@users.noreply.github.com> Date: Mon, 29 Jul 2024 22:11:29 +0600 Subject: [PATCH 3/7] Add new functions and pragmas --- .../jschema/function/CoreFunctions5.java | 62 +++++++++++++++++++ .../internal/builder/JPragmaBuilder.java | 8 +-- .../internal/function/CoreLibrary.java | 4 +- .../jschema/internal/function/IPAddress.java | 46 ++++++++++++++ .../internal/tree/PragmaDescriptor.java | 25 ++++++-- .../jschema/message/OutlineFormatter.java | 17 +++-- .../jschema/tree/PragmaRegistry.java | 55 +++++++++------- 7 files changed, 176 insertions(+), 41 deletions(-) create mode 100644 src/main/java/com/relogiclabs/jschema/function/CoreFunctions5.java create mode 100644 src/main/java/com/relogiclabs/jschema/internal/function/IPAddress.java diff --git a/src/main/java/com/relogiclabs/jschema/function/CoreFunctions5.java b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions5.java new file mode 100644 index 0000000..eb31274 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions5.java @@ -0,0 +1,62 @@ +package com.relogiclabs.jschema.function; + +import com.relogiclabs.jschema.exception.FunctionArgumentValueException; +import com.relogiclabs.jschema.exception.FunctionValidationException; +import com.relogiclabs.jschema.internal.function.IPAddress; +import com.relogiclabs.jschema.message.ActualDetail; +import com.relogiclabs.jschema.message.ErrorDetail; +import com.relogiclabs.jschema.message.ExpectedDetail; +import com.relogiclabs.jschema.node.JInteger; +import com.relogiclabs.jschema.node.JString; + +import java.util.Arrays; +import java.util.stream.Collectors; + +import static com.relogiclabs.jschema.internal.function.IPAddress.IPV4_VERSION; +import static com.relogiclabs.jschema.internal.function.IPAddress.IPV6_VERSION; +import static com.relogiclabs.jschema.message.ErrorCode.IPV4CF01; +import static com.relogiclabs.jschema.message.ErrorCode.IPV6CF01; +import static com.relogiclabs.jschema.message.ErrorCode.IPVACF01; +import static com.relogiclabs.jschema.message.ErrorCode.IPVACF02; +import static com.relogiclabs.jschema.message.ErrorCode.IPVACF03; +import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; + +public final class CoreFunctions5 extends CoreFunctions4 { + public boolean ipv4(JString target) { + // Based on RFC 790 and RFC 1918 + if(IPAddress.isValidIPv4(target.getValue())) return true; + return fail(new FunctionValidationException( + new ErrorDetail(IPV4CF01, "Invalid target IPv4 address"), + new ExpectedDetail(caller, "a valid IPv4 address"), + new ActualDetail(target, "found malformed target " + target))); + } + + public boolean ipv6(JString target) { + // Based on RFC 2373 (including widely used format) + // Also support IPv4 mapped IPv6 address + if(IPAddress.isValidIPv6(target.getValue())) return true; + return fail(new FunctionValidationException( + new ErrorDetail(IPV6CF01, "Invalid target IPv6 address"), + new ExpectedDetail(caller, "a valid IPv6 address"), + new ActualDetail(target, "found malformed target " + target))); + } + + public boolean ipv(JString target, JInteger... versions) { + if(versions.length == 0) return fail(new FunctionArgumentValueException( + formatForSchema(IPVACF01, "Version number(s) are missing", caller))); + for(var version : versions) { + if(version.getValue() == IPV4_VERSION) { + if(IPAddress.isValidIPv4(target.getValue())) return true; + } else if(version.getValue() == IPV6_VERSION) { + if(IPAddress.isValidIPv6(target.getValue())) return true; + } else return fail(new FunctionArgumentValueException(formatForSchema(IPVACF02, + "Invalid version number " + version, caller))); + } + var list = Arrays.stream(versions).map(JInteger::toString) + .collect(Collectors.joining(" or ")); + return fail(new FunctionValidationException( + new ErrorDetail(IPVACF03, "Invalid target IP address"), + new ExpectedDetail(caller, "a valid IPv(" + list + ") address"), + new ActualDetail(target, "found malformed target " + target))); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/builder/JPragmaBuilder.java b/src/main/java/com/relogiclabs/jschema/internal/builder/JPragmaBuilder.java index 91f6631..f2fb3c3 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/builder/JPragmaBuilder.java +++ b/src/main/java/com/relogiclabs/jschema/internal/builder/JPragmaBuilder.java @@ -9,8 +9,8 @@ import lombok.Setter; import lombok.experimental.Accessors; -import static com.relogiclabs.jschema.message.ErrorCode.PRAG01; -import static com.relogiclabs.jschema.message.ErrorCode.PRAG02; +import static com.relogiclabs.jschema.message.ErrorCode.PRGCHK01; +import static com.relogiclabs.jschema.message.ErrorCode.PRGCHK02; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; @Getter @Setter @@ -22,10 +22,10 @@ public final class JPragmaBuilder extends JNodeBuilder { private void checkPragma() { var descriptor = PragmaDescriptor.from(name); if (descriptor == null) - throw new PragmaNotFoundException(formatForSchema(PRAG01, "Invalid pragma '" + throw new PragmaNotFoundException(formatForSchema(PRGCHK01, "Invalid pragma '" + name + "' with value " + value.getOutline() + " found", context())); if (!descriptor.matchType(value.getClass())) - throw new InvalidPragmaValueException(formatForSchema(PRAG02, "Invalid value " + throw new InvalidPragmaValueException(formatForSchema(PRGCHK02, "Invalid value " + value.getOutline() + " for pragma '" + name + "' found", value)); } diff --git a/src/main/java/com/relogiclabs/jschema/internal/function/CoreLibrary.java b/src/main/java/com/relogiclabs/jschema/internal/function/CoreLibrary.java index d2e997b..f178c01 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/function/CoreLibrary.java +++ b/src/main/java/com/relogiclabs/jschema/internal/function/CoreLibrary.java @@ -1,12 +1,12 @@ package com.relogiclabs.jschema.internal.function; -import com.relogiclabs.jschema.function.CoreFunctions4; +import com.relogiclabs.jschema.function.CoreFunctions5; import com.relogiclabs.jschema.internal.tree.EFunction; import java.util.List; public class CoreLibrary { - private static final FunctionMap functions = FunctionLoader.load(CoreFunctions4.class, null); + private static final FunctionMap functions = FunctionLoader.load(CoreFunctions5.class, null); public static List getFunctions(String key) { return functions.get(key); diff --git a/src/main/java/com/relogiclabs/jschema/internal/function/IPAddress.java b/src/main/java/com/relogiclabs/jschema/internal/function/IPAddress.java new file mode 100644 index 0000000..6401c89 --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/internal/function/IPAddress.java @@ -0,0 +1,46 @@ +package com.relogiclabs.jschema.internal.function; + +import java.util.regex.Pattern; + +public final class IPAddress { + public static final int IPV4_VERSION = 4; + public static final int IPV6_VERSION = 6; + + private static final String IPV4_REGEX = + "((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\\.){3}" + + "(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])"; + + private static final String IPV6_REGEX = + "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|" + + "([0-9a-fA-F]{1,4}:){1,7}:|" + + "([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|" + + "([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|" + + "([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|" + + "([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|" + + "([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|" + + "[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|" + + ":((:[0-9a-fA-F]{1,4}){1,7}|:)|" + + "fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|" + + "::(ffff(:0{1,4}){0,1}:){0,1}" + + "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}" + + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|" + + "([0-9a-fA-F]{1,4}:){1,4}:" + + "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}" + + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"; + + + private static final Pattern IPV4_PATTERN = Pattern.compile(IPV4_REGEX); + private static final Pattern IPV6_PATTERN = Pattern.compile(IPV6_REGEX); + + private IPAddress() { + throw new UnsupportedOperationException("This class is not intended for instantiation"); + } + + public static boolean isValidIPv4(String ipAddress) { + return IPV4_PATTERN.matcher(ipAddress).matches(); + } + + public static boolean isValidIPv6(String ipAddress) { + return IPV6_PATTERN.matcher(ipAddress).matches(); + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/tree/PragmaDescriptor.java b/src/main/java/com/relogiclabs/jschema/internal/tree/PragmaDescriptor.java index 2f2709e..7134a18 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/tree/PragmaDescriptor.java +++ b/src/main/java/com/relogiclabs/jschema/internal/tree/PragmaDescriptor.java @@ -1,6 +1,7 @@ package com.relogiclabs.jschema.internal.tree; import com.relogiclabs.jschema.node.JBoolean; +import com.relogiclabs.jschema.node.JInteger; import com.relogiclabs.jschema.node.JNumber; import com.relogiclabs.jschema.node.JString; import lombok.Getter; @@ -8,6 +9,15 @@ import java.util.HashMap; import java.util.Map; +import static com.relogiclabs.jschema.message.OutlineFormatter.DEFAULT_MAXIMUM_LENGTH; +import static com.relogiclabs.jschema.tree.PragmaRegistry.NAME_DATE_DATA_TYPE_FORMAT; +import static com.relogiclabs.jschema.tree.PragmaRegistry.NAME_ENABLE_CONTEXTUAL_EXCEPTION; +import static com.relogiclabs.jschema.tree.PragmaRegistry.NAME_FLOATING_POINT_TOLERANCE; +import static com.relogiclabs.jschema.tree.PragmaRegistry.NAME_IGNORE_OBJECT_PROPERTY_ORDER; +import static com.relogiclabs.jschema.tree.PragmaRegistry.NAME_IGNORE_UNDEFINED_PROPERTIES; +import static com.relogiclabs.jschema.tree.PragmaRegistry.NAME_OUTLINE_MAXIMUM_LENGTH; +import static com.relogiclabs.jschema.tree.PragmaRegistry.NAME_TIME_DATA_TYPE_FORMAT; + @Getter public final class PragmaDescriptor { public static final String ISO_8601_DATE = "YYYY-MM-DD"; @@ -16,15 +26,20 @@ public final class PragmaDescriptor { private static final Map> PRAGMAS = new HashMap<>(); public static final PragmaDescriptor IGNORE_UNDEFINED_PROPERTIES - = new PragmaDescriptor<>("IgnoreUndefinedProperties", JBoolean.class, false); + = new PragmaDescriptor<>(NAME_IGNORE_UNDEFINED_PROPERTIES, JBoolean.class, false); public static final PragmaDescriptor FLOATING_POINT_TOLERANCE - = new PragmaDescriptor<>("FloatingPointTolerance", JNumber.class, 1E-10); + = new PragmaDescriptor<>(NAME_FLOATING_POINT_TOLERANCE, JNumber.class, 1E-10); public static final PragmaDescriptor IGNORE_OBJECT_PROPERTY_ORDER - = new PragmaDescriptor<>("IgnoreObjectPropertyOrder", JBoolean.class, true); + = new PragmaDescriptor<>(NAME_IGNORE_OBJECT_PROPERTY_ORDER, JBoolean.class, true); public static final PragmaDescriptor DATE_DATA_TYPE_FORMAT - = new PragmaDescriptor<>("DateDataTypeFormat", JString.class, ISO_8601_DATE); + = new PragmaDescriptor<>(NAME_DATE_DATA_TYPE_FORMAT, JString.class, ISO_8601_DATE); public static final PragmaDescriptor TIME_DATA_TYPE_FORMAT - = new PragmaDescriptor<>("TimeDataTypeFormat", JString.class, ISO_8601_TIME); + = new PragmaDescriptor<>(NAME_TIME_DATA_TYPE_FORMAT, JString.class, ISO_8601_TIME); + public static final PragmaDescriptor ENABLE_CONTEXTUAL_EXCEPTION + = new PragmaDescriptor<>(NAME_ENABLE_CONTEXTUAL_EXCEPTION, JBoolean.class, false); + public static final PragmaDescriptor OUTLINE_MAXIMUM_LENGTH + = new PragmaDescriptor<>(NAME_OUTLINE_MAXIMUM_LENGTH, JInteger.class, + DEFAULT_MAXIMUM_LENGTH); private final String name; private final Class type; diff --git a/src/main/java/com/relogiclabs/jschema/message/OutlineFormatter.java b/src/main/java/com/relogiclabs/jschema/message/OutlineFormatter.java index 9da56c8..bd289cd 100644 --- a/src/main/java/com/relogiclabs/jschema/message/OutlineFormatter.java +++ b/src/main/java/com/relogiclabs/jschema/message/OutlineFormatter.java @@ -1,27 +1,32 @@ package com.relogiclabs.jschema.message; +import lombok.Getter; + import static org.apache.commons.lang3.StringUtils.left; import static org.apache.commons.lang3.StringUtils.right; public final class OutlineFormatter { + public static final int DEFAULT_MAXIMUM_LENGTH = 200; private static final String ABBREVIATE_MARKER = "..."; - private static int outlineLength = 200; - private static int startLength = 2 * outlineLength / 3; - private static int endLength = outlineLength / 3; + + @Getter + private static int maximumLength = DEFAULT_MAXIMUM_LENGTH; + private static int startLength = 2 * maximumLength / 3; + private static int endLength = maximumLength / 3; private OutlineFormatter() { throw new UnsupportedOperationException("This class is not intended for instantiation"); } - public static void setOutlineLength(int length) { - outlineLength = length; + public static void setMaximumLength(int length) { + maximumLength = length; startLength = 2 * length / 3; endLength = length / 3; } public static String createOutline(Object object) { var string = object.toString(); - return outlineLength >= string.length() ? string + return string.length() <= maximumLength ? string : left(string, startLength) + ABBREVIATE_MARKER + right(string, endLength); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/tree/PragmaRegistry.java b/src/main/java/com/relogiclabs/jschema/tree/PragmaRegistry.java index 71248d6..7ed0690 100644 --- a/src/main/java/com/relogiclabs/jschema/tree/PragmaRegistry.java +++ b/src/main/java/com/relogiclabs/jschema/tree/PragmaRegistry.java @@ -3,6 +3,7 @@ import com.relogiclabs.jschema.exception.DuplicatePragmaException; import com.relogiclabs.jschema.internal.time.DateTimeParser; import com.relogiclabs.jschema.internal.tree.PragmaDescriptor; +import com.relogiclabs.jschema.message.OutlineFormatter; import com.relogiclabs.jschema.node.JPragma; import lombok.Getter; @@ -10,32 +11,35 @@ import java.util.Iterator; import java.util.Map; -import static com.relogiclabs.jschema.message.ErrorCode.PRAG03; +import static com.relogiclabs.jschema.internal.tree.PragmaDescriptor.DATE_DATA_TYPE_FORMAT; +import static com.relogiclabs.jschema.internal.tree.PragmaDescriptor.ENABLE_CONTEXTUAL_EXCEPTION; +import static com.relogiclabs.jschema.internal.tree.PragmaDescriptor.FLOATING_POINT_TOLERANCE; +import static com.relogiclabs.jschema.internal.tree.PragmaDescriptor.IGNORE_OBJECT_PROPERTY_ORDER; +import static com.relogiclabs.jschema.internal.tree.PragmaDescriptor.IGNORE_UNDEFINED_PROPERTIES; +import static com.relogiclabs.jschema.internal.tree.PragmaDescriptor.TIME_DATA_TYPE_FORMAT; +import static com.relogiclabs.jschema.message.ErrorCode.PRGDUP01; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; import static com.relogiclabs.jschema.time.DateTimeType.DATE_TYPE; import static com.relogiclabs.jschema.time.DateTimeType.TIME_TYPE; @Getter public final class PragmaRegistry implements Iterable> { - - private static final String IGNORE_UNDEFINED_PROPERTIES = "IgnoreUndefinedProperties"; - private static final String FLOATING_POINT_TOLERANCE = "FloatingPointTolerance"; - private static final String IGNORE_OBJECT_PROPERTY_ORDER = "IgnoreObjectPropertyOrder"; - private static final String DATE_DATA_TYPE_FORMAT = "DateDataTypeFormat"; - private static final String TIME_DATA_TYPE_FORMAT = "TimeDataTypeFormat"; + public static final String NAME_IGNORE_UNDEFINED_PROPERTIES = "IgnoreUndefinedProperties"; + public static final String NAME_FLOATING_POINT_TOLERANCE = "FloatingPointTolerance"; + public static final String NAME_IGNORE_OBJECT_PROPERTY_ORDER = "IgnoreObjectPropertyOrder"; + public static final String NAME_DATE_DATA_TYPE_FORMAT = "DateDataTypeFormat"; + public static final String NAME_TIME_DATA_TYPE_FORMAT = "TimeDataTypeFormat"; + public static final String NAME_ENABLE_CONTEXTUAL_EXCEPTION = "EnableContextualException"; + public static final String NAME_OUTLINE_MAXIMUM_LENGTH = "OutlineMaximumLength"; private final Map pragmas; - private boolean ignoreUndefinedProperties = PragmaDescriptor - .IGNORE_UNDEFINED_PROPERTIES.getDefaultValue(); - private double floatingPointTolerance = PragmaDescriptor - .FLOATING_POINT_TOLERANCE.getDefaultValue(); - private boolean ignoreObjectPropertyOrder = PragmaDescriptor - .IGNORE_OBJECT_PROPERTY_ORDER.getDefaultValue(); - private String dateDataTypeFormat = PragmaDescriptor - .DATE_DATA_TYPE_FORMAT.getDefaultValue(); - private String timeDataTypeFormat = PragmaDescriptor - .TIME_DATA_TYPE_FORMAT.getDefaultValue(); + private boolean ignoreUndefinedProperties = IGNORE_UNDEFINED_PROPERTIES.getDefaultValue(); + private double floatingPointTolerance = FLOATING_POINT_TOLERANCE.getDefaultValue(); + private boolean ignoreObjectPropertyOrder = IGNORE_OBJECT_PROPERTY_ORDER.getDefaultValue(); + private String dateDataTypeFormat = DATE_DATA_TYPE_FORMAT.getDefaultValue(); + private String timeDataTypeFormat = TIME_DATA_TYPE_FORMAT.getDefaultValue(); + private boolean enableContextualException = ENABLE_CONTEXTUAL_EXCEPTION.getDefaultValue(); private DateTimeParser dateTypeParser; private DateTimeParser timeTypeParser; @@ -47,8 +51,9 @@ public PragmaRegistry() { } public JPragma addPragma(JPragma pragma) { - if(pragmas.containsKey(pragma.getName())) throw new DuplicatePragmaException(formatForSchema( - PRAG03, "Duplication found for " + pragma.getOutline(), pragma)); + if(pragmas.containsKey(pragma.getName())) + throw new DuplicatePragmaException(formatForSchema(PRGDUP01, + "Duplication found for " + pragma.getOutline(), pragma)); pragmas.put(pragma.getName(), pragma); setPragmaValue(pragma.getName(), pragma.getValue().toNativeValue()); return pragma; @@ -56,17 +61,19 @@ public JPragma addPragma(JPragma pragma) { public void setPragmaValue(String name, T value) { switch (name) { - case IGNORE_UNDEFINED_PROPERTIES -> ignoreUndefinedProperties = (boolean) value; - case FLOATING_POINT_TOLERANCE -> floatingPointTolerance = (double) value; - case IGNORE_OBJECT_PROPERTY_ORDER -> ignoreObjectPropertyOrder = (boolean) value; - case DATE_DATA_TYPE_FORMAT -> { + case NAME_IGNORE_UNDEFINED_PROPERTIES -> ignoreUndefinedProperties = (boolean) value; + case NAME_FLOATING_POINT_TOLERANCE -> floatingPointTolerance = (double) value; + case NAME_IGNORE_OBJECT_PROPERTY_ORDER -> ignoreObjectPropertyOrder = (boolean) value; + case NAME_DATE_DATA_TYPE_FORMAT -> { dateDataTypeFormat = (String) value; dateTypeParser = new DateTimeParser(dateDataTypeFormat, DATE_TYPE); } - case TIME_DATA_TYPE_FORMAT -> { + case NAME_TIME_DATA_TYPE_FORMAT -> { timeDataTypeFormat = (String) value; timeTypeParser = new DateTimeParser(timeDataTypeFormat, TIME_TYPE); } + case NAME_ENABLE_CONTEXTUAL_EXCEPTION -> enableContextualException = (boolean) value; + case NAME_OUTLINE_MAXIMUM_LENGTH -> OutlineFormatter.setMaximumLength((int) (long) value); } } From 1eeb42855c141c032adcab17e21499f350f37d83 Mon Sep 17 00:00:00 2001 From: Zahid Hossain <60911932+zhossain-info@users.noreply.github.com> Date: Tue, 30 Jul 2024 13:18:06 +0600 Subject: [PATCH 4/7] Update exception handling for schema --- .../internal/message/ActualHelper.java | 27 ++++---- .../internal/message/ExpectedHelper.java | 29 ++++---- .../internal/message/MessageHelper.java | 17 +++-- .../internal/util/LexerErrorListener.java | 56 ++++++++++----- .../jschema/internal/util/LogHelper.java | 16 +++-- .../jschema/internal/util/MatchResult.java | 21 ++++++ .../internal/util/ParserErrorListener.java | 38 ++++++---- .../com/relogiclabs/jschema/node/JAlias.java | 8 +-- .../com/relogiclabs/jschema/node/JArray.java | 24 +++---- .../relogiclabs/jschema/node/JBoolean.java | 14 ++-- .../relogiclabs/jschema/node/JDataType.java | 50 +++++++------- .../com/relogiclabs/jschema/node/JDouble.java | 14 ++-- .../com/relogiclabs/jschema/node/JFloat.java | 14 ++-- .../relogiclabs/jschema/node/JFunction.java | 15 ++-- .../relogiclabs/jschema/node/JInteger.java | 14 ++-- .../com/relogiclabs/jschema/node/JNode.java | 14 ++-- .../com/relogiclabs/jschema/node/JObject.java | 58 +++++++++------- .../relogiclabs/jschema/node/JProperty.java | 26 +++---- .../relogiclabs/jschema/node/JReceiver.java | 16 ++--- .../com/relogiclabs/jschema/node/JString.java | 14 ++-- .../relogiclabs/jschema/node/JValidator.java | 69 ++++++++++--------- .../relogiclabs/jschema/node/JsonType.java | 57 +++++++-------- .../com/relogiclabs/jschema/type/EArray.java | 10 ++- .../com/relogiclabs/jschema/type/ENumber.java | 10 ++- .../com/relogiclabs/jschema/type/EObject.java | 10 ++- .../com/relogiclabs/jschema/type/EString.java | 10 ++- .../com/relogiclabs/jschema/type/EValue.java | 10 ++- .../relogiclabs/jschema/util/Reference.java | 16 ----- 28 files changed, 379 insertions(+), 298 deletions(-) create mode 100644 src/main/java/com/relogiclabs/jschema/internal/util/MatchResult.java delete mode 100644 src/main/java/com/relogiclabs/jschema/util/Reference.java diff --git a/src/main/java/com/relogiclabs/jschema/internal/message/ActualHelper.java b/src/main/java/com/relogiclabs/jschema/internal/message/ActualHelper.java index e140762..f384c15 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/message/ActualHelper.java +++ b/src/main/java/com/relogiclabs/jschema/internal/message/ActualHelper.java @@ -3,6 +3,7 @@ import com.relogiclabs.jschema.message.ActualDetail; import com.relogiclabs.jschema.node.JArray; import com.relogiclabs.jschema.node.JNode; +import com.relogiclabs.jschema.node.JObject; import com.relogiclabs.jschema.node.JProperty; import static com.relogiclabs.jschema.internal.message.MessageHelper.getTypeName; @@ -14,15 +15,15 @@ private ActualHelper() { throw new UnsupportedOperationException(); } - public static ActualDetail asArrayElementNotFound(JArray array, int index) { + public static ActualDetail asArrayElementNotFound(JArray array) { return new ActualDetail(array, "not found"); } - public static ActualDetail asValueMismatch(JNode node) { + public static ActualDetail asValueMismatched(JNode node) { return new ActualDetail(node, "found " + node.getOutline()); } - public static ActualDetail asGeneralValueMismatch(JNode node) { + public static ActualDetail asGeneralValidationFailed(JNode node) { return new ActualDetail(node, "found " + node.getOutline()); } @@ -35,27 +36,29 @@ public static ActualDetail asDataTypeArgumentFailed(JNode node) { return new ActualDetail(node, "found invalid value " + node.getOutline()); } - public static ActualDetail asDataTypeMismatch(JNode node) { + public static ActualDetail asDataTypeMismatched(JNode node) { return new ActualDetail(node, "found " + getTypeName(node) + " inferred by " + node.getOutline()); } - public static ActualDetail asPropertyNotFound(JNode node, JProperty property) { - return new ActualDetail(node, "not found property key " + quote(property.getKey())); + public static ActualDetail asPropertyNotFound(JNode node, JProperty property, JObject object) { + return new ActualDetail(node, "not found property key " + quote(property.getKey()) + + " in target object " + object.getOutline()); } - public static ActualDetail asUndefinedProperty(JProperty property) { + public static ActualDetail asUndefinedPropertyFound(JProperty property) { return new ActualDetail(property, "property found {" + property.getOutline() + "}"); } - public static ActualDetail asPropertyOrderMismatch(JNode node) { + public static ActualDetail asPropertyOrderMismatched(JNode node) { return node instanceof JProperty property ? new ActualDetail(property, "key " + quote(property.getKey()) - + " is found at current position") - : new ActualDetail(node, "key not found at current position"); + + " in target is found at current position") + : new ActualDetail(node, "no key in target found at current position"); } - public static ActualDetail asInvalidFunction(JNode node) { - return new ActualDetail(node, "applied on non-composite type " + getTypeName(node)); + public static ActualDetail asNestedFunctionFailed(JNode node) { + return new ActualDetail(node, "found non-composite target " + getTypeName(node) + + " of " + node.getOutline()); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/message/ExpectedHelper.java b/src/main/java/com/relogiclabs/jschema/internal/message/ExpectedHelper.java index f75e619..06782dd 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/message/ExpectedHelper.java +++ b/src/main/java/com/relogiclabs/jschema/internal/message/ExpectedHelper.java @@ -1,5 +1,6 @@ package com.relogiclabs.jschema.internal.message; +import com.relogiclabs.jschema.internal.util.MatchResult; import com.relogiclabs.jschema.message.ExpectedDetail; import com.relogiclabs.jschema.node.JDataType; import com.relogiclabs.jschema.node.JFunction; @@ -17,19 +18,23 @@ private ExpectedHelper() { } public static ExpectedDetail asArrayElementNotFound(JNode node, int index) { - return new ExpectedDetail(node, "'" + node.getOutline() + "' element at " + index); + return new ExpectedDetail(node, "an element for [" + node.getOutline() + + "] at " + index); } - public static ExpectedDetail asValueMismatch(JNode node) { + public static ExpectedDetail asValueMismatched(JNode node) { return new ExpectedDetail(node, "value " + node.getOutline()); } - public static ExpectedDetail asGeneralValueMismatch(JNode node) { + public static ExpectedDetail asGeneralValidationFailed(JNode node) { return new ExpectedDetail(node, "a valid value of " + node.getOutline()); } - public static ExpectedDetail asDataTypeMismatch(JDataType dataType) { - return new ExpectedDetail(dataType, "data type " + dataType.toString(true)); + public static ExpectedDetail asDataTypeMismatched(JDataType dataType, MatchResult result) { + var builder = new StringBuilder("data type ").append(dataType.toString(true)); + if(result.getPattern() == null) return new ExpectedDetail(dataType, builder.toString()); + builder.append(" formatted as ").append(quote(result.getPattern())); + return new ExpectedDetail(dataType, builder.toString()); } public static ExpectedDetail asInvalidNonCompositeType(JDataType dataType) { @@ -40,24 +45,24 @@ public static ExpectedDetail asDataTypeArgumentFailed(JDataType dataType) { return new ExpectedDetail(dataType, "a valid value for " + quote(dataType.getAlias())); } - public static ExpectedDetail asDataTypeMismatch(JNode node) { + public static ExpectedDetail asDataTypeMismatched(JNode node) { return new ExpectedDetail(node, getTypeName(node) + " inferred by " + node.getOutline()); } public static ExpectedDetail asPropertyNotFound(JProperty property) { - return new ExpectedDetail(property, "property {" + property.getOutline() + "}"); + return new ExpectedDetail(property, "a property for {" + property.getOutline() + "}"); } - public static ExpectedDetail asUndefinedProperty(JObject object, JProperty property) { + public static ExpectedDetail asUndefinedPropertyFound(JObject object, JProperty property) { return new ExpectedDetail(object, "no property with key " + quote(property.getKey())); } - public static ExpectedDetail asPropertyOrderMismatch(JProperty property) { - return new ExpectedDetail(property, "property with key " + public static ExpectedDetail asPropertyOrderMismatched(JProperty property) { + return new ExpectedDetail(property, "a property with key " + quote(property.getKey()) + " at current position"); } - public static ExpectedDetail asInvalidFunction(JFunction function) { - return new ExpectedDetail(function, "applying on composite type"); + public static ExpectedDetail asNestedFunctionFailed(JFunction function) { + return new ExpectedDetail(function, "a composite data type for " + function); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/message/MessageHelper.java b/src/main/java/com/relogiclabs/jschema/internal/message/MessageHelper.java index 7853774..4d7bf10 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/message/MessageHelper.java +++ b/src/main/java/com/relogiclabs/jschema/internal/message/MessageHelper.java @@ -6,19 +6,18 @@ import com.relogiclabs.jschema.type.EType; public final class MessageHelper { - public static final String ValidationFailed = "Validation failed"; - public static final String ValueMismatch = "Value mismatch"; - public static final String DataTypeMismatch = "Data type mismatch"; + public static final String ValueMismatched = "Value mismatched"; + public static final String DataTypeMismatched = "Data type mismatched"; public static final String InvalidNonCompositeType = "Invalid non-composite value type"; + public static final String NestedFunctionFailed = "Nested function failed for invalid target"; public static final String DataTypeArgumentFailed = "Data type argument failed"; - public static final String InvalidNestedFunction = "Invalid nested function operation"; public static final String PropertyNotFound = "Mandatory property not found"; public static final String ArrayElementNotFound = "Mandatory array element not found"; public static final String UndefinedPropertyFound = "Undefined property found"; - public static final String PropertyKeyMismatch = "Property key mismatch"; - public static final String PropertyValueMismatch = "Property value mismatch"; - public static final String PropertyOrderMismatch = "Property order mismatch"; + public static final String PropertyKeyMismatched = "Property key mismatched"; + public static final String PropertyValueMismatched = "Property value mismatched"; + public static final String PropertyOrderMismatched = "Property order mismatched"; private MessageHelper() { throw new UnsupportedOperationException(); @@ -26,8 +25,8 @@ private MessageHelper() { public static String getTypeName(JNode node) { return node instanceof JsonTypable jsonTypable - ? jsonTypable.getType().getName() - : node.getClass().getName(); + ? jsonTypable.getType().getName() + : node.getClass().getName(); } public static String getTypeName(Class type) { diff --git a/src/main/java/com/relogiclabs/jschema/internal/util/LexerErrorListener.java b/src/main/java/com/relogiclabs/jschema/internal/util/LexerErrorListener.java index 397c42e..a3080bf 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/util/LexerErrorListener.java +++ b/src/main/java/com/relogiclabs/jschema/internal/util/LexerErrorListener.java @@ -1,6 +1,5 @@ package com.relogiclabs.jschema.internal.util; -import com.relogiclabs.jschema.exception.CommonException; import com.relogiclabs.jschema.exception.DateTimeLexerException; import com.relogiclabs.jschema.exception.JsonLexerException; import com.relogiclabs.jschema.exception.SchemaLexerException; @@ -9,50 +8,53 @@ import org.antlr.v4.runtime.RecognitionException; import org.antlr.v4.runtime.Recognizer; -import static com.relogiclabs.jschema.message.ErrorCode.DLEX01; -import static com.relogiclabs.jschema.message.ErrorCode.JLEX01; -import static com.relogiclabs.jschema.message.ErrorCode.SLEX01; +import static com.relogiclabs.jschema.message.ErrorCode.JSNLEX01; +import static com.relogiclabs.jschema.message.ErrorCode.LEXRDT01; +import static com.relogiclabs.jschema.message.ErrorCode.SCMLEX01; import static com.relogiclabs.jschema.message.MessageFormatter.ERROR_POINTER; +import static org.apache.commons.lang3.StringUtils.isEmpty; public abstract class LexerErrorListener extends BaseErrorListener { public static final LexerErrorListener SCHEMA = new SchemaErrorListener(); public static final LexerErrorListener JSON = new JsonErrorListener(); public static final LexerErrorListener DATE_TIME = new DateTimeErrorListener(); - protected abstract CommonException failOnSyntaxError(String message, Throwable cause); + private static final String NOT_FOUND = ""; + + protected abstract RuntimeException failOnSyntaxError(String message, Throwable cause); protected abstract String getMessageFormat(); private static final class SchemaErrorListener extends LexerErrorListener { @Override - protected CommonException failOnSyntaxError(String message, Throwable cause) { - return new SchemaLexerException(SLEX01, message, cause); + protected RuntimeException failOnSyntaxError(String message, Throwable cause) { + return new SchemaLexerException(SCMLEX01, message, cause); } @Override protected String getMessageFormat() { - return "Schema (Line %d:%d) [" + SLEX01 + "]: %s (Line: %s)"; + return "Schema (Line %d:%d) [" + SCMLEX01 + "]: %s (Line: %s)"; } } private static final class JsonErrorListener extends LexerErrorListener { @Override - protected CommonException failOnSyntaxError(String message, Throwable cause) { - return new JsonLexerException(JLEX01, message, cause); + protected RuntimeException failOnSyntaxError(String message, Throwable cause) { + return new JsonLexerException(JSNLEX01, message, cause); } @Override protected String getMessageFormat() { - return "Json (Line %d:%d) [" + JLEX01 + "]: %s (Line: %s)"; + return "Json (Line %d:%d) [" + JSNLEX01 + "]: %s (Line: %s)"; } } private static final class DateTimeErrorListener extends LexerErrorListener { @Override - protected CommonException failOnSyntaxError(String message, Throwable cause) { - return new DateTimeLexerException(DLEX01, message, cause); + protected RuntimeException failOnSyntaxError(String message, Throwable cause) { + return new DateTimeLexerException(LEXRDT01, message, cause); } @Override @@ -64,12 +66,28 @@ protected String getMessageFormat() { @Override public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { - if(this == DATE_TIME) throw failOnSyntaxError(getMessageFormat().formatted(msg, - ((Lexer) recognizer).getText()), e); - var errorLine = new StringBuilder(recognizer.getInputStream().toString() - .split("\\r?\\n")[line - 1]).insert(charPositionInLine, ERROR_POINTER) - .toString().trim(); + if(this == DATE_TIME) { + var note = ((Lexer) recognizer).getText(); + if(isEmpty(note)) note = getLine(recognizer, line); + throw failOnSyntaxError(getMessageFormat().formatted(msg, note), e); + } throw failOnSyntaxError(getMessageFormat().formatted(line, charPositionInLine, - msg, errorLine), e); + msg, getErrorLine(recognizer, line, charPositionInLine)), e); + } + + private static String getErrorLine(Recognizer recognizer, int line, int charPositionInLine) { + var stream = recognizer.getInputStream(); + if(stream == null) return NOT_FOUND; + var textLine = stream.toString().lines().skip(line - 1).findFirst(); + if(textLine.isEmpty()) return NOT_FOUND; + var textBuilder = new StringBuilder(textLine.get()); + if(textBuilder.length() < charPositionInLine) return NOT_FOUND; + return textBuilder.insert(charPositionInLine, ERROR_POINTER).toString().trim(); + } + + private static String getLine(Recognizer recognizer, int line) { + var stream = recognizer.getInputStream(); + if(stream == null) return NOT_FOUND; + return stream.toString().lines().skip(line - 1).findFirst().orElse(NOT_FOUND).trim(); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/util/LogHelper.java b/src/main/java/com/relogiclabs/jschema/internal/util/LogHelper.java index e51604e..5fbee86 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/util/LogHelper.java +++ b/src/main/java/com/relogiclabs/jschema/internal/util/LogHelper.java @@ -1,7 +1,7 @@ package com.relogiclabs.jschema.internal.util; -import com.relogiclabs.jschema.exception.CommonException; -import com.relogiclabs.jschema.exception.ScriptRuntimeException; +import com.relogiclabs.jschema.exception.BaseRuntimeException; +import com.relogiclabs.jschema.exception.MultilevelRuntimeException; import com.relogiclabs.jschema.internal.time.DateTimeContext; import com.relogiclabs.jschema.tree.DataTree; import org.antlr.v4.runtime.Parser; @@ -11,7 +11,7 @@ import java.util.Collections; import java.util.List; -import static com.relogiclabs.jschema.message.ErrorCode.TRYS01; +import static com.relogiclabs.jschema.message.ErrorCode.TRYFSE01; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; // Logging library may require @@ -61,10 +61,12 @@ public static void log(Exception exception, Token token) { if(level > INFO) return; System.out.println("[INFO] [TRYOF ERROR]: " + exception.getMessage()); if(level > DEBUG) return; - Exception ex = exception instanceof CommonException ? exception - : new ScriptRuntimeException(formatForSchema( - TRYS01, exception.getMessage(), token), exception); + if((exception instanceof MultilevelRuntimeException ex && ex.isLowLevel()) + || !(exception instanceof BaseRuntimeException)) { + exception = new BaseRuntimeException(formatForSchema(TRYFSE01, + exception.getMessage(), token), exception); + } System.out.print("[DEBUG] [TRYOF ERROR]: "); - ex.printStackTrace(System.out); + exception.printStackTrace(System.out); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/util/MatchResult.java b/src/main/java/com/relogiclabs/jschema/internal/util/MatchResult.java new file mode 100644 index 0000000..e81814d --- /dev/null +++ b/src/main/java/com/relogiclabs/jschema/internal/util/MatchResult.java @@ -0,0 +1,21 @@ +package com.relogiclabs.jschema.internal.util; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public final class MatchResult { + public static final MatchResult SUCCESS = new MatchResult(true); + public static final MatchResult FAILURE = new MatchResult(false); + + private final boolean success; + private final String note; + private final String pattern; + + public MatchResult(boolean success) { + this.success = success; + this.note = null; + this.pattern = null; + } +} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/util/ParserErrorListener.java b/src/main/java/com/relogiclabs/jschema/internal/util/ParserErrorListener.java index 9d31dc2..bc8be09 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/util/ParserErrorListener.java +++ b/src/main/java/com/relogiclabs/jschema/internal/util/ParserErrorListener.java @@ -1,6 +1,5 @@ package com.relogiclabs.jschema.internal.util; -import com.relogiclabs.jschema.exception.CommonException; import com.relogiclabs.jschema.exception.JsonParserException; import com.relogiclabs.jschema.exception.SchemaParserException; import org.antlr.v4.runtime.BaseErrorListener; @@ -8,38 +7,40 @@ import org.antlr.v4.runtime.RecognitionException; import org.antlr.v4.runtime.Recognizer; -import static com.relogiclabs.jschema.message.ErrorCode.JPRS01; -import static com.relogiclabs.jschema.message.ErrorCode.SPRS01; +import static com.relogiclabs.jschema.message.ErrorCode.JSNPRS01; +import static com.relogiclabs.jschema.message.ErrorCode.SCMPRS01; import static com.relogiclabs.jschema.message.MessageFormatter.ERROR_POINTER; public abstract class ParserErrorListener extends BaseErrorListener { public static final ParserErrorListener SCHEMA = new SchemaErrorListener(); public static final ParserErrorListener JSON = new JsonErrorListener(); - protected abstract CommonException failOnSyntaxError(String message, Throwable cause); + private static final String NOT_FOUND = ""; + + protected abstract RuntimeException failOnSyntaxError(String message, Throwable cause); protected abstract String getMessageFormat(); private static final class SchemaErrorListener extends ParserErrorListener { @Override - protected CommonException failOnSyntaxError(String message, Throwable cause) { - return new SchemaParserException(SPRS01, message, cause); + protected RuntimeException failOnSyntaxError(String message, Throwable cause) { + return new SchemaParserException(SCMPRS01, message, cause); } @Override protected String getMessageFormat() { - return "Schema (Line %d:%d) [" + SPRS01 + "]: %s (Line: %s)"; + return "Schema (Line %d:%d) [" + SCMPRS01 + "]: %s (Line: %s)"; } } private static final class JsonErrorListener extends ParserErrorListener { @Override - protected CommonException failOnSyntaxError(String message, Throwable cause) { - return new JsonParserException(JPRS01, message, cause); + protected RuntimeException failOnSyntaxError(String message, Throwable cause) { + return new JsonParserException(JSNPRS01, message, cause); } @Override protected String getMessageFormat() { - return "Json (Line %d:%d) [" + JPRS01 + "]: %s (Line: %s)"; + return "Json (Line %d:%d) [" + JSNPRS01 + "]: %s (Line: %s)"; } } @@ -47,10 +48,19 @@ protected String getMessageFormat() { public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { LogHelper.debug(recognizer); - var errorLine = new StringBuilder(((CommonTokenStream) recognizer.getInputStream()) - .getTokenSource().getInputStream().toString().split("\\r?\\n")[line - 1]) - .insert(charPositionInLine, ERROR_POINTER).toString().trim(); throw failOnSyntaxError(getMessageFormat().formatted(line, charPositionInLine, - msg, errorLine), e); + msg, getErrorLine(recognizer, line, charPositionInLine)), e); + } + + private static String getErrorLine(Recognizer recognizer, int line, int charPositionInLine) { + var tokenStream = (CommonTokenStream) recognizer.getInputStream(); + if(tokenStream == null) return NOT_FOUND; + var inputStream = tokenStream.getTokenSource().getInputStream(); + if(inputStream == null) return NOT_FOUND; + var textLine = inputStream.toString().lines().skip(line - 1).findFirst(); + if(textLine.isEmpty()) return NOT_FOUND; + var textBuilder = new StringBuilder(textLine.get()); + if(textBuilder.length() < charPositionInLine) return NOT_FOUND; + return textBuilder.insert(charPositionInLine, ERROR_POINTER).toString().trim(); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/node/JAlias.java b/src/main/java/com/relogiclabs/jschema/node/JAlias.java index 618b333..2bb588b 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JAlias.java +++ b/src/main/java/com/relogiclabs/jschema/node/JAlias.java @@ -1,11 +1,11 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.DefinitionNotFoundException; +import com.relogiclabs.jschema.exception.AliasNotFoundException; import com.relogiclabs.jschema.internal.builder.JAliasBuilder; import lombok.EqualsAndHashCode; import lombok.Getter; -import static com.relogiclabs.jschema.message.ErrorCode.DEFI02; +import static com.relogiclabs.jschema.message.ErrorCode.ALSDEF01; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; import static java.util.Objects.requireNonNull; @@ -26,8 +26,8 @@ public static JAlias from(JAliasBuilder builder) { @Override public boolean match(JNode node) { var definitions = getRuntime().getDefinitions(); - if(!definitions.containsKey(this)) throw new DefinitionNotFoundException(formatForSchema( - DEFI02, "Definition of '" + name + "' not found", getContext())); + if(!definitions.containsKey(this)) throw new AliasNotFoundException(formatForSchema( + ALSDEF01, "Definition of '" + name + "' not found", getContext())); return definitions.get(this).match(node); } diff --git a/src/main/java/com/relogiclabs/jschema/node/JArray.java b/src/main/java/com/relogiclabs/jschema/node/JArray.java index 2923603..59794be 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JArray.java +++ b/src/main/java/com/relogiclabs/jschema/node/JArray.java @@ -1,8 +1,8 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.JsonSchemaException; import com.relogiclabs.jschema.exception.MisplacedOptionalException; import com.relogiclabs.jschema.exception.UpdateNotSupportedException; +import com.relogiclabs.jschema.exception.ValueValidationException; import com.relogiclabs.jschema.internal.builder.JArrayBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -18,9 +18,9 @@ import static com.relogiclabs.jschema.internal.message.MessageHelper.ArrayElementNotFound; import static com.relogiclabs.jschema.internal.util.StringHelper.joinWith; -import static com.relogiclabs.jschema.message.ErrorCode.ARRY01; -import static com.relogiclabs.jschema.message.ErrorCode.ARRY02; -import static com.relogiclabs.jschema.message.ErrorCode.AUPD01; +import static com.relogiclabs.jschema.message.ErrorCode.ARRELM01; +import static com.relogiclabs.jschema.message.ErrorCode.ARROPT01; +import static com.relogiclabs.jschema.message.ErrorCode.ROARRY01; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; import static java.util.Objects.requireNonNull; @@ -52,14 +52,14 @@ public boolean match(JNode node) { for(var i = 0; i < elements.size(); i++) { var optional = isOptional(elements.get(i)); if((restOptional |= optional) != optional) - return fail(new MisplacedOptionalException(formatForSchema(ARRY02, - "Mandatory array element cannot appear after optional element", - elements.get(i)))); + return fail(new MisplacedOptionalException(formatForSchema(ARROPT01, + "Mandatory array element cannot appear after optional element", + elements.get(i)))); if(i >= other.elements.size() && !optional) - return fail(new JsonSchemaException( - new ErrorDetail(ARRY01, ArrayElementNotFound), - ExpectedHelper.asArrayElementNotFound(elements.get(i), i), - ActualHelper.asArrayElementNotFound(other, i))); + return fail(new ValueValidationException( + new ErrorDetail(ARRELM01, ArrayElementNotFound), + ExpectedHelper.asArrayElementNotFound(elements.get(i), i), + ActualHelper.asArrayElementNotFound(other))); if(i >= other.elements.size()) continue; result &= elements.get(i).match(other.elements.get(i)); } @@ -79,7 +79,7 @@ public EValue get(int index) { @Override public void set(int index, EValue value) { - throw new UpdateNotSupportedException(AUPD01, "Readonly array cannot be updated"); + throw new UpdateNotSupportedException(ROARRY01, "Readonly array cannot be updated"); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/node/JBoolean.java b/src/main/java/com/relogiclabs/jschema/node/JBoolean.java index 52e6736..e3951c2 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JBoolean.java +++ b/src/main/java/com/relogiclabs/jschema/node/JBoolean.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.ValueValidationException; import com.relogiclabs.jschema.internal.builder.JBooleanBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -8,8 +8,8 @@ import com.relogiclabs.jschema.type.EBoolean; import lombok.EqualsAndHashCode; -import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatch; -import static com.relogiclabs.jschema.message.ErrorCode.BOOL01; +import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatched; +import static com.relogiclabs.jschema.message.ErrorCode.BOOLVL01; import static java.util.Objects.requireNonNull; @EqualsAndHashCode @@ -30,10 +30,10 @@ public boolean match(JNode node) { var other = castType(node, JBoolean.class); if(other == null) return false; if(value == other.value) return true; - return fail(new JsonSchemaException( - new ErrorDetail(BOOL01, ValueMismatch), - ExpectedHelper.asValueMismatch(this), - ActualHelper.asValueMismatch(other))); + return fail(new ValueValidationException( + new ErrorDetail(BOOLVL01, ValueMismatched), + ExpectedHelper.asValueMismatched(this), + ActualHelper.asValueMismatched(other))); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/node/JDataType.java b/src/main/java/com/relogiclabs/jschema/node/JDataType.java index 445b53a..10af752 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JDataType.java +++ b/src/main/java/com/relogiclabs/jschema/node/JDataType.java @@ -1,34 +1,32 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.DefinitionNotFoundException; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.AliasNotFoundException; +import com.relogiclabs.jschema.exception.DataTypeValidationException; import com.relogiclabs.jschema.internal.builder.JDataTypeBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; +import com.relogiclabs.jschema.internal.util.MatchResult; import com.relogiclabs.jschema.message.ErrorDetail; -import com.relogiclabs.jschema.util.Reference; import lombok.EqualsAndHashCode; import lombok.Getter; import static com.relogiclabs.jschema.internal.message.MessageHelper.DataTypeArgumentFailed; -import static com.relogiclabs.jschema.internal.message.MessageHelper.DataTypeMismatch; +import static com.relogiclabs.jschema.internal.message.MessageHelper.DataTypeMismatched; import static com.relogiclabs.jschema.internal.util.CollectionHelper.asList; -import static com.relogiclabs.jschema.message.ErrorCode.DEFI03; -import static com.relogiclabs.jschema.message.ErrorCode.DEFI04; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP05; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP06; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP07; +import static com.relogiclabs.jschema.message.ErrorCode.ALSDEF02; +import static com.relogiclabs.jschema.message.ErrorCode.ALSDEF03; +import static com.relogiclabs.jschema.message.ErrorCode.DTYARG01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYARG02; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS02; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; import static java.util.Objects.requireNonNull; -import static org.apache.commons.lang3.StringUtils.isEmpty; import static org.apache.commons.lang3.StringUtils.uncapitalize; @Getter @EqualsAndHashCode public final class JDataType extends JBranch implements NestedMode { static final String NESTED_MARKER = "*"; - static final String DATA_TYPE_NAME = "DATA_TYPE_NAME"; private final JsonType jsonType; private final JAlias alias; private final boolean nested; @@ -47,30 +45,30 @@ public static JDataType from(JDataTypeBuilder builder) { @Override public boolean match(JNode node) { - Reference error = new Reference<>(); - if(!jsonType.match(node, error)) return failTypeWith(new JsonSchemaException( - new ErrorDetail(nested ? DTYP06 : DTYP04, - formatMessage(DataTypeMismatch, error.getValue())), - ExpectedHelper.asDataTypeMismatch(this), - ActualHelper.asDataTypeMismatch(node))); + var result = jsonType.match(node); + if(!result.isSuccess()) return failOnType(new DataTypeValidationException( + new ErrorDetail(nested ? DTYPMS02 : DTYPMS01, formatMessage(result)), + ExpectedHelper.asDataTypeMismatched(this, result), + ActualHelper.asDataTypeMismatched(node))); if(alias == null) return true; var validator = getRuntime().getDefinitions().get(alias); - if(validator == null) return fail(new DefinitionNotFoundException(formatForSchema( - nested ? DEFI04 : DEFI03, "No definition found for '" + alias + "'", this))); - if(!validator.match(node)) return fail(new JsonSchemaException( - new ErrorDetail(nested ? DTYP07 : DTYP05, DataTypeArgumentFailed), + if(validator == null) return fail(new AliasNotFoundException(formatForSchema( + nested ? ALSDEF03 : ALSDEF02, "No definition found for '" + alias + "'", this))); + if(!validator.match(node)) return fail(new DataTypeValidationException( + new ErrorDetail(nested ? DTYARG02 : DTYARG01, DataTypeArgumentFailed), ExpectedHelper.asDataTypeArgumentFailed(this), ActualHelper.asDataTypeArgumentFailed(node))); return true; } - private boolean failTypeWith(JsonSchemaException exception) { - exception.setAttribute(DATA_TYPE_NAME, toString(true)); + private boolean failOnType(DataTypeValidationException exception) { + exception.setTypeBaseName(toString(true)); return fail(exception); } - private static String formatMessage(String main, String optional) { - return isEmpty(optional) ? main : main + " (" + uncapitalize(optional) + ")"; + private static String formatMessage(MatchResult result) { + if(result.getNote() == null) return DataTypeMismatched; + return DataTypeMismatched + " (" + uncapitalize(result.getNote()) + ")"; } boolean isApplicable(JNode node) { diff --git a/src/main/java/com/relogiclabs/jschema/node/JDouble.java b/src/main/java/com/relogiclabs/jschema/node/JDouble.java index 5bb5876..69d806f 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JDouble.java +++ b/src/main/java/com/relogiclabs/jschema/node/JDouble.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.ValueValidationException; import com.relogiclabs.jschema.internal.builder.JDoubleBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -11,8 +11,8 @@ import java.text.DecimalFormat; -import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatch; -import static com.relogiclabs.jschema.message.ErrorCode.DUBL01; +import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatched; +import static com.relogiclabs.jschema.message.ErrorCode.DUBLVL01; import static java.util.Objects.requireNonNull; @Getter @@ -35,10 +35,10 @@ public boolean match(JNode node) { var other = castType(node, JDouble.class); if(other == null) return false; if(areEqual(value, other.value)) return true; - return fail(new JsonSchemaException( - new ErrorDetail(DUBL01, ValueMismatch), - ExpectedHelper.asValueMismatch(this), - ActualHelper.asValueMismatch(other))); + return fail(new ValueValidationException( + new ErrorDetail(DUBLVL01, ValueMismatched), + ExpectedHelper.asValueMismatched(this), + ActualHelper.asValueMismatched(other))); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/node/JFloat.java b/src/main/java/com/relogiclabs/jschema/node/JFloat.java index ac8edb9..8fd007c 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JFloat.java +++ b/src/main/java/com/relogiclabs/jschema/node/JFloat.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.ValueValidationException; import com.relogiclabs.jschema.internal.builder.JFloatBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -12,8 +12,8 @@ import java.text.DecimalFormat; -import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatch; -import static com.relogiclabs.jschema.message.ErrorCode.FLOT01; +import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatched; +import static com.relogiclabs.jschema.message.ErrorCode.FLOTVL01; import static java.util.Objects.requireNonNull; @Getter @@ -41,10 +41,10 @@ public boolean match(JNode node) { var other = castType(node, JFloat.class); if(other == null) return false; if(areEqual(value, other.value)) return true; - return fail(new JsonSchemaException( - new ErrorDetail(FLOT01, ValueMismatch), - ExpectedHelper.asValueMismatch(this), - ActualHelper.asValueMismatch(other))); + return fail(new ValueValidationException( + new ErrorDetail(FLOTVL01, ValueMismatched), + ExpectedHelper.asValueMismatched(this), + ActualHelper.asValueMismatched(other))); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/node/JFunction.java b/src/main/java/com/relogiclabs/jschema/node/JFunction.java index 91d0364..708b8b3 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JFunction.java +++ b/src/main/java/com/relogiclabs/jschema/node/JFunction.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.exception.TargetInvocationException; import com.relogiclabs.jschema.internal.builder.JFunctionBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; @@ -12,11 +12,11 @@ import java.util.List; -import static com.relogiclabs.jschema.internal.message.MessageHelper.InvalidNestedFunction; +import static com.relogiclabs.jschema.internal.message.MessageHelper.NestedFunctionFailed; import static com.relogiclabs.jschema.internal.util.CommonHelper.nonNullFrom; import static com.relogiclabs.jschema.internal.util.StreamHelper.forEachTrue; import static com.relogiclabs.jschema.internal.util.StringHelper.join; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC06; +import static com.relogiclabs.jschema.message.ErrorCode.FNCFAL01; import static java.util.Objects.requireNonNull; @Getter @@ -44,11 +44,10 @@ public static JFunction from(JFunctionBuilder builder) { @Override public boolean match(JNode node) { if(!nested) return invokeFunction(node); - if(!(node instanceof JComposite composite)) - return fail(new JsonSchemaException( - new ErrorDetail(FUNC06, InvalidNestedFunction), - ExpectedHelper.asInvalidFunction(this), - ActualHelper.asInvalidFunction(node))); + if(!(node instanceof JComposite composite)) return fail(new FunctionValidationException( + new ErrorDetail(FNCFAL01, NestedFunctionFailed), + ExpectedHelper.asNestedFunctionFailed(this), + ActualHelper.asNestedFunctionFailed(node))); return forEachTrue(composite.components().stream().map(this::invokeFunction)); } diff --git a/src/main/java/com/relogiclabs/jschema/node/JInteger.java b/src/main/java/com/relogiclabs/jschema/node/JInteger.java index 5c14974..9b37fc8 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JInteger.java +++ b/src/main/java/com/relogiclabs/jschema/node/JInteger.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.ValueValidationException; import com.relogiclabs.jschema.internal.builder.JIntegerBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -9,8 +9,8 @@ import lombok.EqualsAndHashCode; import lombok.Getter; -import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatch; -import static com.relogiclabs.jschema.message.ErrorCode.INTE01; +import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatched; +import static com.relogiclabs.jschema.message.ErrorCode.INTVAL01; import static java.util.Objects.requireNonNull; @Getter @@ -32,10 +32,10 @@ public boolean match(JNode node) { var other = castType(node, JInteger.class); if(other == null) return false; if(value == other.value) return true; - return fail(new JsonSchemaException( - new ErrorDetail(INTE01, ValueMismatch), - ExpectedHelper.asValueMismatch(this), - ActualHelper.asValueMismatch(other))); + return fail(new ValueValidationException( + new ErrorDetail(INTVAL01, ValueMismatched), + ExpectedHelper.asValueMismatched(this), + ActualHelper.asValueMismatched(other))); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/node/JNode.java b/src/main/java/com/relogiclabs/jschema/node/JNode.java index 61d39cd..d02de25 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JNode.java +++ b/src/main/java/com/relogiclabs/jschema/node/JNode.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.DataTypeValidationException; import com.relogiclabs.jschema.internal.builder.JNodeBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -13,8 +13,8 @@ import java.util.Collection; import java.util.Map; -import static com.relogiclabs.jschema.internal.message.MessageHelper.DataTypeMismatch; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP02; +import static com.relogiclabs.jschema.internal.message.MessageHelper.DataTypeMismatched; +import static com.relogiclabs.jschema.message.ErrorCode.DTYCST01; import static com.relogiclabs.jschema.message.OutlineFormatter.createOutline; import static java.util.Collections.emptyList; import static java.util.Objects.requireNonNull; @@ -53,10 +53,10 @@ public RuntimeContext getRuntime() { T castType(JNode node, Class type) { if(type.isInstance(node)) return type.cast(node); - fail(new JsonSchemaException( - new ErrorDetail(DTYP02, DataTypeMismatch), - ExpectedHelper.asDataTypeMismatch(this), - ActualHelper.asDataTypeMismatch(node))); + fail(new DataTypeValidationException( + new ErrorDetail(DTYCST01, DataTypeMismatched), + ExpectedHelper.asDataTypeMismatched(this), + ActualHelper.asDataTypeMismatched(node))); return null; } diff --git a/src/main/java/com/relogiclabs/jschema/node/JObject.java b/src/main/java/com/relogiclabs/jschema/node/JObject.java index ba04954..f30a8a6 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JObject.java +++ b/src/main/java/com/relogiclabs/jschema/node/JObject.java @@ -1,8 +1,9 @@ package com.relogiclabs.jschema.node; import com.relogiclabs.jschema.collection.IndexMap; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.PropertyOrderException; import com.relogiclabs.jschema.exception.UpdateNotSupportedException; +import com.relogiclabs.jschema.exception.ValueValidationException; import com.relogiclabs.jschema.internal.builder.JObjectBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -19,14 +20,14 @@ import java.util.Set; import static com.relogiclabs.jschema.internal.message.MessageHelper.PropertyNotFound; -import static com.relogiclabs.jschema.internal.message.MessageHelper.PropertyOrderMismatch; +import static com.relogiclabs.jschema.internal.message.MessageHelper.PropertyOrderMismatched; import static com.relogiclabs.jschema.internal.message.MessageHelper.UndefinedPropertyFound; import static com.relogiclabs.jschema.internal.util.CommonHelper.nonNullFrom; import static com.relogiclabs.jschema.internal.util.StringHelper.joinWith; -import static com.relogiclabs.jschema.message.ErrorCode.OUPD01; -import static com.relogiclabs.jschema.message.ErrorCode.PROP05; -import static com.relogiclabs.jschema.message.ErrorCode.PROP06; -import static com.relogiclabs.jschema.message.ErrorCode.PROP07; +import static com.relogiclabs.jschema.message.ErrorCode.PRTFND01; +import static com.relogiclabs.jschema.message.ErrorCode.PRTORD01; +import static com.relogiclabs.jschema.message.ErrorCode.PRTUDF01; +import static com.relogiclabs.jschema.message.ErrorCode.ROOBJT01; import static java.util.Objects.requireNonNull; @EqualsAndHashCode @@ -53,26 +54,34 @@ public boolean match(JNode node) { var unresolved = new HashSet<>(other.properties.keySet()); for(var i = 0; i < properties.size(); i++) { var thisProp = properties.get(i); - var otherProp = getOtherProp(other, i); - if(otherProp != null) { - result &= thisProp.getValue().match(otherProp.getValue()); + try { + var otherProp = getOtherProp(other, i); + if(otherProp != null) { + result &= thisProp.getValue().match(otherProp.getValue()); + unresolved.remove(thisProp.getKey()); + continue; + } + if(!((JValidator) thisProp.getValue()).isOptional()) + result &= fail(new ValueValidationException( + new ErrorDetail(PRTFND01, PropertyNotFound), + ExpectedHelper.asPropertyNotFound(thisProp), + ActualHelper.asPropertyNotFound(node, thisProp, other))); + } catch(PropertyOrderException e) { + result &= fail(new ValueValidationException( + new ErrorDetail(e.getCode(), e.getMessage()), + ExpectedHelper.asPropertyOrderMismatched(thisProp), + ActualHelper.asPropertyOrderMismatched(nonNullFrom(e.getFailOn(), other)), e)); unresolved.remove(thisProp.getKey()); - continue; } - if(!((JValidator) thisProp.getValue()).isOptional()) - return fail(new JsonSchemaException( - new ErrorDetail(PROP05, PropertyNotFound), - ExpectedHelper.asPropertyNotFound(thisProp), - ActualHelper.asPropertyNotFound(node, thisProp))); } if(unresolved.isEmpty() || getRuntime().getPragmas() - .isIgnoreUndefinedProperties()) return result; + .isIgnoreUndefinedProperties()) return result; for(String key : unresolved) { var property = other.properties.get(key); - result &= fail(new JsonSchemaException( - new ErrorDetail(PROP06, UndefinedPropertyFound), - ExpectedHelper.asUndefinedProperty(this, property), - ActualHelper.asUndefinedProperty(property))); + result &= fail(new ValueValidationException( + new ErrorDetail(PRTUDF01, UndefinedPropertyFound), + ExpectedHelper.asUndefinedPropertyFound(this, property), + ActualHelper.asUndefinedPropertyFound(property))); } return result; } @@ -85,11 +94,8 @@ private JProperty getOtherProp(JObject other, int index) { if(areKeysEqual(atProp, thisProp)) otherProp = atProp; JProperty existing = null; if(otherProp == null) existing = other.properties.get(thisProp.getKey()); - if(otherProp == null && existing != null) - fail(new JsonSchemaException( - new ErrorDetail(PROP07, PropertyOrderMismatch), - ExpectedHelper.asPropertyOrderMismatch(thisProp), - ActualHelper.asPropertyOrderMismatch(nonNullFrom(atProp, other)))); + if(otherProp == null && existing != null) throw new PropertyOrderException(PRTORD01, + PropertyOrderMismatched, atProp); } else otherProp = other.properties.get(thisProp.getKey()); return otherProp; } @@ -116,7 +122,7 @@ public EValue get(String key) { @Override public void set(String key, EValue value) { - throw new UpdateNotSupportedException(OUPD01, "Readonly object cannot be updated"); + throw new UpdateNotSupportedException(ROOBJT01, "Readonly object cannot be updated"); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/node/JProperty.java b/src/main/java/com/relogiclabs/jschema/node/JProperty.java index 118c56c..5fe376a 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JProperty.java +++ b/src/main/java/com/relogiclabs/jschema/node/JProperty.java @@ -1,7 +1,7 @@ package com.relogiclabs.jschema.node; import com.relogiclabs.jschema.collection.Keyable; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.ValueValidationException; import com.relogiclabs.jschema.internal.builder.JPropertyBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -10,12 +10,12 @@ import java.util.Objects; -import static com.relogiclabs.jschema.internal.message.MessageHelper.PropertyKeyMismatch; -import static com.relogiclabs.jschema.internal.message.MessageHelper.PropertyValueMismatch; +import static com.relogiclabs.jschema.internal.message.MessageHelper.PropertyKeyMismatched; +import static com.relogiclabs.jschema.internal.message.MessageHelper.PropertyValueMismatched; import static com.relogiclabs.jschema.internal.util.CollectionHelper.asList; import static com.relogiclabs.jschema.internal.util.StringHelper.quote; -import static com.relogiclabs.jschema.message.ErrorCode.PROP01; -import static com.relogiclabs.jschema.message.ErrorCode.PROP02; +import static com.relogiclabs.jschema.message.ErrorCode.PROPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.PROPMS02; import static java.util.Objects.requireNonNull; @Getter @@ -51,14 +51,14 @@ public int hashCode() { public boolean match(JNode node) { var other = castType(node, JProperty.class); if(other == null) return false; - if(!key.equals(other.key)) return fail(new JsonSchemaException( - new ErrorDetail(PROP01, PropertyKeyMismatch), - ExpectedHelper.asValueMismatch(this), - ActualHelper.asValueMismatch(other))); - if(!value.match(other.value)) return fail(new JsonSchemaException( - new ErrorDetail(PROP02, PropertyValueMismatch), - ExpectedHelper.asValueMismatch(this), - ActualHelper.asValueMismatch(other))); + if(!key.equals(other.key)) return fail(new ValueValidationException( + new ErrorDetail(PROPMS01, PropertyKeyMismatched), + ExpectedHelper.asValueMismatched(this), + ActualHelper.asValueMismatched(other))); + if(!value.match(other.value)) return fail(new ValueValidationException( + new ErrorDetail(PROPMS02, PropertyValueMismatched), + ExpectedHelper.asValueMismatched(this), + ActualHelper.asValueMismatched(other))); return true; } diff --git a/src/main/java/com/relogiclabs/jschema/node/JReceiver.java b/src/main/java/com/relogiclabs/jschema/node/JReceiver.java index 779a5dd..8a59cdb 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JReceiver.java +++ b/src/main/java/com/relogiclabs/jschema/node/JReceiver.java @@ -12,10 +12,10 @@ import java.util.Iterator; import java.util.List; -import static com.relogiclabs.jschema.message.ErrorCode.AUPD02; -import static com.relogiclabs.jschema.message.ErrorCode.RECV01; -import static com.relogiclabs.jschema.message.ErrorCode.RECV02; -import static com.relogiclabs.jschema.message.ErrorCode.RECV03; +import static com.relogiclabs.jschema.message.ErrorCode.RECVER01; +import static com.relogiclabs.jschema.message.ErrorCode.RECVER02; +import static com.relogiclabs.jschema.message.ErrorCode.RECVER03; +import static com.relogiclabs.jschema.message.ErrorCode.ROARRY02; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; import static java.util.Objects.requireNonNull; @@ -44,7 +44,7 @@ public int getValueCount() { private List fetchValueNodes() { var list = getRuntime().getReceivers().fetch(this); - if(list == null) throw new ReceiverNotFoundException(formatForSchema(RECV01, + if(list == null) throw new ReceiverNotFoundException(formatForSchema(RECVER01, "Receiver '" + name + "' not found", this)); return list; } @@ -52,9 +52,9 @@ private List fetchValueNodes() { @SuppressWarnings("unchecked") public T getValueNode() { var list = fetchValueNodes(); - if(list.isEmpty()) throw new InvalidReceiverStateException(formatForSchema(RECV02, + if(list.isEmpty()) throw new InvalidReceiverStateException(formatForSchema(RECVER02, "No value received for '" + name + "'", this)); - if(list.size() > 1) throw new InvalidReceiverStateException(formatForSchema(RECV03, + if(list.size() > 1) throw new InvalidReceiverStateException(formatForSchema(RECVER03, "Multiple values received for '" + name + "'", this)); return (T) list.get(0); } @@ -72,7 +72,7 @@ public EValue get(int index) { @Override public void set(int index, EValue value) { - throw new UpdateNotSupportedException(AUPD02, "Readonly array cannot be updated"); + throw new UpdateNotSupportedException(ROARRY02, "Readonly array cannot be updated"); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/node/JString.java b/src/main/java/com/relogiclabs/jschema/node/JString.java index 40f50e6..e753c04 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JString.java +++ b/src/main/java/com/relogiclabs/jschema/node/JString.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.ValueValidationException; import com.relogiclabs.jschema.internal.builder.JStringBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -10,9 +10,9 @@ import lombok.Getter; import lombok.Setter; -import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatch; +import static com.relogiclabs.jschema.internal.message.MessageHelper.ValueMismatched; import static com.relogiclabs.jschema.internal.util.StringHelper.quote; -import static com.relogiclabs.jschema.message.ErrorCode.STRV01; +import static com.relogiclabs.jschema.message.ErrorCode.STRVAL01; import static java.util.Objects.requireNonNull; @Getter @@ -40,10 +40,10 @@ public boolean match(JNode node) { var other = castType(node, JString.class); if(other == null) return false; if(value.equals(other.value)) return true; - return fail(new JsonSchemaException( - new ErrorDetail(STRV01, ValueMismatch), - ExpectedHelper.asValueMismatch(this), - ActualHelper.asValueMismatch(other))); + return fail(new ValueValidationException( + new ErrorDetail(STRVAL01, ValueMismatched), + ExpectedHelper.asValueMismatched(this), + ActualHelper.asValueMismatched(other))); } @Override diff --git a/src/main/java/com/relogiclabs/jschema/node/JValidator.java b/src/main/java/com/relogiclabs/jschema/node/JValidator.java index 065e7f0..9117c35 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JValidator.java +++ b/src/main/java/com/relogiclabs/jschema/node/JValidator.java @@ -1,6 +1,7 @@ package com.relogiclabs.jschema.node; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.DataTypeValidationException; +import com.relogiclabs.jschema.exception.ValidationException; import com.relogiclabs.jschema.internal.builder.JValidatorBuilder; import com.relogiclabs.jschema.internal.message.ActualHelper; import com.relogiclabs.jschema.internal.message.ExpectedHelper; @@ -13,16 +14,16 @@ import java.util.LinkedList; import java.util.List; -import static com.relogiclabs.jschema.internal.message.MessageHelper.DataTypeMismatch; +import static com.relogiclabs.jschema.internal.message.MessageHelper.DataTypeMismatched; import static com.relogiclabs.jschema.internal.message.MessageHelper.InvalidNonCompositeType; import static com.relogiclabs.jschema.internal.message.MessageHelper.ValidationFailed; import static com.relogiclabs.jschema.internal.util.CollectionHelper.addToList; import static com.relogiclabs.jschema.internal.util.CollectionHelper.tryGetLast; +import static com.relogiclabs.jschema.internal.util.CommonHelper.nonNullFrom; import static com.relogiclabs.jschema.internal.util.StreamHelper.forEachTrue; import static com.relogiclabs.jschema.internal.util.StringHelper.join; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP03; -import static com.relogiclabs.jschema.message.ErrorCode.VALD01; -import static com.relogiclabs.jschema.node.JDataType.DATA_TYPE_NAME; +import static com.relogiclabs.jschema.message.ErrorCode.DTYCPS01; +import static com.relogiclabs.jschema.message.ErrorCode.VALDFL01; import static java.util.Collections.unmodifiableCollection; import static lombok.AccessLevel.NONE; @@ -67,18 +68,19 @@ public boolean match(JNode node) { if(other == null) return false; getRuntime().getReceivers().receive(receivers, node); if(node instanceof JNull && dataTypes.stream() - .anyMatch(JDataType::isMatchNull)) return true; + .anyMatch(JDataType::isMatchNull)) return true; if(value != null) rValue &= value.match(other.getNode()); - if(!rValue) return fail(new JsonSchemaException( - new ErrorDetail(VALD01, ValidationFailed), - ExpectedHelper.asGeneralValueMismatch(value), - ActualHelper.asGeneralValueMismatch(node))); + if(!rValue) return getRuntime().getPragmas().isEnableContextualException() + && fail(new ValidationException( + new ErrorDetail(VALDFL01, ValidationFailed), + ExpectedHelper.asGeneralValidationFailed(value), + ActualHelper.asGeneralValidationFailed(node))); var rDataType = matchDataType(node); var fDataType = rDataType && !dataTypes.isEmpty(); - boolean rFunction = forEachTrue(functions.stream() - .filter(f -> f.isApplicable(node) || !fDataType) - .map(f -> f.match(node))); - return rValue & rDataType & rFunction; + var rFunction = forEachTrue(functions.stream() + .filter(f -> f.isApplicable(node) || !fDataType) + .map(f -> f.match(node))); + return rValue && rDataType && rFunction; } private boolean matchDataType(JNode node) { @@ -98,25 +100,26 @@ private static List processTryBuffer(List buffer) { return list; } - private static JsonSchemaException mergeException(Exception ex1, Exception ex2) { - if(!(ex1 instanceof JsonSchemaException e1)) return null; - if(!(ex2 instanceof JsonSchemaException e2)) return null; + private static DataTypeValidationException mergeException(Exception ex1, Exception ex2) { + if(!(ex1 instanceof DataTypeValidationException e1)) return null; + if(!(ex2 instanceof DataTypeValidationException e2)) return null; if(!e1.getCode().equals(e2.getCode())) return null; - var a1 = e1.getAttribute(DATA_TYPE_NAME); - var a2 = e2.getAttribute(DATA_TYPE_NAME); - if(a1 == null || a2 == null) return null; - var cause = ex1.getCause() != null ? ex1.getCause() : ex1; - cause.addSuppressed(ex2); - var result = new JsonSchemaException(new ErrorDetail(e1.getCode(), DataTypeMismatch), + var t1 = e1.getTypeBaseName(); + var t2 = e2.getTypeBaseName(); + if(t1 == null || t2 == null) return null; + var cause = nonNullFrom(e1.getCause(), e1); + cause.addSuppressed(e2); + var result = new DataTypeValidationException( + new ErrorDetail(e1.getCode(), DataTypeMismatched), mergeExpected(e1, e2), e2.getActual(), cause); - result.setAttribute(DATA_TYPE_NAME, a1 + a2); + result.setTypeBaseName(t1 + t2); return result; } - private static ExpectedDetail mergeExpected(JsonSchemaException ex1, - JsonSchemaException ex2) { - var typeName2 = ex2.getAttribute(DATA_TYPE_NAME); - var expected1 = ex1.getExpected(); + private static ExpectedDetail mergeExpected(DataTypeValidationException e1, + DataTypeValidationException e2) { + var typeName2 = e2.getTypeBaseName(); + var expected1 = e1.getExpected(); return new ExpectedDetail(expected1.getContext(), expected1.getMessage() + " or " + typeName2); } @@ -129,13 +132,13 @@ private boolean checkDataType(JNode node) { && (d.isApplicable(node) || !result1)).toList(); if(list2.isEmpty()) return result1 || list1.isEmpty(); if(!(node instanceof JComposite composite)) - return fail(new JsonSchemaException( - new ErrorDetail(DTYP03, InvalidNonCompositeType), - ExpectedHelper.asInvalidNonCompositeType(list2.get(0)), - ActualHelper.asInvalidNonCompositeType(node))); + return fail(new DataTypeValidationException( + new ErrorDetail(DTYCPS01, InvalidNonCompositeType), + ExpectedHelper.asInvalidNonCompositeType(list2.get(0)), + ActualHelper.asInvalidNonCompositeType(node))); saveTryBuffer(); var result2 = forEachTrue(composite.components().stream() - .map(n -> anyMatch(list2, n))); + .map(n -> anyMatch(list2, n))); return (result1 || list1.isEmpty()) && result2; } diff --git a/src/main/java/com/relogiclabs/jschema/node/JsonType.java b/src/main/java/com/relogiclabs/jschema/node/JsonType.java index 88cae6e..9851d77 100644 --- a/src/main/java/com/relogiclabs/jschema/node/JsonType.java +++ b/src/main/java/com/relogiclabs/jschema/node/JsonType.java @@ -1,16 +1,19 @@ package com.relogiclabs.jschema.node; import com.relogiclabs.jschema.exception.InvalidDataTypeException; -import com.relogiclabs.jschema.tree.Location; +import com.relogiclabs.jschema.exception.InvalidDateTimeException; +import com.relogiclabs.jschema.internal.util.LogHelper; +import com.relogiclabs.jschema.internal.util.MatchResult; import com.relogiclabs.jschema.type.EType; -import com.relogiclabs.jschema.util.Reference; import lombok.RequiredArgsConstructor; -import org.antlr.v4.runtime.tree.TerminalNode; +import org.antlr.v4.runtime.Token; import java.util.HashMap; import java.util.Map; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP01; +import static com.relogiclabs.jschema.internal.util.MatchResult.FAILURE; +import static com.relogiclabs.jschema.internal.util.MatchResult.SUCCESS; +import static com.relogiclabs.jschema.message.ErrorCode.DTYVDF01; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; import static com.relogiclabs.jschema.type.EType.ANY; import static com.relogiclabs.jschema.type.EType.ARRAY; @@ -58,33 +61,33 @@ private static void mapType(EType type, Class typeClass) { typeClassMap.put(type, typeClass); } - public static JsonType from(TerminalNode node) { - return from(node.getText(), Location.from(node.getSymbol())); - } - - private static JsonType from(String name, Location location) { - var type = stringTypeMap.get(name); - if(type == null) throw new InvalidDataTypeException(formatForSchema(DTYP01, - "Invalid data type " + name, location)); + public static JsonType from(Token token) { + var type = stringTypeMap.get(token.getText()); + if(type == null) throw new InvalidDataTypeException(formatForSchema(DTYVDF01, + "Invalid data type " + token.getText(), token)); return new JsonType(type); } - public boolean match(JNode node, Reference error) { - if(!typeClassMap.get(type).isInstance(node)) return false; - if(type == DATE) { - var date = (JString) node; - var dateTime = node.getRuntime().getPragmas().getDateTypeParser() - .tryParse(date.getValue(), error); - if(dateTime == null) return false; - date.setDerived(JDate.from(date, dateTime)); - } else if(type == TIME) { - var time = (JString) node; - var dateTime = node.getRuntime().getPragmas().getTimeTypeParser() - .tryParse(time.getValue(), error); - if(dateTime == null) return false; - time.setDerived(JTime.from(time, dateTime)); + public MatchResult match(JNode node) { + if(!typeClassMap.get(type).isInstance(node)) return FAILURE; + try { + if(type == DATE) { + var string = (JString) node; + var dateTime = node.getRuntime().getPragmas().getDateTypeParser() + .parse(string.getValue()); + string.setDerived(JDate.from(string, dateTime)); + } else if(type == TIME) { + var string = (JString) node; + var dateTime = node.getRuntime().getPragmas().getTimeTypeParser() + .parse(string.getValue()); + string.setDerived(JTime.from(string, dateTime)); + } + } catch(InvalidDateTimeException e) { + LogHelper.debug(e); + return new MatchResult(false, e.getMessage(), + e.getContext().getParser().getPattern()); } - return true; + return SUCCESS; } boolean isNullType() { diff --git a/src/main/java/com/relogiclabs/jschema/type/EArray.java b/src/main/java/com/relogiclabs/jschema/type/EArray.java index 9019dba..44b1c1c 100644 --- a/src/main/java/com/relogiclabs/jschema/type/EArray.java +++ b/src/main/java/com/relogiclabs/jschema/type/EArray.java @@ -1,6 +1,7 @@ package com.relogiclabs.jschema.type; -import com.relogiclabs.jschema.internal.library.ArrayLibrary; +import com.relogiclabs.jschema.exception.MethodNotFoundException; +import com.relogiclabs.jschema.internal.library.ArrayMethods; import com.relogiclabs.jschema.internal.library.MethodEvaluator; import java.util.List; @@ -20,6 +21,11 @@ default EType getType() { @Override default MethodEvaluator getMethod(String name, int argCount) { - return ArrayLibrary.getInstance().getMethod(name, argCount); + try { + return ArrayMethods.getInstance().getMethod(name, argCount); + } catch(MethodNotFoundException e) { + e.setType(getType()); + throw e; + } } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/type/ENumber.java b/src/main/java/com/relogiclabs/jschema/type/ENumber.java index 6e59721..fda4fc4 100644 --- a/src/main/java/com/relogiclabs/jschema/type/ENumber.java +++ b/src/main/java/com/relogiclabs/jschema/type/ENumber.java @@ -1,7 +1,8 @@ package com.relogiclabs.jschema.type; +import com.relogiclabs.jschema.exception.MethodNotFoundException; import com.relogiclabs.jschema.internal.library.MethodEvaluator; -import com.relogiclabs.jschema.internal.library.NumberLibrary; +import com.relogiclabs.jschema.internal.library.NumberMethods; public interface ENumber extends EValue { double toDouble(); @@ -13,6 +14,11 @@ default EType getType() { @Override default MethodEvaluator getMethod(String name, int argCount) { - return NumberLibrary.getInstance().getMethod(name, argCount); + try { + return NumberMethods.getInstance().getMethod(name, argCount); + } catch(MethodNotFoundException e) { + e.setType(getType()); + throw e; + } } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/type/EObject.java b/src/main/java/com/relogiclabs/jschema/type/EObject.java index f4da580..5d4c3b7 100644 --- a/src/main/java/com/relogiclabs/jschema/type/EObject.java +++ b/src/main/java/com/relogiclabs/jschema/type/EObject.java @@ -1,7 +1,8 @@ package com.relogiclabs.jschema.type; +import com.relogiclabs.jschema.exception.MethodNotFoundException; import com.relogiclabs.jschema.internal.library.MethodEvaluator; -import com.relogiclabs.jschema.internal.library.ObjectLibrary; +import com.relogiclabs.jschema.internal.library.ObjectMethods; import java.util.Set; @@ -18,6 +19,11 @@ default EType getType() { @Override default MethodEvaluator getMethod(String name, int argCount) { - return ObjectLibrary.getInstance().getMethod(name, argCount); + try { + return ObjectMethods.getInstance().getMethod(name, argCount); + } catch(MethodNotFoundException e) { + e.setType(getType()); + throw e; + } } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/type/EString.java b/src/main/java/com/relogiclabs/jschema/type/EString.java index b5195e4..da5d928 100644 --- a/src/main/java/com/relogiclabs/jschema/type/EString.java +++ b/src/main/java/com/relogiclabs/jschema/type/EString.java @@ -1,7 +1,8 @@ package com.relogiclabs.jschema.type; +import com.relogiclabs.jschema.exception.MethodNotFoundException; import com.relogiclabs.jschema.internal.library.MethodEvaluator; -import com.relogiclabs.jschema.internal.library.StringLibrary; +import com.relogiclabs.jschema.internal.library.StringMethods; public interface EString extends EValue { String getValue(); @@ -17,6 +18,11 @@ default EType getType() { @Override default MethodEvaluator getMethod(String name, int argCount) { - return StringLibrary.getInstance().getMethod(name, argCount); + try { + return StringMethods.getInstance().getMethod(name, argCount); + } catch(MethodNotFoundException e) { + e.setType(getType()); + throw e; + } } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/type/EValue.java b/src/main/java/com/relogiclabs/jschema/type/EValue.java index 2f057fc..b612927 100644 --- a/src/main/java/com/relogiclabs/jschema/type/EValue.java +++ b/src/main/java/com/relogiclabs/jschema/type/EValue.java @@ -1,6 +1,7 @@ package com.relogiclabs.jschema.type; -import com.relogiclabs.jschema.internal.library.CommonLibrary; +import com.relogiclabs.jschema.exception.MethodNotFoundException; +import com.relogiclabs.jschema.internal.library.CommonMethods; import com.relogiclabs.jschema.internal.library.MethodEvaluator; public interface EValue extends Scriptable { @@ -31,6 +32,11 @@ default boolean toBoolean() { @Override default MethodEvaluator getMethod(String name, int argCount) { - return CommonLibrary.getInstance().getMethod(name, argCount); + try { + return CommonMethods.getInstance().getMethod(name, argCount); + } catch(MethodNotFoundException e) { + e.setType(getType()); + throw e; + } } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/util/Reference.java b/src/main/java/com/relogiclabs/jschema/util/Reference.java deleted file mode 100644 index 042ddde..0000000 --- a/src/main/java/com/relogiclabs/jschema/util/Reference.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.relogiclabs.jschema.util; - -import lombok.Data; - -@Data -public final class Reference { - private V value; - - public Reference() { - value = null; - } - - public Reference(V initialValue) { - this.value = initialValue; - } -} \ No newline at end of file From ee39b4715df1a148946527fe8a2b785238987be2 Mon Sep 17 00:00:00 2001 From: Zahid Hossain <60911932+zhossain-info@users.noreply.github.com> Date: Sun, 11 Aug 2024 22:03:28 +0600 Subject: [PATCH 5/7] Refactor and optimize source code --- .../com/relogiclabs/jschema/JsonSchema.java | 12 +- .../jschema/function/CoreFunctions1.java | 179 ++--- .../jschema/function/CoreFunctions2.java | 184 +++-- .../jschema/function/CoreFunctions3.java | 117 ++-- .../jschema/function/CoreFunctions4.java | 136 ++-- .../jschema/internal/antlr/SchemaParser.java | 22 +- .../antlr/SchemaParserBaseVisitor.java | 4 +- .../internal/antlr/SchemaParserVisitor.java | 8 +- .../internal/function/DateTimeAgent.java | 23 +- .../internal/function/FunctionLoader.java | 29 +- .../jschema/internal/grammar/SchemaParser.g4 | 2 +- .../internal/time/DateTimeContext.java | 107 ++- .../jschema/internal/time/DateTimeParser.java | 22 +- .../internal/time/SegmentProcessor.java | 146 ++-- .../jschema/internal/tree/NativeFunction.java | 14 +- .../internal/tree/SchemaTreeVisitor.java | 2 +- .../jschema/internal/tree/ScriptFunction.java | 20 +- .../jschema/internal/tree/TreeHelper.java | 10 +- .../jschema/message/ContextDetail.java | 10 +- .../jschema/message/ErrorCode.java | 658 +++++++++--------- .../jschema/message/MessageFormatter.java | 61 +- .../com/relogiclabs/jschema/tree/Context.java | 8 +- .../jschema/tree/FunctionRegistry.java | 41 +- .../relogiclabs/jschema/tree/Location.java | 14 - .../jschema/tree/RuntimeContext.java | 17 +- 25 files changed, 913 insertions(+), 933 deletions(-) delete mode 100644 src/main/java/com/relogiclabs/jschema/tree/Location.java diff --git a/src/main/java/com/relogiclabs/jschema/JsonSchema.java b/src/main/java/com/relogiclabs/jschema/JsonSchema.java index 1b24443..1b78dcd 100644 --- a/src/main/java/com/relogiclabs/jschema/JsonSchema.java +++ b/src/main/java/com/relogiclabs/jschema/JsonSchema.java @@ -45,13 +45,19 @@ public boolean isValid(String json) { /** * Writes error messages that occur during JSchema validation process, to the * standard error output stream. + * @return Returns the number of errors written in the standard error stream. */ - public void writeError() { + public int writeError() { if(exceptions.getCount() == 0) { System.out.println("No error has occurred"); - return; + return 0; } - for(var exception : exceptions) System.err.println(exception.getMessage()); + for(var exception : exceptions) System.err.println(formatMessage(exception)); + return exceptions.getCount(); + } + + private static String formatMessage(Exception exception) { + return exception.getClass().getSimpleName() + ": " + exception.getMessage(); } /** diff --git a/src/main/java/com/relogiclabs/jschema/function/CoreFunctions1.java b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions1.java index 035abe3..506d3c6 100644 --- a/src/main/java/com/relogiclabs/jschema/function/CoreFunctions1.java +++ b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions1.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.function; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.message.ActualDetail; import com.relogiclabs.jschema.message.ErrorDetail; import com.relogiclabs.jschema.message.ExpectedDetail; @@ -10,152 +10,155 @@ import com.relogiclabs.jschema.node.JString; import com.relogiclabs.jschema.node.JUndefined; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN01; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN02; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN03; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN04; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN05; -import static com.relogiclabs.jschema.message.ErrorCode.OLEN01; -import static com.relogiclabs.jschema.message.ErrorCode.OLEN02; -import static com.relogiclabs.jschema.message.ErrorCode.OLEN03; -import static com.relogiclabs.jschema.message.ErrorCode.OLEN04; -import static com.relogiclabs.jschema.message.ErrorCode.OLEN05; -import static com.relogiclabs.jschema.message.ErrorCode.SLEN01; -import static com.relogiclabs.jschema.message.ErrorCode.SLEN02; -import static com.relogiclabs.jschema.message.ErrorCode.SLEN03; -import static com.relogiclabs.jschema.message.ErrorCode.SLEN04; -import static com.relogiclabs.jschema.message.ErrorCode.SLEN05; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR01; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR02; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR03; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR04; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR05; +import static com.relogiclabs.jschema.message.ErrorCode.LENOBJ01; +import static com.relogiclabs.jschema.message.ErrorCode.LENOBJ02; +import static com.relogiclabs.jschema.message.ErrorCode.LENOBJ03; +import static com.relogiclabs.jschema.message.ErrorCode.LENOBJ04; +import static com.relogiclabs.jschema.message.ErrorCode.LENOBJ05; +import static com.relogiclabs.jschema.message.ErrorCode.LENSTR01; +import static com.relogiclabs.jschema.message.ErrorCode.LENSTR02; +import static com.relogiclabs.jschema.message.ErrorCode.LENSTR03; +import static com.relogiclabs.jschema.message.ErrorCode.LENSTR04; +import static com.relogiclabs.jschema.message.ErrorCode.LENSTR05; public abstract class CoreFunctions1 extends FunctionProvider { public boolean length(JString target, JInteger length) { - var rLength = target.length(); - if(rLength != length.getValue()) return fail(new JsonSchemaException( - new ErrorDetail(SLEN01, "Invalid length of string " + target), + var tLength = target.length(); + if(tLength != length.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENSTR01, "Target string length is invalid"), new ExpectedDetail(caller, "a string of length " + length), - new ActualDetail(target, "found " + rLength + " which does not match"))); + new ActualDetail(target, "found length " + tLength + " for target " + + target.getOutline()))); return true; } public boolean length(JArray target, JInteger length) { - var rLength = target.getElements().size(); - if(rLength != length.getValue()) return fail(new JsonSchemaException( - new ErrorDetail(ALEN01, "Invalid length of array " + target.getOutline()), + var tLength = target.getElements().size(); + if(tLength != length.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENARR01, "Target array length is invalid"), new ExpectedDetail(caller, "an array of length " + length), - new ActualDetail(target, "found " + rLength + " which does not match"))); + new ActualDetail(target, "found length " + tLength + " for target " + + target.getOutline()))); return true; } public boolean length(JObject target, JInteger length) { - var rLength = target.getProperties().size(); - if(rLength != length.getValue()) return fail(new JsonSchemaException( - new ErrorDetail(OLEN01, "Invalid size or length of object " + target.getOutline()), + var tLength = target.getProperties().size(); + if(tLength != length.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENOBJ01, "Target object size or length is invalid"), new ExpectedDetail(caller, "an object of length " + length), - new ActualDetail(target, "found " + rLength + " which does not match"))); + new ActualDetail(target, "found length " + tLength + " for target " + + target.getOutline()))); return true; } public boolean length(JString target, JInteger minimum, JInteger maximum) { var length = target.length(); - if(length < minimum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(SLEN02, - "String " + target.getOutline() + " length is outside of range"), - new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"), - new ActualDetail(target, "found " + length + " that is less than " + minimum))); - if(length > maximum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(SLEN03, - "String " + target.getOutline() + " length is outside of range"), - new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"), - new ActualDetail(target, "found " + length + " that is greater than " + maximum))); + if(length < minimum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENSTR02, "Target string length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + minimum + ", " + maximum + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is less than " + minimum))); + if(length > maximum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENSTR03, "Target string length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + minimum + ", " + maximum + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is greater than " + maximum))); return true; } public boolean length(JString target, JInteger minimum, JUndefined undefined) { var length = target.length(); - if(length < minimum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(SLEN04, - "String " + target.getOutline() + " length is outside of range"), - new ExpectedDetail(caller, "length in range [" + minimum + ", " + undefined + "]"), - new ActualDetail(target, "found " + length + " that is less than " + minimum))); + if(length < minimum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENSTR04, "Target string length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + minimum + ", " + undefined + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is less than " + minimum))); return true; } public boolean length(JString target, JUndefined undefined, JInteger maximum) { var length = target.length(); - if(length > maximum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(SLEN05, - "String " + target.getOutline() + " length is outside of range"), - new ExpectedDetail(caller, "length in range [" + undefined + ", " + maximum + "]"), - new ActualDetail(target, "found " + length + " that is greater than " + maximum))); + if(length > maximum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENSTR05, "Target string length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + undefined + ", " + maximum + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is greater than " + maximum))); return true; } public boolean length(JArray target, JInteger minimum, JInteger maximum) { var length = target.getElements().size(); - if(length < minimum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(ALEN02, - "Array " + target.getOutline() + " length is outside of range"), - new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"), - new ActualDetail(target, "found " + length + " that is less than " + minimum))); - if(length > maximum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(ALEN03, - "Array " + target.getOutline() + " length is outside of range"), - new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"), - new ActualDetail(target, "found " + length + " that is greater than " + maximum))); + if(length < minimum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENARR02, "Target array length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + minimum + ", " + maximum + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is less than " + minimum))); + if(length > maximum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENARR03, "Target array length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + minimum + ", " + maximum + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is greater than " + maximum))); return true; } public boolean length(JArray target, JInteger minimum, JUndefined undefined) { var length = target.getElements().size(); - if(length < minimum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(ALEN04, - "Array " + target.getOutline() + " length is outside of range"), - new ExpectedDetail(caller, "length in range [" + minimum + ", " + undefined + "]"), - new ActualDetail(target, "found " + length + " that is less than " + minimum))); + if(length < minimum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENARR04, "Target array length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + minimum + ", " + undefined + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is less than " + minimum))); return true; } public boolean length(JArray target, JUndefined undefined, JInteger maximum) { var length = target.getElements().size(); - if(length > maximum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(ALEN05, - "Array " + target.getOutline() + " length is outside of range"), - new ExpectedDetail(caller, "length in range [" + undefined + ", " + maximum + "]"), - new ActualDetail(target, "found " + length + " that is greater than " + maximum))); + if(length > maximum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENARR05, "Target array length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + undefined + ", " + maximum + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is greater than " + maximum))); return true; } public boolean length(JObject target, JInteger minimum, JInteger maximum) { var length = target.getProperties().size(); - if(length < minimum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(OLEN02, - "Object " + target.getOutline() + " size or length is outside of range"), - new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"), - new ActualDetail(target, "found " + length + " that is less than " + minimum))); - if(length > maximum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(OLEN03, - "Object " + target.getOutline() + " size or length is outside of range"), - new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"), - new ActualDetail(target, "found " + length + " that is greater than " + maximum))); + if(length < minimum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENOBJ02, "Target object size or length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + minimum + ", " + maximum + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is less than " + minimum))); + if(length > maximum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENOBJ03, "Target object size or length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + minimum + ", " + maximum + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is greater than " + maximum))); return true; } public boolean length(JObject target, JInteger minimum, JUndefined undefined) { var length = target.getProperties().size(); - if(length < minimum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(OLEN04, - "Object " + target.getOutline() + " size or length is outside of range"), - new ExpectedDetail(caller, "length in range [" + minimum + ", " + undefined + "]"), - new ActualDetail(target, "found " + length + " that is less than " + minimum))); + if(length < minimum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENOBJ04, "Target object size or length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + minimum + ", " + undefined + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is less than " + minimum))); return true; } public boolean length(JObject target, JUndefined undefined, JInteger maximum) { var length = target.getProperties().size(); - if(length > maximum.getValue()) - return fail(new JsonSchemaException(new ErrorDetail(OLEN05, - "Object " + target.getOutline() + " size or length is outside of range"), - new ExpectedDetail(caller, "length in range [" + undefined + ", " + maximum + "]"), - new ActualDetail(target, "found " + length + " that is greater than " + maximum))); + if(length > maximum.getValue()) return fail(new FunctionValidationException( + new ErrorDetail(LENOBJ05, "Target object size or length is outside of range"), + new ExpectedDetail(caller, "a length in range [" + undefined + ", " + maximum + "]"), + new ActualDetail(target, "length " + length + " of target " + target.getOutline() + + " is greater than " + maximum))); return true; } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/function/CoreFunctions2.java b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions2.java index 8fc0ebb..7482b40 100644 --- a/src/main/java/com/relogiclabs/jschema/function/CoreFunctions2.java +++ b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions2.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.function; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.message.ActualDetail; import com.relogiclabs.jschema.message.ErrorDetail; import com.relogiclabs.jschema.message.ExpectedDetail; @@ -14,178 +14,174 @@ import java.util.Arrays; import static com.relogiclabs.jschema.internal.util.StringHelper.joinWith; -import static com.relogiclabs.jschema.message.ErrorCode.ENUM01; -import static com.relogiclabs.jschema.message.ErrorCode.ENUM02; -import static com.relogiclabs.jschema.message.ErrorCode.MAXI01; -import static com.relogiclabs.jschema.message.ErrorCode.MAXI02; -import static com.relogiclabs.jschema.message.ErrorCode.MAXI03; -import static com.relogiclabs.jschema.message.ErrorCode.MINI01; -import static com.relogiclabs.jschema.message.ErrorCode.MINI02; -import static com.relogiclabs.jschema.message.ErrorCode.MINI03; -import static com.relogiclabs.jschema.message.ErrorCode.NEGI01; -import static com.relogiclabs.jschema.message.ErrorCode.NEGI02; -import static com.relogiclabs.jschema.message.ErrorCode.NEMT01; -import static com.relogiclabs.jschema.message.ErrorCode.NEMT02; -import static com.relogiclabs.jschema.message.ErrorCode.NEMT03; -import static com.relogiclabs.jschema.message.ErrorCode.POSI01; -import static com.relogiclabs.jschema.message.ErrorCode.POSI02; -import static com.relogiclabs.jschema.message.ErrorCode.RANG01; -import static com.relogiclabs.jschema.message.ErrorCode.RANG02; -import static com.relogiclabs.jschema.message.ErrorCode.RANG03; -import static com.relogiclabs.jschema.message.ErrorCode.RANG04; +import static com.relogiclabs.jschema.message.ErrorCode.EMPTCF01; +import static com.relogiclabs.jschema.message.ErrorCode.EMPTCF02; +import static com.relogiclabs.jschema.message.ErrorCode.EMPTCF03; +import static com.relogiclabs.jschema.message.ErrorCode.ENMNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.ENMSTR01; +import static com.relogiclabs.jschema.message.ErrorCode.MAXICF01; +import static com.relogiclabs.jschema.message.ErrorCode.MAXICF02; +import static com.relogiclabs.jschema.message.ErrorCode.MAXICF03; +import static com.relogiclabs.jschema.message.ErrorCode.MINICF01; +import static com.relogiclabs.jschema.message.ErrorCode.MINICF02; +import static com.relogiclabs.jschema.message.ErrorCode.MINICF03; +import static com.relogiclabs.jschema.message.ErrorCode.NEGICF01; +import static com.relogiclabs.jschema.message.ErrorCode.NEGICF02; +import static com.relogiclabs.jschema.message.ErrorCode.POSICF01; +import static com.relogiclabs.jschema.message.ErrorCode.POSICF02; +import static com.relogiclabs.jschema.message.ErrorCode.RNGNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.RNGNUM02; +import static com.relogiclabs.jschema.message.ErrorCode.RNGNUM03; +import static com.relogiclabs.jschema.message.ErrorCode.RNGNUM04; public abstract class CoreFunctions2 extends CoreFunctions1 { // enum is a keyword in Java and _ will be escaped public boolean _enum(JString target, JString... items) { var list = Arrays.asList(items); - if(!list.contains(target)) return fail(new JsonSchemaException( - new ErrorDetail(ENUM01, "String is not in enum list"), - new ExpectedDetail(caller, "string in list " + joinWith(list, ", ", "[", "]")), - new ActualDetail(target, "string " + target + " is not found in list"))); + if(!list.contains(target)) return fail(new FunctionValidationException( + new ErrorDetail(ENMSTR01, "Target string is not in enum listing"), + new ExpectedDetail(caller, "a string in listing " + joinWith(list, ", ", "[", "]")), + new ActualDetail(target, "target " + target + " is not in listing"))); return true; } public boolean _enum(JNumber target, JNumber... items) { var list = Arrays.asList(items); - if(!list.contains(target)) return fail(new JsonSchemaException( - new ErrorDetail(ENUM02, "Number is not in enum list"), - new ExpectedDetail(caller, "number in list " + joinWith(list, ", ", "[", "]")), - new ActualDetail(target, "number " + target + " is not found in list"))); + if(!list.contains(target)) return fail(new FunctionValidationException( + new ErrorDetail(ENMNUM01, "Target number is not in enum listing"), + new ExpectedDetail(caller, "a number in listing " + joinWith(list, ", ", "[", "]")), + new ActualDetail(target, "target " + target + " is not in listing"))); return true; } public boolean minimum(JNumber target, JNumber minimum) { - if(target.compare(minimum) < 0) - return fail(new JsonSchemaException( - new ErrorDetail(MINI01, "Number is less than provided minimum"), - new ExpectedDetail(caller, "a number greater than or equal to " + minimum), - new ActualDetail(target, "number " + target + " is less than " + minimum))); + if(target.compare(minimum) < 0) return fail(new FunctionValidationException( + new ErrorDetail(MINICF01, "Target number must not be less than minimum"), + new ExpectedDetail(caller, "a number greater than or equal to " + minimum), + new ActualDetail(target, "target " + target + " is less than " + minimum))); return true; } public boolean minimum(JNumber target, JNumber minimum, JBoolean exclusive) { var relationTo = exclusive.getValue() ? "greater than " : "greater than or equal to "; - if(target.compare(minimum) < 0) - return fail(new JsonSchemaException( - new ErrorDetail(MINI02, "Number is less than provided minimum"), - new ExpectedDetail(caller, "a number " + relationTo + minimum), - new ActualDetail(target, "number " + target + " is less than " + minimum))); + if(target.compare(minimum) < 0) return fail(new FunctionValidationException( + new ErrorDetail(MINICF02, "Target number must not be less than minimum"), + new ExpectedDetail(caller, "a number " + relationTo + minimum), + new ActualDetail(target, "target " + target + " is less than " + minimum))); if(exclusive.getValue() && target.compare(minimum) == 0) - return fail(new JsonSchemaException( - new ErrorDetail(MINI03, "Number is equal to provided minimum"), + return fail(new FunctionValidationException( + new ErrorDetail(MINICF03, "Target number must be greater than minimum"), new ExpectedDetail(caller, "a number " + relationTo + minimum), - new ActualDetail(target, "number " + target + " is equal to " + minimum))); + new ActualDetail(target, "target " + target + " is equal to " + minimum))); return true; } public boolean maximum(JNumber target, JNumber maximum) { - if(target.compare(maximum) > 0) - return fail(new JsonSchemaException( - new ErrorDetail(MAXI01, "Number is greater than provided maximum"), - new ExpectedDetail(caller, "a number less than or equal " + maximum), - new ActualDetail(target, "number " + target + " is greater than " + maximum))); + if(target.compare(maximum) > 0) return fail(new FunctionValidationException( + new ErrorDetail(MAXICF01, "Target number must not be greater than maximum"), + new ExpectedDetail(caller, "a number less than or equal " + maximum), + new ActualDetail(target, "target " + target + " is greater than " + maximum))); return true; } public boolean maximum(JNumber target, JNumber maximum, JBoolean exclusive) { var relationTo = exclusive.getValue() ? "less than " : "less than or equal to "; - if(target.compare(maximum) > 0) - return fail(new JsonSchemaException( - new ErrorDetail(MAXI02, "Number is greater than provided maximum"), - new ExpectedDetail(caller, "a number " + relationTo + maximum), - new ActualDetail(target, "number " + target + " is greater than " + maximum))); + if(target.compare(maximum) > 0) return fail(new FunctionValidationException( + new ErrorDetail(MAXICF02, "Target number must not be greater than maximum"), + new ExpectedDetail(caller, "a number " + relationTo + maximum), + new ActualDetail(target, "target " + target + " is greater than " + maximum))); if(exclusive.getValue() && target.compare(maximum) == 0) - return fail(new JsonSchemaException( - new ErrorDetail(MAXI03, "Number is equal to provided maximum"), + return fail(new FunctionValidationException( + new ErrorDetail(MAXICF03, "Target number must be less than maximum"), new ExpectedDetail(caller, "a number " + relationTo + maximum), - new ActualDetail(target, "number " + target + " is equal to " + maximum))); + new ActualDetail(target, "target " + target + " is equal to " + maximum))); return true; } public boolean positive(JNumber target) { - if(target.compare(0) <= 0) return fail(new JsonSchemaException( - new ErrorDetail(POSI01, "Number is not positive"), + if(target.compare(0) <= 0) return fail(new FunctionValidationException( + new ErrorDetail(POSICF01, "Target number must be positive"), new ExpectedDetail(caller, "a positive number"), - new ActualDetail(target, "number " + target + " is less than or equal to zero"))); + new ActualDetail(target, "target " + target + " is less than or equal to zero"))); return true; } public boolean negative(JNumber target) { - if(target.compare(0) >= 0) return fail(new JsonSchemaException( - new ErrorDetail(NEGI01, "Number is not negative"), + if(target.compare(0) >= 0) return fail(new FunctionValidationException( + new ErrorDetail(NEGICF01, "Target number must be negative"), new ExpectedDetail(caller, "a negative number"), - new ActualDetail(target, "number " + target + " is greater than or equal to zero"))); + new ActualDetail(target, "target " + target + " is greater than or equal to zero"))); return true; } public boolean positive(JNumber target, JNumber reference) { - if(target.compare(reference) < 0) return fail(new JsonSchemaException( - new ErrorDetail(POSI02, "Number is not positive from reference"), + if(target.compare(reference) < 0) return fail(new FunctionValidationException( + new ErrorDetail(POSICF02, "Target number must be positive from reference"), new ExpectedDetail(caller, "a positive number from " + reference), - new ActualDetail(target, "number " + target + " is less than reference"))); + new ActualDetail(target, "target " + target + " is less than reference"))); return true; } public boolean negative(JNumber target, JNumber reference) { - if(target.compare(reference) > 0) return fail(new JsonSchemaException( - new ErrorDetail(NEGI02, "Number is not negative from reference"), + if(target.compare(reference) > 0) return fail(new FunctionValidationException( + new ErrorDetail(NEGICF02, "Target number must be negative from reference"), new ExpectedDetail(caller, "a negative number from " + reference), - new ActualDetail(target, "number " + target + " is greater than reference"))); + new ActualDetail(target, "target " + target + " is greater than reference"))); return true; } public boolean range(JNumber target, JNumber minimum, JNumber maximum) { - if(target.compare(minimum) < 0) return fail(new JsonSchemaException( - new ErrorDetail(RANG01, "Number is outside of range"), - new ExpectedDetail(caller, "number in range [" + minimum + ", " + maximum + "]"), - new ActualDetail(target, "number " + target + " is less than " + minimum))); - if(target.compare(maximum) > 0) return fail(new JsonSchemaException( - new ErrorDetail(RANG02, "Number is outside of range"), - new ExpectedDetail(caller, "number in range [" + minimum + ", " + maximum + "]"), - new ActualDetail(target, "number " + target + " is greater than " + maximum))); + if(target.compare(minimum) < 0) return fail(new FunctionValidationException( + new ErrorDetail(RNGNUM01, "Target number is outside of range"), + new ExpectedDetail(caller, "a number in range [" + minimum + ", " + maximum + "]"), + new ActualDetail(target, "target " + target + " is less than " + minimum))); + if(target.compare(maximum) > 0) return fail(new FunctionValidationException( + new ErrorDetail(RNGNUM02, "Target number is outside of range"), + new ExpectedDetail(caller, "a number in range [" + minimum + ", " + maximum + "]"), + new ActualDetail(target, "target " + target + " is greater than " + maximum))); return true; } public boolean range(JNumber target, JNumber minimum, JUndefined undefined) { - if(target.compare(minimum) < 0) return fail(new JsonSchemaException( - new ErrorDetail(RANG03, "Number is outside of range"), - new ExpectedDetail(caller, "number in range [" + minimum + ", " + undefined + "]"), - new ActualDetail(target, "number " + target + " is less than " + minimum))); + if(target.compare(minimum) < 0) return fail(new FunctionValidationException( + new ErrorDetail(RNGNUM03, "Target number is outside of range"), + new ExpectedDetail(caller, "a number in range [" + minimum + ", " + undefined + "]"), + new ActualDetail(target, "target " + target + " is less than " + minimum))); return true; } public boolean range(JNumber target, JUndefined undefined, JNumber maximum) { - if(target.compare(maximum) > 0) return fail(new JsonSchemaException( - new ErrorDetail(RANG04, "Number is outside of range"), - new ExpectedDetail(caller, "number in range [" + undefined + ", " + maximum + "]"), - new ActualDetail(target, "number " + target + " is greater than " + maximum))); + if(target.compare(maximum) > 0) return fail(new FunctionValidationException( + new ErrorDetail(RNGNUM04, "Target number is outside of range"), + new ExpectedDetail(caller, "a number in range [" + undefined + ", " + maximum + "]"), + new ActualDetail(target, "target " + target + " is greater than " + maximum))); return true; } public boolean nonempty(JString target) { var length = target.length(); - if(length <= 0) return fail(new JsonSchemaException( - new ErrorDetail(NEMT01, "String is empty"), - new ExpectedDetail(caller, "Non empty string"), - new ActualDetail(target, "found empty string"))); + if(length <= 0) return fail(new FunctionValidationException( + new ErrorDetail(EMPTCF01, "Target string must not be empty"), + new ExpectedDetail(caller, "a non-empty string"), + new ActualDetail(target, "found empty target string"))); return true; } public boolean nonempty(JArray target) { var length = target.getElements().size(); - if(length <= 0) return fail(new JsonSchemaException( - new ErrorDetail(NEMT02, "Array is empty"), - new ExpectedDetail(caller, "Non empty array"), - new ActualDetail(target, "found empty array"))); + if(length <= 0) return fail(new FunctionValidationException( + new ErrorDetail(EMPTCF02, "Target array must not be empty"), + new ExpectedDetail(caller, "a non-empty array"), + new ActualDetail(target, "found empty target array"))); return true; } public boolean nonempty(JObject target) { var length = target.getProperties().size(); - if(length <= 0) return fail(new JsonSchemaException( - new ErrorDetail(NEMT03, "Object is empty"), - new ExpectedDetail(caller, "Non empty object"), - new ActualDetail(target, "found empty object"))); + if(length <= 0) return fail(new FunctionValidationException( + new ErrorDetail(EMPTCF03, "Target object must not be empty"), + new ExpectedDetail(caller, "a non-empty object"), + new ActualDetail(target, "found empty target object"))); return true; } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/function/CoreFunctions3.java b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions3.java index eae35e2..5c05d9e 100644 --- a/src/main/java/com/relogiclabs/jschema/function/CoreFunctions3.java +++ b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions3.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.function; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.message.ActualDetail; import com.relogiclabs.jschema.message.ErrorDetail; import com.relogiclabs.jschema.message.ExpectedDetail; @@ -17,16 +17,16 @@ import static com.relogiclabs.jschema.internal.util.CollectionHelper.containsValues; import static com.relogiclabs.jschema.internal.util.StreamHelper.count; import static com.relogiclabs.jschema.internal.util.StringHelper.quote; -import static com.relogiclabs.jschema.message.ErrorCode.ELEM01; -import static com.relogiclabs.jschema.message.ErrorCode.EMAL01; -import static com.relogiclabs.jschema.message.ErrorCode.KEYS01; -import static com.relogiclabs.jschema.message.ErrorCode.PHON01; -import static com.relogiclabs.jschema.message.ErrorCode.REGX01; -import static com.relogiclabs.jschema.message.ErrorCode.URLA01; -import static com.relogiclabs.jschema.message.ErrorCode.URLA02; -import static com.relogiclabs.jschema.message.ErrorCode.URLA03; -import static com.relogiclabs.jschema.message.ErrorCode.URLA04; -import static com.relogiclabs.jschema.message.ErrorCode.VALU01; +import static com.relogiclabs.jschema.message.ErrorCode.ELEMCF01; +import static com.relogiclabs.jschema.message.ErrorCode.EMALCF01; +import static com.relogiclabs.jschema.message.ErrorCode.KEYFND01; +import static com.relogiclabs.jschema.message.ErrorCode.PHONCF01; +import static com.relogiclabs.jschema.message.ErrorCode.REGXCF01; +import static com.relogiclabs.jschema.message.ErrorCode.URLADR01; +import static com.relogiclabs.jschema.message.ErrorCode.URLADR02; +import static com.relogiclabs.jschema.message.ErrorCode.URLSCM01; +import static com.relogiclabs.jschema.message.ErrorCode.URLSCM02; +import static com.relogiclabs.jschema.message.ErrorCode.VALFND01; public abstract class CoreFunctions3 extends CoreFunctions2 { private static final String URI_SCHEME_HTTPS = "https"; @@ -44,10 +44,10 @@ public boolean elements(JArray target, JNode... items) { } private boolean failOnElement(JArray target, JNode node) { - return fail(new JsonSchemaException( - new ErrorDetail(ELEM01, "Value is not an element of array"), - new ExpectedDetail(caller, "array with value " + node), - new ActualDetail(target, "not found in " + target.getOutline()))); + return fail(new FunctionValidationException( + new ErrorDetail(ELEMCF01, "Element not found in target array"), + new ExpectedDetail(caller, "an array with element " + node), + new ActualDetail(target, "not found in target " + target.getOutline()))); } public boolean keys(JObject target, JString... items) { @@ -56,10 +56,10 @@ public boolean keys(JObject target, JString... items) { } private boolean failOnKey(JObject target, String string) { - return fail(new JsonSchemaException( - new ErrorDetail(KEYS01, "Object does not contain the key"), - new ExpectedDetail(caller, "object with key " + quote(string)), - new ActualDetail(target, "does not contain in " + target.getOutline()))); + return fail(new FunctionValidationException( + new ErrorDetail(KEYFND01, "Key not found in target object"), + new ExpectedDetail(caller, "an object with key " + quote(string)), + new ActualDetail(target, "not found in target " + target.getOutline()))); } public boolean values(JObject target, JNode... items) { @@ -68,82 +68,79 @@ public boolean values(JObject target, JNode... items) { } private boolean failOnValue(JObject target, JNode node) { - return fail(new JsonSchemaException( - new ErrorDetail(VALU01, "Object does not contain the value"), - new ExpectedDetail(caller, "object with value " + node), - new ActualDetail(target, "does not contain in " + target.getOutline()))); + return fail(new FunctionValidationException( + new ErrorDetail(VALFND01, "Value not found in target object"), + new ExpectedDetail(caller, "an object with value " + node), + new ActualDetail(target, "not found in target " + target.getOutline()))); } public boolean regex(JString target, JString pattern) { if(!Pattern.matches(pattern.getValue(), target.getValue())) - return fail(new JsonSchemaException( - new ErrorDetail(REGX01, "Regex pattern does not match"), - new ExpectedDetail(caller, "string of pattern " + pattern.getOutline()), - new ActualDetail(target, "found " + target.getOutline() - + " that mismatches with pattern"))); + return fail(new FunctionValidationException( + new ErrorDetail(REGXCF01, "Target mismatched with regex pattern"), + new ExpectedDetail(caller, "a string following pattern " + pattern), + new ActualDetail(target, "mismatched for target " + target.getOutline()))); return true; } public boolean email(JString target) { // Based on SMTP protocol RFC 5322 if(!EMAIL_REGEX.matcher(target.getValue()).matches()) - return fail(new JsonSchemaException( - new ErrorDetail(EMAL01, "Invalid email address"), + return fail(new FunctionValidationException( + new ErrorDetail(EMALCF01, "Invalid target email address"), new ExpectedDetail(caller, "a valid email address"), - new ActualDetail(target, "found " + target + " that is invalid"))); + new ActualDetail(target, "found malformed target " + target))); return true; } public boolean url(JString target) { - boolean result; URI uri; try { uri = new URI(target.getValue()); - result = switch(uri.getScheme()) { - case URI_SCHEME_HTTP, URI_SCHEME_HTTPS -> true; - default -> false; - }; } catch (Exception e) { - return fail(new JsonSchemaException( - new ErrorDetail(URLA01, "Invalid url address"), - new ExpectedDetail(caller, "a valid url address"), - new ActualDetail(target, "found " + target + " that is invalid"))); + return fail(new FunctionValidationException( + new ErrorDetail(URLADR01, "Invalid target URL address"), + new ExpectedDetail(caller, "a valid URL address"), + new ActualDetail(target, "found malformed target " + target))); } - if(!result) return fail(new JsonSchemaException( - new ErrorDetail(URLA02, "Invalid url address scheme"), - new ExpectedDetail(caller, "HTTP or HTTPS scheme"), - new ActualDetail(target, "found " + quote(uri.getScheme()) + " from " - + target + " that has invalid scheme"))); + var result = switch(uri.getScheme()) { + case URI_SCHEME_HTTP, URI_SCHEME_HTTPS -> true; + default -> false; + }; + if(!result) return fail(new FunctionValidationException( + new ErrorDetail(URLADR02, "Mismatched target URL address scheme"), + new ExpectedDetail(caller, "A URL with HTTP or HTTPS scheme"), + new ActualDetail(target, "found scheme " + quote(uri.getScheme()) + + " for target " + target))); return true; } public boolean url(JString target, JString scheme) { - boolean result; URI uri; try { uri = new URI(target.getValue()); } catch (Exception e) { - return fail(new JsonSchemaException( - new ErrorDetail(URLA03, "Invalid url address"), - new ExpectedDetail(caller, "a valid url address"), - new ActualDetail(target, "found " + target + " that is invalid"))); + return fail(new FunctionValidationException( + new ErrorDetail(URLSCM01, "Invalid target URL address"), + new ExpectedDetail(caller, "a valid URL address"), + new ActualDetail(target, "found malformed target " + target))); } - result = scheme.getValue().equals(uri.getScheme()); - if(!result) return fail(new JsonSchemaException( - new ErrorDetail(URLA04, "Mismatch url address scheme"), - new ExpectedDetail(caller, "scheme " + scheme + " for url address"), - new ActualDetail(target, "found " + quote(uri.getScheme()) + " from " - + target + " that does not matched"))); + var result = scheme.getValue().equals(uri.getScheme()); + if(!result) return fail(new FunctionValidationException( + new ErrorDetail(URLSCM02, "Mismatched target URL address scheme"), + new ExpectedDetail(caller, "A URL with scheme " + scheme), + new ActualDetail(target, "found scheme " + quote(uri.getScheme()) + + " for target " + target))); return true; } public boolean phone(JString target) { - // Based on ITU-T E.163 and E.164 (extended) + // Based on ITU-T E.163 and E.164 (including widely used) if(!PHONE_REGEX.matcher(target.getValue()).matches()) - return fail(new JsonSchemaException( - new ErrorDetail(PHON01, "Invalid phone number format"), + return fail(new FunctionValidationException( + new ErrorDetail(PHONCF01, "Invalid target phone number format"), new ExpectedDetail(caller, "a valid phone number"), - new ActualDetail(target, "found " + target + " that is invalid"))); + new ActualDetail(target, "found malformed target " + target))); return true; } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/function/CoreFunctions4.java b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions4.java index eeb098d..98bb842 100644 --- a/src/main/java/com/relogiclabs/jschema/function/CoreFunctions4.java +++ b/src/main/java/com/relogiclabs/jschema/function/CoreFunctions4.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.function; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.internal.function.DateTimeAgent; import com.relogiclabs.jschema.internal.time.DateTimeParser; import com.relogiclabs.jschema.message.ActualDetail; @@ -11,25 +11,25 @@ import com.relogiclabs.jschema.node.JUndefined; import com.relogiclabs.jschema.time.DateTimeType; -import static com.relogiclabs.jschema.message.ErrorCode.AFTR01; -import static com.relogiclabs.jschema.message.ErrorCode.AFTR02; -import static com.relogiclabs.jschema.message.ErrorCode.BFOR01; -import static com.relogiclabs.jschema.message.ErrorCode.BFOR02; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG01; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG02; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG03; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG04; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG05; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG06; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG07; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG08; -import static com.relogiclabs.jschema.message.ErrorCode.ENDE01; -import static com.relogiclabs.jschema.message.ErrorCode.ENDE02; -import static com.relogiclabs.jschema.message.ErrorCode.STRT01; -import static com.relogiclabs.jschema.message.ErrorCode.STRT02; +import static com.relogiclabs.jschema.message.ErrorCode.AFTRDT01; +import static com.relogiclabs.jschema.message.ErrorCode.AFTRDT02; +import static com.relogiclabs.jschema.message.ErrorCode.BFORDT01; +import static com.relogiclabs.jschema.message.ErrorCode.BFORDT02; +import static com.relogiclabs.jschema.message.ErrorCode.ENDFDT01; +import static com.relogiclabs.jschema.message.ErrorCode.ENDFDT02; +import static com.relogiclabs.jschema.message.ErrorCode.RNDEND01; +import static com.relogiclabs.jschema.message.ErrorCode.RNDEND02; +import static com.relogiclabs.jschema.message.ErrorCode.RNDEND03; +import static com.relogiclabs.jschema.message.ErrorCode.RNDEND04; +import static com.relogiclabs.jschema.message.ErrorCode.RNDSTA01; +import static com.relogiclabs.jschema.message.ErrorCode.RNDSTA02; +import static com.relogiclabs.jschema.message.ErrorCode.RNDSTA03; +import static com.relogiclabs.jschema.message.ErrorCode.RNDSTA04; +import static com.relogiclabs.jschema.message.ErrorCode.STRTDT01; +import static com.relogiclabs.jschema.message.ErrorCode.STRTDT02; import static com.relogiclabs.jschema.time.DateTimeType.DATE_TYPE; -public final class CoreFunctions4 extends CoreFunctions3 { +public abstract class CoreFunctions4 extends CoreFunctions3 { public boolean date(JString target, JString pattern) { return dateTime(target, pattern, DATE_TYPE); } @@ -42,29 +42,29 @@ private boolean dateTime(JString target, JString pattern, DateTimeType type) { return new DateTimeAgent(pattern.getValue(), type).parse(caller, target) != null; } - public boolean before(JDateTime target, JString reference) { - var dateTimeNode = getDateTime(target.getDateTimeParser(), reference); - if(dateTimeNode == null) return false; - if(target.getDateTime().compare(dateTimeNode.getDateTime()) < 0) return true; - var dateTime = target.getDateTime().getType(); - var code = dateTime == DATE_TYPE ? BFOR01 : BFOR02; - return fail(new JsonSchemaException( - new ErrorDetail(code, dateTime + " is not earlier than specified"), - new ExpectedDetail(caller, "a " + dateTime + " before " + reference), - new ActualDetail(target, "found " + target + " which is not inside limit") + public boolean before(JDateTime target, JString dateTime) { + var refDateTime = getDateTime(target.getDateTimeParser(), dateTime); + if(refDateTime == null) return false; + if(target.getDateTime().compare(refDateTime.getDateTime()) < 0) return true; + var type = target.getDateTime().getType(); + var code = type == DATE_TYPE ? BFORDT01 : BFORDT02; + return fail(new FunctionValidationException( + new ErrorDetail(code, "Target is not earlier than specified " + type), + new ExpectedDetail(caller, "a " + type + " before " + dateTime), + new ActualDetail(target, "target " + target + " is not earlier") )); } - public boolean after(JDateTime target, JString reference) { - var dateTimeNode = getDateTime(target.getDateTimeParser(), reference); - if(dateTimeNode == null) return false; - if(target.getDateTime().compare(dateTimeNode.getDateTime()) > 0) return true; - var dateTime = target.getDateTime().getType(); - var code = dateTime == DATE_TYPE ? AFTR01 : AFTR02; - return fail(new JsonSchemaException( - new ErrorDetail(code, dateTime + " is not later than specified"), - new ExpectedDetail(caller, "a " + dateTime + " after " + reference), - new ActualDetail(target, "found " + target + " which is not inside limit") + public boolean after(JDateTime target, JString dateTime) { + var refDateTime = getDateTime(target.getDateTimeParser(), dateTime); + if(refDateTime == null) return false; + if(target.getDateTime().compare(refDateTime.getDateTime()) > 0) return true; + var type = target.getDateTime().getType(); + var code = type == DATE_TYPE ? AFTRDT01 : AFTRDT02; + return fail(new FunctionValidationException( + new ErrorDetail(code, "Target is not later than specified " + type), + new ExpectedDetail(caller, "a " + type + " after " + dateTime), + new ActualDetail(target, "target " + target + " is not later") )); } @@ -74,9 +74,9 @@ public boolean range(JDateTime target, JString start, JString end) { var rEnd = getDateTime(target.getDateTimeParser(), end); if(rEnd == null) return false; if(target.getDateTime().compare(rStart.getDateTime()) < 0) - return failOnStartDate(target, rStart, getErrorCode(target, DRNG01, DRNG02)); + return failOnStartDate(target, rStart, getErrorCode(target, RNDSTA01, RNDSTA02)); if(target.getDateTime().compare(rEnd.getDateTime()) > 0) - return failOnEndDate(target, rEnd, getErrorCode(target, DRNG03, DRNG04)); + return failOnEndDate(target, rEnd, getErrorCode(target, RNDEND01, RNDEND02)); return true; } @@ -86,19 +86,19 @@ private static String getErrorCode(JDateTime target, String date, String time) { private boolean failOnStartDate(JDateTime target, JDateTime start, String code) { var dateTime = target.getDateTime().getType(); - return fail(new JsonSchemaException( - new ErrorDetail(code, dateTime + " is earlier than start " + dateTime), + return fail(new FunctionValidationException( + new ErrorDetail(code, "Target is earlier than start " + dateTime), new ExpectedDetail(caller, "a " + dateTime + " from or after " + start), - new ActualDetail(target, "found " + target + " which is before start " + dateTime) + new ActualDetail(target, "target " + target + " is before start") )); } private boolean failOnEndDate(JDateTime target, JDateTime end, String code) { var dateTime = target.getDateTime().getType(); - return fail(new JsonSchemaException( - new ErrorDetail(code, dateTime + " is later than end " + dateTime), + return fail(new FunctionValidationException( + new ErrorDetail(code, "Target is later than end " + dateTime), new ExpectedDetail(caller, "a " + dateTime + " until or before " + end), - new ActualDetail(target, "found " + target + " which is after end " + dateTime) + new ActualDetail(target, "target " + target + " is after end") )); } @@ -106,41 +106,41 @@ public boolean range(JDateTime target, JUndefined start, JString end) { var rEnd = getDateTime(target.getDateTimeParser(), end); if(rEnd == null) return false; if (target.getDateTime().compare(rEnd.getDateTime()) <= 0) return true; - return failOnEndDate(target, rEnd, getErrorCode(target, DRNG05, DRNG06)); + return failOnEndDate(target, rEnd, getErrorCode(target, RNDEND03, RNDEND04)); } public boolean range(JDateTime target, JString start, JUndefined end) { var rStart = getDateTime(target.getDateTimeParser(), start); if(rStart == null) return false; if (target.getDateTime().compare(rStart.getDateTime()) >= 0) return true; - return failOnStartDate(target, rStart, getErrorCode(target, DRNG07, DRNG08)); + return failOnStartDate(target, rStart, getErrorCode(target, RNDSTA03, RNDSTA04)); } - public boolean start(JDateTime target, JString reference) { - var dateTimeNode = getDateTime(target.getDateTimeParser(), reference); - if(dateTimeNode == null) return false; - if(target.getDateTime().compare(dateTimeNode.getDateTime()) < 0) { - var dateTime = target.getDateTime().getType(); - var code = dateTime == DATE_TYPE ? STRT01 : STRT02; - return fail(new JsonSchemaException( - new ErrorDetail(code, dateTime + " is earlier than specified"), - new ExpectedDetail(caller, "a " + dateTime + " from or after " + dateTimeNode), - new ActualDetail(target, "found " + target + " which is before limit") + public boolean start(JDateTime target, JString dateTime) { + var refDateTime = getDateTime(target.getDateTimeParser(), dateTime); + if(refDateTime == null) return false; + if(target.getDateTime().compare(refDateTime.getDateTime()) < 0) { + var type = target.getDateTime().getType(); + var code = type == DATE_TYPE ? STRTDT01 : STRTDT02; + return fail(new FunctionValidationException( + new ErrorDetail(code, "Target is earlier than start " + type), + new ExpectedDetail(caller, "a " + type + " from or after " + dateTime), + new ActualDetail(target, "target " + target + " is before start") )); } return true; } - public boolean end(JDateTime target, JString reference) { - var dateTimeNode = getDateTime(target.getDateTimeParser(), reference); - if(dateTimeNode == null) return false; - if(target.getDateTime().compare(dateTimeNode.getDateTime()) > 0) { - var dateTime = target.getDateTime().getType(); - var code = dateTime == DATE_TYPE ? ENDE01 : ENDE02; - return fail(new JsonSchemaException( - new ErrorDetail(code, dateTime + " is later than specified"), - new ExpectedDetail(caller, "a " + dateTime + " until or before " + dateTimeNode), - new ActualDetail(target, "found " + target + " which is after limit") + public boolean end(JDateTime target, JString dateTime) { + var refDateTime = getDateTime(target.getDateTimeParser(), dateTime); + if(refDateTime == null) return false; + if(target.getDateTime().compare(refDateTime.getDateTime()) > 0) { + var type = target.getDateTime().getType(); + var code = type == DATE_TYPE ? ENDFDT01 : ENDFDT02; + return fail(new FunctionValidationException( + new ErrorDetail(code, "Target is later than end " + type), + new ExpectedDetail(caller, "a " + type + " before or until " + dateTime), + new ActualDetail(target, "target " + target + " is after end") )); } return true; diff --git a/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParser.java b/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParser.java index c243d2a..f9b49b4 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParser.java +++ b/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParser.java @@ -3128,6 +3128,16 @@ public T accept(ParseTreeVisitor visitor) { } } @SuppressWarnings("CheckReturnValue") + public static class VariableExpressionContext extends ExpressionContext { + public TerminalNode G_IDENTIFIER() { return getToken(SchemaParser.G_IDENTIFIER, 0); } + public VariableExpressionContext(ExpressionContext ctx) { copyFrom(ctx); } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof SchemaParserVisitor ) return ((SchemaParserVisitor)visitor).visitVariableExpression(this); + else return visitor.visitChildren(this); + } + } + @SuppressWarnings("CheckReturnValue") public static class ThrowExpressionContext extends ExpressionContext { public TerminalNode G_THROW() { return getToken(SchemaParser.G_THROW, 0); } public TerminalNode G_LPAREN() { return getToken(SchemaParser.G_LPAREN, 0); } @@ -3147,16 +3157,6 @@ public T accept(ParseTreeVisitor visitor) { } } @SuppressWarnings("CheckReturnValue") - public static class IdentifierExpressionContext extends ExpressionContext { - public TerminalNode G_IDENTIFIER() { return getToken(SchemaParser.G_IDENTIFIER, 0); } - public IdentifierExpressionContext(ExpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SchemaParserVisitor ) return ((SchemaParserVisitor)visitor).visitIdentifierExpression(this); - else return visitor.visitChildren(this); - } - } - @SuppressWarnings("CheckReturnValue") public static class InvokeFunctionExpressionContext extends ExpressionContext { public TerminalNode G_IDENTIFIER() { return getToken(SchemaParser.G_IDENTIFIER, 0); } public TerminalNode G_LPAREN() { return getToken(SchemaParser.G_LPAREN, 0); } @@ -3493,7 +3493,7 @@ private ExpressionContext expression(int _p) throws RecognitionException { break; case 14: { - _localctx = new IdentifierExpressionContext(_localctx); + _localctx = new VariableExpressionContext(_localctx); _ctx = _localctx; _prevctx = _localctx; setState(470); diff --git a/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParserBaseVisitor.java b/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParserBaseVisitor.java index 911f31b..6ac045a 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParserBaseVisitor.java +++ b/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParserBaseVisitor.java @@ -458,14 +458,14 @@ public class SchemaParserBaseVisitor extends AbstractParseTreeVisitor impl *

The default implementation returns the result of calling * {@link #visitChildren} on {@code ctx}.

*/ - @Override public T visitThrowExpression(SchemaParser.ThrowExpressionContext ctx) { return visitChildren(ctx); } + @Override public T visitVariableExpression(SchemaParser.VariableExpressionContext ctx) { return visitChildren(ctx); } /** * {@inheritDoc} * *

The default implementation returns the result of calling * {@link #visitChildren} on {@code ctx}.

*/ - @Override public T visitIdentifierExpression(SchemaParser.IdentifierExpressionContext ctx) { return visitChildren(ctx); } + @Override public T visitThrowExpression(SchemaParser.ThrowExpressionContext ctx) { return visitChildren(ctx); } /** * {@inheritDoc} * diff --git a/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParserVisitor.java b/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParserVisitor.java index 1d12960..eb9349f 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParserVisitor.java +++ b/src/main/java/com/relogiclabs/jschema/internal/antlr/SchemaParserVisitor.java @@ -419,19 +419,19 @@ public interface SchemaParserVisitor extends ParseTreeVisitor { */ T visitLogicalNotExpression(SchemaParser.LogicalNotExpressionContext ctx); /** - * Visit a parse tree produced by the {@code ThrowExpression} + * Visit a parse tree produced by the {@code VariableExpression} * labeled alternative in {@link SchemaParser#expression}. * @param ctx the parse tree * @return the visitor result */ - T visitThrowExpression(SchemaParser.ThrowExpressionContext ctx); + T visitVariableExpression(SchemaParser.VariableExpressionContext ctx); /** - * Visit a parse tree produced by the {@code IdentifierExpression} + * Visit a parse tree produced by the {@code ThrowExpression} * labeled alternative in {@link SchemaParser#expression}. * @param ctx the parse tree * @return the visitor result */ - T visitIdentifierExpression(SchemaParser.IdentifierExpressionContext ctx); + T visitThrowExpression(SchemaParser.ThrowExpressionContext ctx); /** * Visit a parse tree produced by the {@code InvokeFunctionExpression} * labeled alternative in {@link SchemaParser#expression}. diff --git a/src/main/java/com/relogiclabs/jschema/internal/function/DateTimeAgent.java b/src/main/java/com/relogiclabs/jschema/internal/function/DateTimeAgent.java index 5c24941..c0b1cc2 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/function/DateTimeAgent.java +++ b/src/main/java/com/relogiclabs/jschema/internal/function/DateTimeAgent.java @@ -1,8 +1,8 @@ package com.relogiclabs.jschema.internal.function; import com.relogiclabs.jschema.exception.DateTimeLexerException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.exception.InvalidDateTimeException; -import com.relogiclabs.jschema.exception.JsonSchemaException; import com.relogiclabs.jschema.internal.time.DateTimeParser; import com.relogiclabs.jschema.message.ActualDetail; import com.relogiclabs.jschema.message.ErrorDetail; @@ -12,6 +12,8 @@ import com.relogiclabs.jschema.time.DateTimeType; import com.relogiclabs.jschema.time.JsonDateTime; +import static com.relogiclabs.jschema.internal.util.StringHelper.quote; + public class DateTimeAgent { private final String pattern; private final DateTimeType type; @@ -33,17 +35,16 @@ public JsonDateTime parse(JFunction caller, JString dateTime) { if(parser == null) parser = new DateTimeParser(pattern, type); return parser.parse(dateTime.getValue()); } catch(DateTimeLexerException ex) { - fail(caller, new JsonSchemaException( - new ErrorDetail(ex.getCode(), ex.getMessage()), - new ExpectedDetail(caller, "a valid " + type + " pattern"), - new ActualDetail(dateTime, "found " + pattern + " that is invalid"), - ex)); + fail(caller, new FunctionValidationException( + new ErrorDetail(ex.getCode(), ex.getMessage()), + new ExpectedDetail(caller, "a valid " + type + " pattern"), + new ActualDetail(dateTime, "found " + quote(pattern) + " that is invalid"), ex)); } catch(InvalidDateTimeException ex) { - fail(caller, new JsonSchemaException( - new ErrorDetail(ex.getCode(), ex.getMessage()), - new ExpectedDetail(caller, "a valid " + type + " formatted as " + pattern), - new ActualDetail(dateTime, "found " + dateTime + " that is invalid or malformatted"), - ex)); + fail(caller, new FunctionValidationException( + new ErrorDetail(ex.getCode(), ex.getMessage()), + new ExpectedDetail(caller, "a valid " + type + " formatted as " + quote(pattern)), + new ActualDetail(dateTime, "target " + dateTime + " is invalid or malformed"), + ex)); } return null; } diff --git a/src/main/java/com/relogiclabs/jschema/internal/function/FunctionLoader.java b/src/main/java/com/relogiclabs/jschema/internal/function/FunctionLoader.java index 877ed9b..5b9d77b 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/function/FunctionLoader.java +++ b/src/main/java/com/relogiclabs/jschema/internal/function/FunctionLoader.java @@ -1,7 +1,6 @@ package com.relogiclabs.jschema.internal.function; import com.relogiclabs.jschema.exception.ClassInstantiationException; -import com.relogiclabs.jschema.exception.CommonException; import com.relogiclabs.jschema.exception.InvalidFunctionException; import com.relogiclabs.jschema.function.FunctionProvider; import com.relogiclabs.jschema.function.FutureFunction; @@ -13,12 +12,12 @@ import java.lang.reflect.Parameter; import static com.relogiclabs.jschema.internal.tree.NativeFunction.getSignature; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC01; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC02; -import static com.relogiclabs.jschema.message.ErrorCode.INST01; -import static com.relogiclabs.jschema.message.ErrorCode.INST02; -import static com.relogiclabs.jschema.message.ErrorCode.INST03; -import static com.relogiclabs.jschema.message.ErrorCode.INST04; +import static com.relogiclabs.jschema.message.ErrorCode.FNCSIG01; +import static com.relogiclabs.jschema.message.ErrorCode.FNCSIG02; +import static com.relogiclabs.jschema.message.ErrorCode.INSTCR01; +import static com.relogiclabs.jschema.message.ErrorCode.INSTCR02; +import static com.relogiclabs.jschema.message.ErrorCode.INSTCR03; +import static com.relogiclabs.jschema.message.ErrorCode.INSTCR04; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; public final class FunctionLoader { @@ -50,13 +49,13 @@ private static FunctionProvider createInstance(Class var constructor = type.getDeclaredConstructor(); return constructor.newInstance(); } catch (NoSuchMethodException e) { - throw failOnCreateInstance(INST01, e, type, context); + throw failOnCreateInstance(INSTCR01, e, type, context); } catch (InstantiationException e) { - throw failOnCreateInstance(INST02, e, type, context); + throw failOnCreateInstance(INSTCR02, e, type, context); } catch (InvocationTargetException e) { - throw failOnCreateInstance(INST03, e, type, context); + throw failOnCreateInstance(INSTCR03, e, type, context); } catch (IllegalAccessException e) { - throw failOnCreateInstance(INST04, e, type, context); + throw failOnCreateInstance(INSTCR04, e, type, context); } } @@ -67,19 +66,19 @@ private static boolean isValidReturnType(Class type) { return false; } - private static CommonException failOnCreateInstance(String code, Exception ex, + private static RuntimeException failOnCreateInstance(String code, Exception ex, Class type, Context context) { return new ClassInstantiationException(formatForSchema(code, - "Fail to create instance of " + type.getName(), context), ex); + "Fail to create an instance of class " + type.getName(), context), ex); } private static InvalidFunctionException failOnReturnType(Method method, Context context) { - return new InvalidFunctionException(formatForSchema(FUNC01, + return new InvalidFunctionException(formatForSchema(FNCSIG01, "Function [" + getSignature(method) + "] requires valid return type", context)); } private static InvalidFunctionException failOnTargetParameter(Method method, Context context) { - return new InvalidFunctionException(formatForSchema(FUNC02, + return new InvalidFunctionException(formatForSchema(FNCSIG02, "Function [" + getSignature(method) + "] requires valid target parameter", context)); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/grammar/SchemaParser.g4 b/src/main/java/com/relogiclabs/jschema/internal/grammar/SchemaParser.g4 index 3e3d0a7..49c1807 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/grammar/SchemaParser.g4 +++ b/src/main/java/com/relogiclabs/jschema/internal/grammar/SchemaParser.g4 @@ -208,7 +208,7 @@ expression | G_DIV_ASSIGN | G_MOD_ASSIGN ) expression # AssignmentAugExpression | G_TARGET # TargetExpression | G_CALLER # CallerExpression - | G_IDENTIFIER # IdentifierExpression + | G_IDENTIFIER # VariableExpression | literal # LiteralExpression | G_LPAREN expression G_RPAREN # ParenthesizedExpression | G_TRYOF G_LPAREN expression G_RPAREN # TryofExpression diff --git a/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeContext.java b/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeContext.java index 9aebf61..14b349d 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeContext.java +++ b/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeContext.java @@ -5,30 +5,29 @@ import com.relogiclabs.jschema.time.JsonDateTime; import com.relogiclabs.jschema.time.JsonUtcOffset; import lombok.Getter; -import lombok.RequiredArgsConstructor; import java.time.LocalDate; import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import static com.relogiclabs.jschema.message.ErrorCode.DCNF01; -import static com.relogiclabs.jschema.message.ErrorCode.DDAY03; -import static com.relogiclabs.jschema.message.ErrorCode.DDAY04; -import static com.relogiclabs.jschema.message.ErrorCode.DERA02; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR03; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR04; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR05; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR06; -import static com.relogiclabs.jschema.message.ErrorCode.DINV01; -import static com.relogiclabs.jschema.message.ErrorCode.DMIN03; -import static com.relogiclabs.jschema.message.ErrorCode.DMON05; -import static com.relogiclabs.jschema.message.ErrorCode.DSEC03; -import static com.relogiclabs.jschema.message.ErrorCode.DTAP02; -import static com.relogiclabs.jschema.message.ErrorCode.DUTC04; -import static com.relogiclabs.jschema.message.ErrorCode.DUTC05; -import static com.relogiclabs.jschema.message.ErrorCode.DWKD03; -import static com.relogiclabs.jschema.message.ErrorCode.DYAR03; +import static com.relogiclabs.jschema.message.ErrorCode.AMPMVD01; +import static com.relogiclabs.jschema.message.ErrorCode.CNFLDT01; +import static com.relogiclabs.jschema.message.ErrorCode.DAYVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.DAYVDF02; +import static com.relogiclabs.jschema.message.ErrorCode.ERAVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.HURVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.HURVDF02; +import static com.relogiclabs.jschema.message.ErrorCode.HURVDF03; +import static com.relogiclabs.jschema.message.ErrorCode.HURVDF04; +import static com.relogiclabs.jschema.message.ErrorCode.INVLDT02; +import static com.relogiclabs.jschema.message.ErrorCode.MNTVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.MONVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.SECVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.UTCVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.UTCVDF02; +import static com.relogiclabs.jschema.message.ErrorCode.WEKVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.YARVDF01; import static com.relogiclabs.jschema.time.JsonDateTime.UNSET; import static java.time.DayOfWeek.FRIDAY; import static java.time.DayOfWeek.MONDAY; @@ -39,7 +38,6 @@ import static java.time.DayOfWeek.WEDNESDAY; import static org.apache.commons.lang3.StringUtils.removeEnd; -@RequiredArgsConstructor public class DateTimeContext { private static final int PIVOT_YEAR = 50; private static final int[] DAYS_IN_MONTH = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; @@ -93,21 +91,27 @@ private static void addWeekday(String key1, String key2, int value) { private int utcMinute = UNSET; @Getter - public final DateTimeType type; + private final DateTimeParser parser; + + @Getter + private final DateTimeType type; + + public DateTimeContext(DateTimeParser parser) { + this.parser = parser; + this.type = parser.getType(); + } public void setEra(String era) { var eraNum = switch(era.toUpperCase()) { case "BC" -> 1; case "AD" -> 2; - default -> throw new InvalidDateTimeException(DERA02, - "Invalid " + type + " era input"); + default -> throw failOnInvalidDateTime(ERAVDF01, "era input"); }; this.era = checkField(this.era, eraNum); } public void setYear(int year, int digitNum) { - if(year < 1 || year > 9999) throw new InvalidDateTimeException(DYAR03, - "Invalid " + type + " year out of range"); + if(year < 1 || year > 9999) throw failOnInvalidDateTime(YARVDF01, "year out of range"); year = digitNum <= 2 ? toFourDigitYear(year) : year; this.year = checkField(this.year, year); } @@ -118,8 +122,7 @@ public void setMonth(String month) { } public void setMonth(int month) { - if(month < 1 || month > 12) throw new InvalidDateTimeException(DMON05, - "Invalid " + type + " month out of range"); + if(month < 1 || month > 12) throw failOnInvalidDateTime(MONVDF01, "month out of range"); this.month = checkField(this.month, month); } @@ -129,8 +132,7 @@ public void setWeekday(String weekday) { } public void setDay(int day) { - if(day < 1 || day > 31) throw new InvalidDateTimeException(DDAY04, - "Invalid " + type + " day out of range"); + if(day < 1 || day > 31) throw failOnInvalidDateTime(DAYVDF01, "day out of range"); this.day = checkField(this.day, day); } @@ -138,34 +140,28 @@ public void setAmPm(String amPm) { var amPmNum = switch(amPm.toLowerCase()) { case "am" -> 1; case "pm" -> 2; - default -> throw new InvalidDateTimeException(DTAP02, - "Invalid " + type + " hour AM/PM input"); + default -> throw failOnInvalidDateTime(AMPMVD01, "hour AM/PM input"); }; if(hour != UNSET && (hour < 1 || hour > 12)) - throw new InvalidDateTimeException(DHUR03, - "Invalid " + type + " hour AM/PM out of range"); + throw failOnInvalidDateTime(HURVDF01, "hour out of range for AM/PM"); this.amPm = checkField(this.amPm, amPmNum); } public void setHour(int hour) { if(amPm != UNSET && (this.hour < 1 || this.hour > 12)) - throw new InvalidDateTimeException(DHUR04, - "Invalid " + type + " hour AM/PM out of range"); + throw failOnInvalidDateTime(HURVDF02, "hour out of range for AM/PM"); if(hour < 0 || hour > 23) - throw new InvalidDateTimeException(DHUR06, - "Invalid " + type + " hour out of range"); + throw failOnInvalidDateTime(HURVDF03, "hour out of range"); this.hour = checkField(this.hour, hour); } public void setMinute(int minute) { - if(minute < 0 || minute > 59) throw new InvalidDateTimeException(DMIN03, - "Invalid " + type + " minute out of range"); + if(minute < 0 || minute > 59) throw failOnInvalidDateTime(MNTVDF01, "minute out of range"); this.minute = checkField(this.minute, minute); } public void setSecond(int second) { - if(second < 0 || second > 59) throw new InvalidDateTimeException(DSEC03, - "Invalid " + type + " second out of range"); + if(second < 0 || second > 59) throw failOnInvalidDateTime(SECVDF01, "second out of range"); this.second = checkField(this.second, second); } @@ -174,17 +170,17 @@ public void setFraction(int fraction) { } public void setUtcOffset(int hour, int minute) { - if(hour < -12 || hour > 12) throw new InvalidDateTimeException(DUTC04, - "Invalid " + type + " UTC offset hour out of range"); - if(minute < 0 || minute > 59) throw new InvalidDateTimeException(DUTC05, - "Invalid " + type + " UTC offset minute out of range"); + if(hour < -12 || hour > 12) throw failOnInvalidDateTime(UTCVDF01, + "UTC offset hour out of range"); + if(minute < 0 || minute > 59) throw failOnInvalidDateTime(UTCVDF02, + "UTC offset minute out of range"); utcHour = checkField(utcHour, hour); utcMinute = checkField(utcMinute, minute); } private int checkField(int current, int newValue) { - if(current != UNSET && current != newValue) - throw new InvalidDateTimeException(DCNF01, "Conflicting " + type + " segments input"); + if(current != UNSET && current != newValue) throw new InvalidDateTimeException(CNFLDT01, + "Conflicting target " + type + " segments input", this); return newValue; } @@ -198,24 +194,21 @@ public JsonDateTime validate() { if(isAllSet(year, month, day)) { DAYS_IN_MONTH[2] = isLeapYear(year)? 29 : 28; if(day < 1 || day > DAYS_IN_MONTH[month]) - throw new InvalidDateTimeException(DDAY03, - "Invalid " + type + " day out of range"); + throw failOnInvalidDateTime(DAYVDF02, "day out of range"); dateTime = new JsonDateTime(type, year, month, day); if(weekday != UNSET && dateTime.getDayOfWeek().getValue() != weekday) - throw new InvalidDateTimeException(DWKD03, - "Invalid " + type + " weekday input"); + throw failOnInvalidDateTime(WEKVDF01, "weekday not matched"); } if(isAllSet(hour, amPm)) convertTo24Hour(); if(hour != UNSET && (hour < 0 || hour > 23)) - throw new InvalidDateTimeException(DHUR05, - "Invalid " + type + " hour out of range"); + throw failOnInvalidDateTime(HURVDF04, "hour out of range"); return new JsonDateTime(type, year, month, day, hour, minute, second, - fraction, new JsonUtcOffset(utcHour, utcMinute)); + fraction, new JsonUtcOffset(utcHour, utcMinute)); } catch(InvalidDateTimeException e) { throw e; } catch(Exception e) { - throw new InvalidDateTimeException(DINV01, - "Invalid " + type + " year, month or day out of range", e); + throw new InvalidDateTimeException(INVLDT02, + "Invalid target " + type + " year, month or day out of range", this, e); } } @@ -254,4 +247,8 @@ public String toString() { private void append(StringBuilder builder, String label, int value) { builder.append(label).append(value).append(", "); } + + private InvalidDateTimeException failOnInvalidDateTime(String code, String message) { + return new InvalidDateTimeException(code, "Invalid target " + type + " " + message, this); + } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeParser.java b/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeParser.java index 860f802..e15f5db 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeParser.java +++ b/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeParser.java @@ -6,7 +6,6 @@ import com.relogiclabs.jschema.internal.util.LogHelper; import com.relogiclabs.jschema.time.DateTimeType; import com.relogiclabs.jschema.time.JsonDateTime; -import com.relogiclabs.jschema.util.Reference; import lombok.Getter; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.Token; @@ -77,7 +76,7 @@ import static com.relogiclabs.jschema.internal.time.SegmentProcessor.Whitespace; import static com.relogiclabs.jschema.internal.time.SegmentProcessor.YearNumber2; import static com.relogiclabs.jschema.internal.time.SegmentProcessor.YearNumber4; -import static com.relogiclabs.jschema.message.ErrorCode.DINV02; +import static com.relogiclabs.jschema.message.ErrorCode.INVLDT01; public final class DateTimeParser { private static final Map PROCESSORS; @@ -138,11 +137,12 @@ public DateTimeParser(String pattern, DateTimeType type) { private JsonDateTime parse(String input, DateTimeContext context) { for(var token : lexerTokens) { - var processor = PROCESSORS.get(dateTimeLexer.getVocabulary().getSymbolicName(token.getType())); + var processor = PROCESSORS.get(dateTimeLexer.getVocabulary() + .getSymbolicName(token.getType())); input = processor.process(input, token, context); } - if(!input.isEmpty()) throw new InvalidDateTimeException(DINV02, - "Invalid " + context.getType() + " input format"); + if(!input.isEmpty()) throw new InvalidDateTimeException(INVLDT01, + "Invalid " + type + " input malformed", context); var dateTime = context.validate(); LogHelper.debug(context); @@ -150,16 +150,6 @@ private JsonDateTime parse(String input, DateTimeContext context) { } public JsonDateTime parse(String input) { - return parse(input, new DateTimeContext(type)); - } - - public JsonDateTime tryParse(String input, Reference error) { - try { - return parse(input); - } catch(InvalidDateTimeException e) { - LogHelper.debug(e); - error.setValue(e.getMessage()); - return null; - } + return parse(input, new DateTimeContext(this)); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/time/SegmentProcessor.java b/src/main/java/com/relogiclabs/jschema/internal/time/SegmentProcessor.java index 1b5b888..306d6fa 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/time/SegmentProcessor.java +++ b/src/main/java/com/relogiclabs/jschema/internal/time/SegmentProcessor.java @@ -6,44 +6,43 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import static com.relogiclabs.jschema.message.ErrorCode.DDAY01; -import static com.relogiclabs.jschema.message.ErrorCode.DDAY02; -import static com.relogiclabs.jschema.message.ErrorCode.DERA01; -import static com.relogiclabs.jschema.message.ErrorCode.DFRC01; -import static com.relogiclabs.jschema.message.ErrorCode.DFRC02; -import static com.relogiclabs.jschema.message.ErrorCode.DFRC03; -import static com.relogiclabs.jschema.message.ErrorCode.DFRC04; -import static com.relogiclabs.jschema.message.ErrorCode.DFRC05; -import static com.relogiclabs.jschema.message.ErrorCode.DFRC06; -import static com.relogiclabs.jschema.message.ErrorCode.DFRC07; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR01; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR02; -import static com.relogiclabs.jschema.message.ErrorCode.DMIN01; -import static com.relogiclabs.jschema.message.ErrorCode.DMIN02; -import static com.relogiclabs.jschema.message.ErrorCode.DMON01; -import static com.relogiclabs.jschema.message.ErrorCode.DMON02; -import static com.relogiclabs.jschema.message.ErrorCode.DMON03; -import static com.relogiclabs.jschema.message.ErrorCode.DMON04; -import static com.relogiclabs.jschema.message.ErrorCode.DSEC01; -import static com.relogiclabs.jschema.message.ErrorCode.DSEC02; -import static com.relogiclabs.jschema.message.ErrorCode.DSYM01; -import static com.relogiclabs.jschema.message.ErrorCode.DTAP01; -import static com.relogiclabs.jschema.message.ErrorCode.DTXT01; -import static com.relogiclabs.jschema.message.ErrorCode.DUTC01; -import static com.relogiclabs.jschema.message.ErrorCode.DUTC02; -import static com.relogiclabs.jschema.message.ErrorCode.DUTC03; -import static com.relogiclabs.jschema.message.ErrorCode.DWKD01; -import static com.relogiclabs.jschema.message.ErrorCode.DWKD02; -import static com.relogiclabs.jschema.message.ErrorCode.DWTS01; -import static com.relogiclabs.jschema.message.ErrorCode.DYAR01; -import static com.relogiclabs.jschema.message.ErrorCode.DYAR02; +import static com.relogiclabs.jschema.message.ErrorCode.AMPMDT01; +import static com.relogiclabs.jschema.message.ErrorCode.DAYNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.DAYNUM02; +import static com.relogiclabs.jschema.message.ErrorCode.ERANAM01; +import static com.relogiclabs.jschema.message.ErrorCode.FRACDT01; +import static com.relogiclabs.jschema.message.ErrorCode.FRACDT02; +import static com.relogiclabs.jschema.message.ErrorCode.FRACDT03; +import static com.relogiclabs.jschema.message.ErrorCode.FRACDT04; +import static com.relogiclabs.jschema.message.ErrorCode.FRACDT05; +import static com.relogiclabs.jschema.message.ErrorCode.FRACDT06; +import static com.relogiclabs.jschema.message.ErrorCode.FRACDT07; +import static com.relogiclabs.jschema.message.ErrorCode.HURNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.HURNUM02; +import static com.relogiclabs.jschema.message.ErrorCode.MNTNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.MNTNUM02; +import static com.relogiclabs.jschema.message.ErrorCode.MONNAM01; +import static com.relogiclabs.jschema.message.ErrorCode.MONNAM02; +import static com.relogiclabs.jschema.message.ErrorCode.MONNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.MONNUM02; +import static com.relogiclabs.jschema.message.ErrorCode.SECNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.SECNUM02; +import static com.relogiclabs.jschema.message.ErrorCode.SYMBDT01; +import static com.relogiclabs.jschema.message.ErrorCode.TEXTDT01; +import static com.relogiclabs.jschema.message.ErrorCode.UTCTIM01; +import static com.relogiclabs.jschema.message.ErrorCode.UTCTIM02; +import static com.relogiclabs.jschema.message.ErrorCode.UTCTIM03; +import static com.relogiclabs.jschema.message.ErrorCode.WEKNAM01; +import static com.relogiclabs.jschema.message.ErrorCode.WEKNAM02; +import static com.relogiclabs.jschema.message.ErrorCode.WSPACE01; +import static com.relogiclabs.jschema.message.ErrorCode.YARNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.YARNUM02; import static java.util.regex.Pattern.CASE_INSENSITIVE; import static java.util.regex.Pattern.compile; import static org.apache.commons.lang3.StringUtils.replaceChars; import static org.apache.commons.lang3.StringUtils.substring; abstract class SegmentProcessor { - private static final Pattern EraRegex = compile("^(BC|AD)", CASE_INSENSITIVE); private static final Pattern YearNumber4Regex = compile("^(\\d{4})"); private static final Pattern YearNumber2Regex = compile("^(\\d{2})"); @@ -86,41 +85,41 @@ abstract class SegmentProcessor { public static final SegmentProcessor Symbol = new SymbolProcessor(); public static final SegmentProcessor Whitespace = new WhitespaceProcessor(); - public static final SegmentProcessor Era = new EraProcessor(EraRegex, DERA01); - public static final SegmentProcessor YearNumber4 = new YearNumberProcessor(YearNumber4Regex, DYAR01); - public static final SegmentProcessor YearNumber2 = new YearNumberProcessor(YearNumber2Regex, DYAR02); + public static final SegmentProcessor Era = new EraProcessor(EraRegex, ERANAM01); + public static final SegmentProcessor YearNumber4 = new YearNumberProcessor(YearNumber4Regex, YARNUM01); + public static final SegmentProcessor YearNumber2 = new YearNumberProcessor(YearNumber2Regex, YARNUM02); - public static final SegmentProcessor MonthName = new MonthNameProcessor(MonthNameRegex, DMON01); - public static final SegmentProcessor MonthShortName = new MonthNameProcessor(MonthShortNameRegex, DMON02); - public static final SegmentProcessor MonthNumber2 = new MonthNumberProcessor(MonthNumber2Regex, DMON03); - public static final SegmentProcessor MonthNumber = new MonthNumberProcessor(MonthNumberRegex, DMON04); + public static final SegmentProcessor MonthName = new MonthNameProcessor(MonthNameRegex, MONNAM01); + public static final SegmentProcessor MonthShortName = new MonthNameProcessor(MonthShortNameRegex, MONNAM02); + public static final SegmentProcessor MonthNumber2 = new MonthNumberProcessor(MonthNumber2Regex, MONNUM01); + public static final SegmentProcessor MonthNumber = new MonthNumberProcessor(MonthNumberRegex, MONNUM02); - public static final SegmentProcessor WeekdayName = new WeekdayProcessor(WeekdayNameRegex, DWKD01); - public static final SegmentProcessor WeekdayShortName = new WeekdayProcessor(WeekdayShortNameRegex, DWKD02); - public static final SegmentProcessor DayNumber2 = new DayNumberProcessor(DayNumber2Regex, DDAY01); - public static final SegmentProcessor DayNumber = new DayNumberProcessor(DayNumberRegex, DDAY02); + public static final SegmentProcessor WeekdayName = new WeekdayProcessor(WeekdayNameRegex, WEKNAM01); + public static final SegmentProcessor WeekdayShortName = new WeekdayProcessor(WeekdayShortNameRegex, WEKNAM02); + public static final SegmentProcessor DayNumber2 = new DayNumberProcessor(DayNumber2Regex, DAYNUM01); + public static final SegmentProcessor DayNumber = new DayNumberProcessor(DayNumberRegex, DAYNUM02); - public static final SegmentProcessor ClockAmPm = new ClockAmPmProcessor(ClockAmPmRegex, DTAP01); - public static final SegmentProcessor HourNumber2 = new HourNumberProcessor(HourNumber2Regex, DHUR01); - public static final SegmentProcessor HourNumber = new HourNumberProcessor(HourNumberRegex, DHUR02); + public static final SegmentProcessor ClockAmPm = new ClockAmPmProcessor(ClockAmPmRegex, AMPMDT01); + public static final SegmentProcessor HourNumber2 = new HourNumberProcessor(HourNumber2Regex, HURNUM01); + public static final SegmentProcessor HourNumber = new HourNumberProcessor(HourNumberRegex, HURNUM02); - public static final SegmentProcessor MinuteNumber2 = new MinuteNumberProcessor(MinuteNumber2Regex, DMIN01); - public static final SegmentProcessor MinuteNumber = new MinuteNumberProcessor(MinuteNumberRegex, DMIN02); + public static final SegmentProcessor MinuteNumber2 = new MinuteNumberProcessor(MinuteNumber2Regex, MNTNUM01); + public static final SegmentProcessor MinuteNumber = new MinuteNumberProcessor(MinuteNumberRegex, MNTNUM02); - public static final SegmentProcessor SecondNumber2 = new SecondNumberProcessor(SecondNumber2Regex, DSEC01); - public static final SegmentProcessor SecondNumber = new SecondNumberProcessor(SecondNumberRegex, DSEC02); + public static final SegmentProcessor SecondNumber2 = new SecondNumberProcessor(SecondNumber2Regex, SECNUM01); + public static final SegmentProcessor SecondNumber = new SecondNumberProcessor(SecondNumberRegex, SECNUM02); - public static final SegmentProcessor FractionNumber = new FractionNumberProcessor(FractionNumberRegex, DFRC01); - public static final SegmentProcessor FractionNumber1 = new FractionNumberProcessor(FractionNumber1Regex, DFRC02); - public static final SegmentProcessor FractionNumber2 = new FractionNumberProcessor(FractionNumber2Regex, DFRC03); - public static final SegmentProcessor FractionNumber3 = new FractionNumberProcessor(FractionNumber3Regex, DFRC04); - public static final SegmentProcessor FractionNumber4 = new FractionNumberProcessor(FractionNumber4Regex, DFRC05); - public static final SegmentProcessor FractionNumber5 = new FractionNumberProcessor(FractionNumber5Regex, DFRC06); - public static final SegmentProcessor FractionNumber6 = new FractionNumberProcessor(FractionNumber6Regex, DFRC07); + public static final SegmentProcessor FractionNumber = new FractionNumberProcessor(FractionNumberRegex, FRACDT01); + public static final SegmentProcessor FractionNumber1 = new FractionNumberProcessor(FractionNumber1Regex, FRACDT02); + public static final SegmentProcessor FractionNumber2 = new FractionNumberProcessor(FractionNumber2Regex, FRACDT03); + public static final SegmentProcessor FractionNumber3 = new FractionNumberProcessor(FractionNumber3Regex, FRACDT04); + public static final SegmentProcessor FractionNumber4 = new FractionNumberProcessor(FractionNumber4Regex, FRACDT05); + public static final SegmentProcessor FractionNumber5 = new FractionNumberProcessor(FractionNumber5Regex, FRACDT06); + public static final SegmentProcessor FractionNumber6 = new FractionNumberProcessor(FractionNumber6Regex, FRACDT07); - public static final SegmentProcessor UtcOffsetHour = new UtcOffsetHourProcessor(UtcOffsetHourRegex, DUTC01); - public static final SegmentProcessor UtcOffsetTime1 = new UtcOffsetTimeProcessor(UtcOffsetTime1Regex, DUTC02); - public static final SegmentProcessor UtcOffsetTime2 = new UtcOffsetTimeProcessor(UtcOffsetTime2Regex, DUTC03); + public static final SegmentProcessor UtcOffsetHour = new UtcOffsetHourProcessor(UtcOffsetHourRegex, UTCTIM01); + public static final SegmentProcessor UtcOffsetTime1 = new UtcOffsetTimeProcessor(UtcOffsetTime1Regex, UTCTIM02); + public static final SegmentProcessor UtcOffsetTime2 = new UtcOffsetTimeProcessor(UtcOffsetTime2Regex, UTCTIM03); public abstract String process(String input, Token token, DateTimeContext context); @@ -129,8 +128,8 @@ private static final class TextProcessor extends SegmentProcessor { @Override public String process(String input, Token token, DateTimeContext context) { var text = replaceChars(substring(token.getText(), 1, -1), "''", "'"); - if(!input.startsWith(text)) throw failOnInvalidDateTime(context, DTXT01, - "text mismatch or input format"); + if(!input.startsWith(text)) throw failOnInvalidDateTime(context, TEXTDT01, + "preset text input"); return input.substring(text.length()); } } @@ -138,8 +137,8 @@ public String process(String input, Token token, DateTimeContext context) { private static final class SymbolProcessor extends SegmentProcessor { @Override public String process(String input, Token token, DateTimeContext context) { - if(!input.startsWith(token.getText())) throw failOnInvalidDateTime(context, DSYM01, - "symbol mismatch or input format"); + if(!input.startsWith(token.getText())) throw failOnInvalidDateTime(context, SYMBDT01, + "symbol input"); return input.substring(token.getText().length()); } } @@ -147,8 +146,8 @@ public String process(String input, Token token, DateTimeContext context) { private static final class WhitespaceProcessor extends SegmentProcessor { @Override public String process(String input, Token token, DateTimeContext context) { - if(!input.startsWith(token.getText())) throw failOnInvalidDateTime(context, DWTS01, - "whitespace mismatch or input format"); + if(!input.startsWith(token.getText())) throw failOnInvalidDateTime(context, WSPACE01, + "whitespace input"); return input.substring(token.getText().length()); } } @@ -205,7 +204,7 @@ private MonthNameProcessor(Pattern regex, String code) { @Override protected void process(Matcher matcher, DateTimeContext context) { if(!matcher.lookingAt()) throw failOnInvalidDateTime(context, code, - "month name input"); + "month name input"); context.setMonth(matcher.group(1)); } } @@ -218,7 +217,7 @@ private MonthNumberProcessor(Pattern regex, String code) { @Override protected void process(Matcher matcher, DateTimeContext context) { if(!matcher.lookingAt()) throw failOnInvalidDateTime(context, code, - "month number input"); + "month number input"); context.setMonth(Integer.parseInt(matcher.group(1))); } } @@ -303,7 +302,7 @@ private FractionNumberProcessor(Pattern regex, String code) { @Override protected void process(Matcher matcher, DateTimeContext context) { if(!matcher.lookingAt()) throw failOnInvalidDateTime(context, code, - "second fraction input"); + "second fraction input"); context.setFraction(Integer.parseInt(matcher.group(1))); } } @@ -316,9 +315,9 @@ private UtcOffsetHourProcessor(Pattern regex, String code) { @Override protected void process(Matcher matcher, DateTimeContext context) { if(!matcher.lookingAt()) throw failOnInvalidDateTime(context, code, - "UTC offset hour input"); + "UTC offset hour input"); context.setUtcOffset("Z".equals(matcher.group()) - ? 0 : Integer.parseInt(matcher.group(1)), 0); + ? 0 : Integer.parseInt(matcher.group(1)), 0); } } @@ -330,7 +329,7 @@ private UtcOffsetTimeProcessor(Pattern regex, String code) { @Override protected void process(Matcher matcher, DateTimeContext context) { if(!matcher.lookingAt()) throw failOnInvalidDateTime(context, code, - "UTC offset input"); + "UTC offset input"); if("Z".equals(matcher.group())) context.setUtcOffset(0, 0); else context.setUtcOffset(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2))); @@ -339,6 +338,7 @@ protected void process(Matcher matcher, DateTimeContext context) { private static InvalidDateTimeException failOnInvalidDateTime(DateTimeContext context, String code, String message) { - return new InvalidDateTimeException(code, "Invalid " + context.getType() + " " + message); + return new InvalidDateTimeException(code, "Invalid target " + context.getType() + + " " + message, context); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/internal/tree/NativeFunction.java b/src/main/java/com/relogiclabs/jschema/internal/tree/NativeFunction.java index 7e17e3e..ddca162 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/tree/NativeFunction.java +++ b/src/main/java/com/relogiclabs/jschema/internal/tree/NativeFunction.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.internal.tree; -import com.relogiclabs.jschema.exception.InvalidFunctionException; +import com.relogiclabs.jschema.exception.InvalidReturnTypeException; import com.relogiclabs.jschema.exception.TargetInvocationException; import com.relogiclabs.jschema.function.FunctionProvider; import com.relogiclabs.jschema.node.JFunction; @@ -18,9 +18,9 @@ import static com.relogiclabs.jschema.internal.util.CollectionHelper.subList; import static com.relogiclabs.jschema.internal.util.CommonHelper.getDerived; import static com.relogiclabs.jschema.internal.util.CommonHelper.nonNullFrom; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC07; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC08; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC09; +import static com.relogiclabs.jschema.message.ErrorCode.FNCNVK01; +import static com.relogiclabs.jschema.message.ErrorCode.FNCNVK02; +import static com.relogiclabs.jschema.message.ErrorCode.FNCNVK03; import static java.util.stream.Collectors.joining; @Getter @@ -69,14 +69,14 @@ public Object invoke(JFunction caller, Object[] arguments) { instance.setRuntime(caller.getRuntime()); instance.setCaller(caller); var result = method.invoke(instance, arguments); - if(result == null) throw new InvalidFunctionException(FUNC07, + if(result == null) throw new InvalidReturnTypeException(FNCNVK01, "Function " + method.getName() + " must not return null"); return result; } catch (InvocationTargetException e) { - throw new TargetInvocationException(FUNC08, + throw new TargetInvocationException(FNCNVK02, "Target invocation exception occurred", nonNullFrom(e.getCause(), e)); } catch (IllegalAccessException e) { - throw new TargetInvocationException(FUNC09, "Illegal access exception occurred", e); + throw new TargetInvocationException(FNCNVK03, "Illegal access exception occurred", e); } } diff --git a/src/main/java/com/relogiclabs/jschema/internal/tree/SchemaTreeVisitor.java b/src/main/java/com/relogiclabs/jschema/internal/tree/SchemaTreeVisitor.java index 08aa597..3f7cf74 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/tree/SchemaTreeVisitor.java +++ b/src/main/java/com/relogiclabs/jschema/internal/tree/SchemaTreeVisitor.java @@ -233,7 +233,7 @@ public JNode visitDatatypeNode(DatatypeNodeContext ctx) { return new JDataTypeBuilder() .relations(relations) .context(new Context(ctx, runtime)) - .jsonType(JsonType.from(ctx.S_DATATYPE())) + .jsonType(JsonType.from(ctx.S_DATATYPE().getSymbol())) .alias((JAlias) visit(ctx.aliasNode())) .nested(ctx.S_STAR() != null) .build(); diff --git a/src/main/java/com/relogiclabs/jschema/internal/tree/ScriptFunction.java b/src/main/java/com/relogiclabs/jschema/internal/tree/ScriptFunction.java index 356d747..ab5a390 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/tree/ScriptFunction.java +++ b/src/main/java/com/relogiclabs/jschema/internal/tree/ScriptFunction.java @@ -1,9 +1,9 @@ package com.relogiclabs.jschema.internal.tree; -import com.relogiclabs.jschema.exception.InvalidFunctionException; -import com.relogiclabs.jschema.exception.JsonSchemaException; -import com.relogiclabs.jschema.exception.ScriptCommonException; -import com.relogiclabs.jschema.exception.ScriptInitiatedException; +import com.relogiclabs.jschema.exception.BaseRuntimeException; +import com.relogiclabs.jschema.exception.FunctionValidationException; +import com.relogiclabs.jschema.exception.InvalidReturnTypeException; +import com.relogiclabs.jschema.exception.ScriptThrowInitiatedException; import com.relogiclabs.jschema.function.FutureFunction; import com.relogiclabs.jschema.internal.script.GArray; import com.relogiclabs.jschema.internal.script.GFunction; @@ -20,7 +20,7 @@ import static com.relogiclabs.jschema.internal.util.CollectionHelper.subList; import static com.relogiclabs.jschema.internal.util.StringHelper.joinWith; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC08; +import static com.relogiclabs.jschema.message.ErrorCode.FNSRET01; import static com.relogiclabs.jschema.type.EValue.VOID; @Getter @@ -64,8 +64,8 @@ public Object invoke(JFunction caller, Object[] arguments) { int i = 1; for(var p : parameters) scope.addVariable(p.getName(), (EValue) arguments[i++]); return function.isFuture() - ? (FutureFunction) () -> invoke(scope) - : invoke(scope); + ? (FutureFunction) () -> invoke(scope) + : invoke(scope); } private boolean invoke(ConstraintScope scope) { @@ -73,12 +73,12 @@ private boolean invoke(ConstraintScope scope) { scope.unpackReceivers(); var result = function.invoke(scope); if(result == VOID) return true; - if(!(result instanceof EBoolean b)) throw new InvalidFunctionException(FUNC08, + if(!(result instanceof EBoolean b)) throw new InvalidReturnTypeException(FNSRET01, "Function '" + name + "' must return a boolean value"); return b.getValue(); - } catch(JsonSchemaException | ScriptInitiatedException e) { + } catch(FunctionValidationException | ScriptThrowInitiatedException e) { throw e; - } catch(ScriptCommonException e) { + } catch(BaseRuntimeException e) { scope.getRuntime().getExceptions().fail(e); return false; } diff --git a/src/main/java/com/relogiclabs/jschema/internal/tree/TreeHelper.java b/src/main/java/com/relogiclabs/jschema/internal/tree/TreeHelper.java index b5a93c7..f2d7340 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/tree/TreeHelper.java +++ b/src/main/java/com/relogiclabs/jschema/internal/tree/TreeHelper.java @@ -9,8 +9,8 @@ import java.util.function.Function; import static com.relogiclabs.jschema.internal.util.StreamHelper.halt; -import static com.relogiclabs.jschema.message.ErrorCode.PROP03; -import static com.relogiclabs.jschema.message.ErrorCode.PROP04; +import static com.relogiclabs.jschema.message.ErrorCode.PRTDUP01; +import static com.relogiclabs.jschema.message.ErrorCode.PRTDUP02; import static com.relogiclabs.jschema.message.MessageFormatter.formatForJson; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; import static com.relogiclabs.jschema.tree.TreeType.JSON_TREE; @@ -26,12 +26,12 @@ public static List requireUniqueness(List list, TreeType t } private static ErrorDetail format(TreeType type, JProperty property) { - if(type == JSON_TREE) return formatForJson(PROP03, getMessage(property), property); - if(type == SCHEMA_TREE) return formatForSchema(PROP04, getMessage(property), property); + if(type == JSON_TREE) return formatForJson(PRTDUP01, getMessage(property), property); + if(type == SCHEMA_TREE) return formatForSchema(PRTDUP02, getMessage(property), property); throw new IllegalStateException("Invalid parser state"); } private static String getMessage(JProperty property) { - return "Multiple key with name '" + property.getKey() + "' found"; + return "Duplicate key with name '" + property.getKey() + "' found"; } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/message/ContextDetail.java b/src/main/java/com/relogiclabs/jschema/message/ContextDetail.java index 3160885..448e768 100644 --- a/src/main/java/com/relogiclabs/jschema/message/ContextDetail.java +++ b/src/main/java/com/relogiclabs/jschema/message/ContextDetail.java @@ -2,9 +2,9 @@ import com.relogiclabs.jschema.node.JNode; import com.relogiclabs.jschema.tree.Context; -import com.relogiclabs.jschema.tree.Location; import lombok.AllArgsConstructor; import lombok.Getter; +import org.antlr.v4.runtime.Token; @Getter @AllArgsConstructor @@ -17,7 +17,11 @@ public ContextDetail(JNode node, String message) { this.message = message; } - public Location getLocation() { - return context.getLocation(); + public String getLocation() { + return getLocation(context.getToken()); + } + + public static String getLocation(Token token) { + return token.getLine() + ":" + token.getCharPositionInLine(); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/message/ErrorCode.java b/src/main/java/com/relogiclabs/jschema/message/ErrorCode.java index 9b89202..6a4e93f 100644 --- a/src/main/java/com/relogiclabs/jschema/message/ErrorCode.java +++ b/src/main/java/com/relogiclabs/jschema/message/ErrorCode.java @@ -1,330 +1,336 @@ package com.relogiclabs.jschema.message; public interface ErrorCode { - String ACTL01 = "ACTL01"; - String ACTL02 = "ACTL02"; - String ACTL03 = "ACTL03"; - String ADDN01 = "ADDN01"; - String ADDN02 = "ADDN02"; - String ADDN03 = "ADDN03"; - String ADDT01 = "ADDT01"; - String ADDT02 = "ADDT02"; - String AFND01 = "AFND01"; - String AFTR01 = "AFTR01"; - String AFTR02 = "AFTR02"; - String AIDX01 = "AIDX01"; - String AIDX02 = "AIDX02"; - String ALEN01 = "ALEN01"; - String ALEN02 = "ALEN02"; - String ALEN03 = "ALEN03"; - String ALEN04 = "ALEN04"; - String ALEN05 = "ALEN05"; - String ANDL01 = "ANDL01"; - String ARAS01 = "ARAS01"; - String ARNG01 = "ARNG01"; - String ARNG02 = "ARNG02"; - String ARNG03 = "ARNG03"; - String ARRL01 = "ARRL01"; - String ARRY01 = "ARRY01"; - String ARRY02 = "ARRY02"; - String ARUD01 = "ARUD01"; - String ASIN01 = "ASIN01"; - String ASIN02 = "ASIN02"; - String ASIN03 = "ASIN03"; - String ASIN04 = "ASIN04"; - String ASIN05 = "ASIN05"; - String AUPD01 = "AUPD01"; - String AUPD02 = "AUPD02"; - String AUPD03 = "AUPD03"; - String AWRT01 = "AWRT01"; - String AWRT02 = "AWRT02"; - String BFOR01 = "BFOR01"; - String BFOR02 = "BFOR02"; - String BKTA01 = "BKTA01"; - String BKTR01 = "BKTR01"; - String BKTR02 = "BKTR02"; - String BKTU01 = "BKTU01"; - String BKTU02 = "BKTU02"; - String BLOK01 = "BLOK01"; - String BOOL01 = "BOOL01"; - String CALR01 = "CALR01"; - String CALR02 = "CALR02"; - String CLAS01 = "CLAS01"; - String CLAS02 = "CLAS02"; - String CLAS03 = "CLAS03"; - String DCNF01 = "DCNF01"; - String DDAY01 = "DDAY01"; - String DDAY02 = "DDAY02"; - String DDAY03 = "DDAY03"; - String DDAY04 = "DDAY04"; - String DECE01 = "DECE01"; - String DECE02 = "DECE02"; - String DECE03 = "DECE03"; - String DECT01 = "DECT01"; - String DECT02 = "DECT02"; - String DECT03 = "DECT03"; - String DEFI01 = "DEFI01"; - String DEFI02 = "DEFI02"; - String DEFI03 = "DEFI03"; - String DEFI04 = "DEFI04"; - String DERA01 = "DERA01"; - String DERA02 = "DERA02"; - String DFRC01 = "DFRC01"; - String DFRC02 = "DFRC02"; - String DFRC03 = "DFRC03"; - String DFRC04 = "DFRC04"; - String DFRC05 = "DFRC05"; - String DFRC06 = "DFRC06"; - String DFRC07 = "DFRC07"; - String DHUR01 = "DHUR01"; - String DHUR02 = "DHUR02"; - String DHUR03 = "DHUR03"; - String DHUR04 = "DHUR04"; - String DHUR05 = "DHUR05"; - String DHUR06 = "DHUR06"; - String DINV01 = "DINV01"; - String DINV02 = "DINV02"; - String DIVD01 = "DIVD01"; - String DIVD02 = "DIVD02"; - String DIVN01 = "DIVN01"; - String DIVN02 = "DIVN02"; - String DIVN03 = "DIVN03"; - String DLEX01 = "DLEX01"; - String DMIN01 = "DMIN01"; - String DMIN02 = "DMIN02"; - String DMIN03 = "DMIN03"; - String DMON01 = "DMON01"; - String DMON02 = "DMON02"; - String DMON03 = "DMON03"; - String DMON04 = "DMON04"; - String DMON05 = "DMON05"; - String DRNG01 = "DRNG01"; - String DRNG02 = "DRNG02"; - String DRNG03 = "DRNG03"; - String DRNG04 = "DRNG04"; - String DRNG05 = "DRNG05"; - String DRNG06 = "DRNG06"; - String DRNG07 = "DRNG07"; - String DRNG08 = "DRNG08"; - String DSEC01 = "DSEC01"; - String DSEC02 = "DSEC02"; - String DSEC03 = "DSEC03"; - String DSYM01 = "DSYM01"; - String DTAP01 = "DTAP01"; - String DTAP02 = "DTAP02"; - String DTXT01 = "DTXT01"; - String DTYP01 = "DTYP01"; - String DTYP02 = "DTYP02"; - String DTYP03 = "DTYP03"; - String DTYP04 = "DTYP04"; - String DTYP05 = "DTYP05"; - String DTYP06 = "DTYP06"; - String DTYP07 = "DTYP07"; - String DUBL01 = "DUBL01"; - String DUTC01 = "DUTC01"; - String DUTC02 = "DUTC02"; - String DUTC03 = "DUTC03"; - String DUTC04 = "DUTC04"; - String DUTC05 = "DUTC05"; - String DWKD01 = "DWKD01"; - String DWKD02 = "DWKD02"; - String DWKD03 = "DWKD03"; - String DWTS01 = "DWTS01"; - String DYAR01 = "DYAR01"; - String DYAR02 = "DYAR02"; - String DYAR03 = "DYAR03"; - String ELEM01 = "ELEM01"; - String EMAL01 = "EMAL01"; - String ENDE01 = "ENDE01"; - String ENDE02 = "ENDE02"; - String ENUM01 = "ENUM01"; - String ENUM02 = "ENUM02"; - String EQUL01 = "EQUL01"; - String EXPC01 = "EXPC01"; - String EXPC02 = "EXPC02"; - String EXPC03 = "EXPC03"; - String EXPR01 = "EXPR01"; - String EXPR02 = "EXPR02"; - String FAIL01 = "FAIL01"; - String FAIL02 = "FAIL02"; - String FAIL03 = "FAIL03"; - String FAIL04 = "FAIL04"; - String FAIL05 = "FAIL05"; - String FAIL06 = "FAIL06"; - String FAIL07 = "FAIL07"; - String FAIL08 = "FAIL08"; - String FAIL09 = "FAIL09"; - String FAIL10 = "FAIL10"; - String FAIL11 = "FAIL11"; - String FAIL12 = "FAIL12"; - String FILL01 = "FILL01"; - String FLOT01 = "FLOT01"; - String FNVK01 = "FNVK01"; - String FNVK02 = "FNVK02"; - String FNVK03 = "FNVK03"; - String FNVK04 = "FNVK04"; - String FORS01 = "FORS01"; - String FPRM01 = "FPRM01"; - String FREC01 = "FREC01"; - String FUNC01 = "FUNC01"; - String FUNC02 = "FUNC02"; - String FUNC03 = "FUNC03"; - String FUNC04 = "FUNC04"; - String FUNC05 = "FUNC05"; - String FUNC06 = "FUNC06"; - String FUNC07 = "FUNC07"; - String FUNC08 = "FUNC08"; - String FUNC09 = "FUNC08"; - String FUND01 = "FUND01"; - String FUND02 = "FUND02"; - String FUND03 = "FUND03"; - String IDEN01 = "IDEN01"; - String IDEN02 = "IDEN02"; - String IDEN03 = "IDEN03"; - String IDXA01 = "IDXA01"; - String IDXR01 = "IDXR01"; - String IDXU01 = "IDXU01"; - String IFST01 = "IFST01"; - String IFST02 = "IFST02"; - String INCE01 = "INCE01"; - String INCE02 = "INCE02"; - String INCE03 = "INCE03"; - String INCT01 = "INCT01"; - String INCT02 = "INCT02"; - String INCT03 = "INCT03"; - String INST01 = "INST01"; - String INST02 = "INST02"; - String INST03 = "INST03"; - String INST04 = "INST04"; - String INTE01 = "INTE01"; - String ITER01 = "ITER01"; - String JLEX01 = "JLEX01"; - String JPRS01 = "JPRS01"; - String KEYS01 = "KEYS01"; - String MAXI01 = "MAXI01"; - String MAXI02 = "MAXI02"; - String MAXI03 = "MAXI03"; - String MINI01 = "MINI01"; - String MINI02 = "MINI02"; - String MINI03 = "MINI03"; - String MINS01 = "MINS01"; - String MINS02 = "MINS02"; - String MNVK01 = "MNVK01"; - String MNVK02 = "MNVK02"; - String MODN01 = "MODN01"; - String MODN02 = "MODN02"; - String MODN03 = "MODN03"; - String MODU01 = "MODU01"; - String MODU02 = "MODU02"; - String MULN01 = "MULN01"; - String MULN02 = "MULN02"; - String MULN03 = "MULN03"; - String MULT01 = "MULT01"; - String MULT02 = "MULT02"; - String NEGI01 = "NEGI01"; - String NEGI02 = "NEGI02"; - String NEMT01 = "NEMT01"; - String NEMT02 = "NEMT02"; - String NEMT03 = "NEMT03"; - String NEQL01 = "NEQL01"; - String NOTL01 = "NOTL01"; - String OBJL01 = "OBJL01"; - String OLEN01 = "OLEN01"; - String OLEN02 = "OLEN02"; - String OLEN03 = "OLEN03"; - String OLEN04 = "OLEN04"; - String OLEN05 = "OLEN05"; - String ORLG01 = "ORLG01"; - String OUPD01 = "OUPD01"; - String OUPD02 = "OUPD02"; - String PHON01 = "PHON01"; - String PLUS01 = "PLUS01"; - String PLUS02 = "PLUS02"; - String POSI01 = "POSI01"; - String POSI02 = "POSI02"; - String POWR01 = "POWR01"; - String PRAG01 = "PRAG01"; - String PRAG02 = "PRAG02"; - String PRAG03 = "PRAG03"; - String PROP01 = "PROP01"; - String PROP02 = "PROP02"; - String PROP03 = "PROP03"; - String PROP04 = "PROP04"; - String PROP05 = "PROP05"; - String PROP06 = "PROP06"; - String PROP07 = "PROP07"; - String PRPT01 = "PRPT01"; - String PRPT02 = "PRPT02"; - String PRPT03 = "PRPT03"; - String PRPT04 = "PRPT04"; - String RANG01 = "RANG01"; - String RANG02 = "RANG02"; - String RANG03 = "RANG03"; - String RANG04 = "RANG04"; - String RECV01 = "RECV01"; - String RECV02 = "RECV02"; - String RECV03 = "RECV03"; - String REGX01 = "REGX01"; - String RELA01 = "RELA01"; - String RELA02 = "RELA02"; - String RELA03 = "RELA03"; - String RELA04 = "RELA04"; - String RELA05 = "RELA05"; - String RELA06 = "RELA06"; - String RELA07 = "RELA07"; - String RELA08 = "RELA08"; - String RETN01 = "RETN01"; - String RETN02 = "RETN02"; - String RETN03 = "RETN03"; - String RNGA01 = "RNGA01"; - String RNGR01 = "RNGR01"; - String RNGT01 = "RNGT01"; - String RNGT02 = "RNGT02"; - String RNGT03 = "RNGT03"; - String RNGT04 = "RNGT04"; - String RNGT05 = "RNGT05"; - String RNGT06 = "RNGT06"; - String RNGU01 = "RNGU01"; - String SASN01 = "SASN01"; - String SFND01 = "SFND01"; - String SFND02 = "SFND02"; - String SFND03 = "SFND03"; - String SIDX01 = "SIDX01"; - String SIDX02 = "SIDX02"; - String SLEN01 = "SLEN01"; - String SLEN02 = "SLEN02"; - String SLEN03 = "SLEN03"; - String SLEN04 = "SLEN04"; - String SLEN05 = "SLEN05"; - String SLEX01 = "SLEX01"; - String SPRS01 = "SPRS01"; - String SRNG01 = "SRNG01"; - String SRNG02 = "SRNG02"; - String SRNG03 = "SRNG03"; - String SRPT01 = "SRPT01"; - String SRPT02 = "SRPT02"; - String STRT01 = "STRT01"; - String STRT02 = "STRT02"; - String STRV01 = "STRV01"; - String SUBN01 = "SUBN01"; - String SUBN02 = "SUBN02"; - String SUBN03 = "SUBN03"; - String SUBT01 = "SUBT01"; - String SUBT02 = "SUBT02"; - String SUPD01 = "SUPD01"; - String THRO01 = "THRO01"; - String THRO02 = "THRO02"; - String TRGT01 = "TRGT01"; - String TRGT02 = "TRGT02"; - String TRYS01 = "TRYS01"; - String URLA01 = "URLA01"; - String URLA02 = "URLA02"; - String URLA03 = "URLA03"; - String URLA04 = "URLA04"; - String VALD01 = "VALD01"; - String VALU01 = "VALU01"; - String VARD01 = "VARD01"; - String VARD02 = "VARD02"; - String VARD03 = "VARD03"; - String WHIL01 = "WHIL01"; + String ACTLSF01 = "ACTLSF01"; + String ACTLSF02 = "ACTLSF02"; + String ACTLSF03 = "ACTLSF03"; + String ADDASN01 = "ADDASN01"; + String ADDASN02 = "ADDASN02"; + String ADDASN03 = "ADDASN03"; + String AFTRDT01 = "AFTRDT01"; + String AFTRDT02 = "AFTRDT02"; + String ALSDEF01 = "ALSDEF01"; + String ALSDEF02 = "ALSDEF02"; + String ALSDEF03 = "ALSDEF03"; + String ALSDUP01 = "ALSDUP01"; + String AMPMDT01 = "AMPMDT01"; + String AMPMVD01 = "AMPMVD01"; + String ARNSRT01 = "ARNSRT01"; + String ARNSRT02 = "ARNSRT02"; + String ARRASN01 = "ARRASN01"; + String ARRELM01 = "ARRELM01"; + String ARRIDX01 = "ARRIDX01"; + String ARRIDX02 = "ARRIDX02"; + String ARRLIT01 = "ARRLIT01"; + String ARROPT01 = "ARROPT01"; + String ARRRNG01 = "ARRRNG01"; + String ARRRNG02 = "ARRRNG02"; + String ARRRNG03 = "ARRRNG03"; + String ARRUPD01 = "ARRUPD01"; + String ASNBKT01 = "ASNBKT01"; + String ASNDOT01 = "ASNDOT01"; + String ASNDOT02 = "ASNDOT02"; + String ASNVAR01 = "ASNVAR01"; + String ASNVAR02 = "ASNVAR02"; + String ASNVAR03 = "ASNVAR03"; + String BFORDT01 = "BFORDT01"; + String BFORDT02 = "BFORDT02"; + String BKTASN01 = "BKTASN01"; + String BKTUPD01 = "BKTUPD01"; + String BKTUPD02 = "BKTUPD02"; + String BLOKSE01 = "BLOKSE01"; + String BOOLVL01 = "BOOLVL01"; + String CALRSE01 = "CALRSE01"; + String CALRSE02 = "CALRSE02"; + String CMPSGE01 = "CMPSGE01"; + String CMPSGE02 = "CMPSGE02"; + String CMPSGT01 = "CMPSGT01"; + String CMPSGT02 = "CMPSGT02"; + String CMPSLE01 = "CMPSLE01"; + String CMPSLE02 = "CMPSLE02"; + String CMPSLT01 = "CMPSLT01"; + String CMPSLT02 = "CMPSLT02"; + String CNFLDT01 = "CNFLDT01"; + String DAYNUM01 = "DAYNUM01"; + String DAYNUM02 = "DAYNUM02"; + String DAYVDF01 = "DAYVDF01"; + String DAYVDF02 = "DAYVDF02"; + String DECPRE01 = "DECPRE01"; + String DECPRE02 = "DECPRE02"; + String DECPRE03 = "DECPRE03"; + String DECPST01 = "DECPST01"; + String DECPST02 = "DECPST02"; + String DECPST03 = "DECPST03"; + String DIVASN01 = "DIVASN01"; + String DIVASN02 = "DIVASN02"; + String DIVASN03 = "DIVASN03"; + String DTYARG01 = "DTYARG01"; + String DTYARG02 = "DTYARG02"; + String DTYCPS01 = "DTYCPS01"; + String DTYCST01 = "DTYCST01"; + String DTYPMS01 = "DTYPMS01"; + String DTYPMS02 = "DTYPMS02"; + String DTYVDF01 = "DTYVDF01"; + String DUBLVL01 = "DUBLVL01"; + String ELEMCF01 = "ELEMCF01"; + String EMALCF01 = "EMALCF01"; + String EMPTCF01 = "EMPTCF01"; + String EMPTCF02 = "EMPTCF02"; + String EMPTCF03 = "EMPTCF03"; + String ENDFDT01 = "ENDFDT01"; + String ENDFDT02 = "ENDFDT02"; + String ENMNUM01 = "ENMNUM01"; + String ENMSTR01 = "ENMSTR01"; + String ERANAM01 = "ERANAM01"; + String ERAVDF01 = "ERAVDF01"; + String EXPRSE01 = "EXPRSE01"; + String EXPRSE02 = "EXPRSE02"; + String EXPTSF01 = "EXPTSF01"; + String EXPTSF02 = "EXPTSF02"; + String EXPTSF03 = "EXPTSF03"; + String FAILBS01 = "FAILBS01"; + String FAILBS02 = "FAILBS02"; + String FAILBS03 = "FAILBS03"; + String FAILDF01 = "FAILDF01"; + String FAILEX01 = "FAILEX01"; + String FAILEX02 = "FAILEX02"; + String FAILEX03 = "FAILEX03"; + String FAILEX04 = "FAILEX04"; + String FAILMB01 = "FAILMB01"; + String FAILMB02 = "FAILMB02"; + String FAILMB03 = "FAILMB03"; + String FAILMB04 = "FAILMB04"; + String FILARR01 = "FILARR01"; + String FLOTVL01 = "FLOTVL01"; + String FNCDEF01 = "FNCDEF01"; + String FNCDEF02 = "FNCDEF02"; + String FNCFAL01 = "FNCFAL01"; + String FNCNVK01 = "FNCNVK01"; + String FNCNVK02 = "FNCNVK02"; + String FNCNVK03 = "FNCNVK03"; + String FNCSIG01 = "FNCSIG01"; + String FNCSIG02 = "FNCSIG02"; + String FNDARR01 = "FNDARR01"; + String FNDSTR01 = "FNDSTR01"; + String FNDSTR02 = "FNDSTR02"; + String FNDSTR03 = "FNDSTR03"; + String FNSDEC01 = "FNSDEC01"; + String FNSDUP01 = "FNSDUP01"; + String FNSDUP02 = "FNSDUP02"; + String FNSNVK01 = "FNSNVK01"; + String FNSNVK02 = "FNSNVK02"; + String FNSNVK03 = "FNSNVK03"; + String FNSNVK04 = "FNSNVK04"; + String FNSRET01 = "FNSRET01"; + String FNTRGT01 = "FNTRGT01"; + String FORECH01 = "FORECH01"; + String FORSTM01 = "FORSTM01"; + String FRACDT01 = "FRACDT01"; + String FRACDT02 = "FRACDT02"; + String FRACDT03 = "FRACDT03"; + String FRACDT04 = "FRACDT04"; + String FRACDT05 = "FRACDT05"; + String FRACDT06 = "FRACDT06"; + String FRACDT07 = "FRACDT07"; + String HURNUM01 = "HURNUM01"; + String HURNUM02 = "HURNUM02"; + String HURVDF01 = "HURVDF01"; + String HURVDF02 = "HURVDF02"; + String HURVDF03 = "HURVDF03"; + String HURVDF04 = "HURVDF04"; + String IDXASN01 = "IDXASN01"; + String IDXUPD01 = "IDXUPD01"; + String IFSTMT01 = "IFSTMT01"; + String IFSTMT02 = "IFSTMT02"; + String IMPCLS01 = "IMPCLS01"; + String IMPCLS02 = "IMPCLS02"; + String IMPDUP01 = "IMPDUP01"; + String INCPRE01 = "INCPRE01"; + String INCPRE02 = "INCPRE02"; + String INCPRE03 = "INCPRE03"; + String INCPST01 = "INCPST01"; + String INCPST02 = "INCPST02"; + String INCPST03 = "INCPST03"; + String INSTCR01 = "INSTCR01"; + String INSTCR02 = "INSTCR02"; + String INSTCR03 = "INSTCR03"; + String INSTCR04 = "INSTCR04"; + String INTVAL01 = "INTVAL01"; + String INVLDT01 = "INVLDT01"; + String INVLDT02 = "INVLDT02"; + String IPV4CF01 = "IPV4CF01"; + String IPV6CF01 = "IPV6CF01"; + String IPVACF01 = "IPVACF01"; + String IPVACF02 = "IPVACF02"; + String IPVACF03 = "IPVACF03"; + String ITERSE01 = "ITERSE01"; + String JSNLEX01 = "JSNLEX01"; + String JSNPRS01 = "JSNPRS01"; + String KEYFND01 = "KEYFND01"; + String LENARR01 = "LENARR01"; + String LENARR02 = "LENARR02"; + String LENARR03 = "LENARR03"; + String LENARR04 = "LENARR04"; + String LENARR05 = "LENARR05"; + String LENOBJ01 = "LENOBJ01"; + String LENOBJ02 = "LENOBJ02"; + String LENOBJ03 = "LENOBJ03"; + String LENOBJ04 = "LENOBJ04"; + String LENOBJ05 = "LENOBJ05"; + String LENSTR01 = "LENSTR01"; + String LENSTR02 = "LENSTR02"; + String LENSTR03 = "LENSTR03"; + String LENSTR04 = "LENSTR04"; + String LENSTR05 = "LENSTR05"; + String LEXRDT01 = "LEXRDT01"; + String MAXICF01 = "MAXICF01"; + String MAXICF02 = "MAXICF02"; + String MAXICF03 = "MAXICF03"; + String MINICF01 = "MINICF01"; + String MINICF02 = "MINICF02"; + String MINICF03 = "MINICF03"; + String MNTNUM01 = "MNTNUM01"; + String MNTNUM02 = "MNTNUM02"; + String MNTVDF01 = "MNTVDF01"; + String MODASN01 = "MODASN01"; + String MODASN02 = "MODASN02"; + String MODASN03 = "MODASN03"; + String MONNAM01 = "MONNAM01"; + String MONNAM02 = "MONNAM02"; + String MONNUM01 = "MONNUM01"; + String MONNUM02 = "MONNUM02"; + String MONVDF01 = "MONVDF01"; + String MTHNVK01 = "MTHNVK01"; + String MTHNVK02 = "MTHNVK02"; + String MULASN01 = "MULASN01"; + String MULASN02 = "MULASN02"; + String MULASN03 = "MULASN03"; + String NEGICF01 = "NEGICF01"; + String NEGICF02 = "NEGICF02"; + String OBJLIT01 = "OBJLIT01"; + String OPADDT01 = "OPADDT01"; + String OPADDT02 = "OPADDT02"; + String OPANDL01 = "OPANDL01"; + String OPDIVD01 = "OPDIVD01"; + String OPDIVD02 = "OPDIVD02"; + String OPEQUL01 = "OPEQUL01"; + String OPMINS01 = "OPMINS01"; + String OPMINS02 = "OPMINS02"; + String OPMODU01 = "OPMODU01"; + String OPMODU02 = "OPMODU02"; + String OPMULT01 = "OPMULT01"; + String OPMULT02 = "OPMULT02"; + String OPNEQL01 = "OPNEQL01"; + String OPNOTL01 = "OPNOTL01"; + String OPORLG01 = "OPORLG01"; + String OPPLUS01 = "OPPLUS01"; + String OPPLUS02 = "OPPLUS02"; + String OPPRTY01 = "OPPRTY01"; + String OPPRTY02 = "OPPRTY02"; + String OPPRTY03 = "OPPRTY03"; + String OPPRTY04 = "OPPRTY04"; + String OPRNGT01 = "OPRNGT01"; + String OPRNGT02 = "OPRNGT02"; + String OPRNGT03 = "OPRNGT03"; + String OPRNGT04 = "OPRNGT04"; + String OPRNGT05 = "OPRNGT05"; + String OPRNGT06 = "OPRNGT06"; + String OPSUBT01 = "OPSUBT01"; + String OPSUBT02 = "OPSUBT02"; + String PARMSF01 = "PARMSF01"; + String PHONCF01 = "PHONCF01"; + String POSICF01 = "POSICF01"; + String POSICF02 = "POSICF02"; + String POWNUM01 = "POWNUM01"; + String PRGCHK01 = "PRGCHK01"; + String PRGCHK02 = "PRGCHK02"; + String PRGDUP01 = "PRGDUP01"; + String PROPMS01 = "PROPMS01"; + String PROPMS02 = "PROPMS02"; + String PRTDUP01 = "PRTDUP01"; + String PRTDUP02 = "PRTDUP02"; + String PRTFND01 = "PRTFND01"; + String PRTORD01 = "PRTORD01"; + String PRTUDF01 = "PRTUDF01"; + String RECVER01 = "RECVER01"; + String RECVER02 = "RECVER02"; + String RECVER03 = "RECVER03"; + String REDBKT01 = "REDBKT01"; + String REDBKT02 = "REDBKT02"; + String REDIDX01 = "REDIDX01"; + String REDRNG01 = "REDRNG01"; + String REGXCF01 = "REGXCF01"; + String RETNSE01 = "RETNSE01"; + String RETNSE02 = "RETNSE02"; + String RETNSE03 = "RETNSE03"; + String RNDEND01 = "RNDEND01"; + String RNDEND02 = "RNDEND02"; + String RNDEND03 = "RNDEND03"; + String RNDEND04 = "RNDEND04"; + String RNDSTA01 = "RNDSTA01"; + String RNDSTA02 = "RNDSTA02"; + String RNDSTA03 = "RNDSTA03"; + String RNDSTA04 = "RNDSTA04"; + String RNGASN01 = "RNGASN01"; + String RNGNUM01 = "RNGNUM01"; + String RNGNUM02 = "RNGNUM02"; + String RNGNUM03 = "RNGNUM03"; + String RNGNUM04 = "RNGNUM04"; + String RNGUPD01 = "RNGUPD01"; + String ROARRY01 = "ROARRY01"; + String ROARRY02 = "ROARRY02"; + String ROARRY03 = "ROARRY03"; + String ROOBJT01 = "ROOBJT01"; + String ROOBJT02 = "ROOBJT02"; + String SCMLEX01 = "SCMLEX01"; + String SCMPRS01 = "SCMPRS01"; + String SECNUM01 = "SECNUM01"; + String SECNUM02 = "SECNUM02"; + String SECVDF01 = "SECVDF01"; + String SRPTSE01 = "SRPTSE01"; + String SRPTSE02 = "SRPTSE02"; + String STRASN01 = "STRASN01"; + String STRIDX01 = "STRIDX01"; + String STRIDX02 = "STRIDX02"; + String STRRNG01 = "STRRNG01"; + String STRRNG02 = "STRRNG02"; + String STRRNG03 = "STRRNG03"; + String STRTDT01 = "STRTDT01"; + String STRTDT02 = "STRTDT02"; + String STRUPD01 = "STRUPD01"; + String STRVAL01 = "STRVAL01"; + String SUBASN01 = "SUBASN01"; + String SUBASN02 = "SUBASN02"; + String SUBASN03 = "SUBASN03"; + String SYMBDT01 = "SYMBDT01"; + String TEXTDT01 = "TEXTDT01"; + String THRODF01 = "THRODF01"; + String THROSE01 = "THROSE01"; + String TRGTSE01 = "TRGTSE01"; + String TRGTSE02 = "TRGTSE02"; + String TRYFSE01 = "TRYFSE01"; + String URLADR01 = "URLADR01"; + String URLADR02 = "URLADR02"; + String URLSCM01 = "URLSCM01"; + String URLSCM02 = "URLSCM02"; + String UTCTIM01 = "UTCTIM01"; + String UTCTIM02 = "UTCTIM02"; + String UTCTIM03 = "UTCTIM03"; + String UTCVDF01 = "UTCVDF01"; + String UTCVDF02 = "UTCVDF02"; + String VALDFL01 = "VALDFL01"; + String VALFND01 = "VALFND01"; + String VARDEC01 = "VARDEC01"; + String VARDEC02 = "VARDEC02"; + String VARDUP01 = "VARDUP01"; + String VARRES01 = "VARRES01"; + String VARRES02 = "VARRES02"; + String WEKNAM01 = "WEKNAM01"; + String WEKNAM02 = "WEKNAM02"; + String WEKVDF01 = "WEKVDF01"; + String WHILSE01 = "WHILSE01"; + String WSPACE01 = "WSPACE01"; + String YARNUM01 = "YARNUM01"; + String YARNUM02 = "YARNUM02"; + String YARVDF01 = "YARVDF01"; } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/message/MessageFormatter.java b/src/main/java/com/relogiclabs/jschema/message/MessageFormatter.java index 187d0ee..39a88f0 100644 --- a/src/main/java/com/relogiclabs/jschema/message/MessageFormatter.java +++ b/src/main/java/com/relogiclabs/jschema/message/MessageFormatter.java @@ -2,10 +2,10 @@ import com.relogiclabs.jschema.node.JNode; import com.relogiclabs.jschema.tree.Context; -import com.relogiclabs.jschema.tree.Location; import lombok.Getter; import org.antlr.v4.runtime.Token; +import static com.relogiclabs.jschema.message.ContextDetail.getLocation; import static org.apache.commons.lang3.StringUtils.capitalize; @Getter @@ -17,6 +17,7 @@ public abstract class MessageFormatter { private static final String JSON_BASIC_FORMAT = "Json Input [%s]: %s"; private static final String SCHEMA_DETAIL_FORMAT = "Schema (Line: %s) [%s]: %s"; private static final String JSON_DETAIL_FORMAT = "Json (Line: %s) [%s]: %s"; + private static final String BOTH_DETAIL_FORMAT = "Schema (Line: %s) Json (Line: %s) [%s]: %s"; public static final MessageFormatter SCHEMA_VALIDATION = new ValidationFormatter( "Schema (Line: %s) Json (Line: %s) [%s]: %s.", @@ -52,10 +53,10 @@ private ValidationFormatter(String summary, String expected, String actual) { @Override public String format(ErrorDetail error, ExpectedDetail expected, ActualDetail actual) { - return getSummary().formatted(expected.getLocation(), actual.getLocation(), - error.getCode(), error.getMessage()) + - getExpected().formatted(capitalize(expected.getMessage())) + - getActual().formatted(actual.getMessage()); + return getSummary().formatted(expected.getLocation(), actual.getLocation(), + error.getCode(), error.getMessage()) + + getExpected().formatted(capitalize(expected.getMessage())) + + getActual().formatted(actual.getMessage()); } } @@ -66,58 +67,52 @@ public AssertionFormatter(String summary, String expected, String actual) { @Override public String format(ErrorDetail error, ExpectedDetail expected, ActualDetail actual) { - return getSummary().formatted(error.getCode(), error.getMessage()) + - getExpected().formatted(expected.getLocation(), expected.getMessage()) + - getActual().formatted(actual.getLocation(), actual.getMessage()); + return getSummary().formatted(error.getCode(), error.getMessage()) + + getExpected().formatted(expected.getLocation(), expected.getMessage()) + + getActual().formatted(actual.getLocation(), actual.getMessage()); } } public static ErrorDetail formatForSchema(String code, String message, JNode node) { - return formatForSchema(code, message, node != null? node.getContext().getLocation() : null); + return formatForSchema(code, message, node != null? node.getContext().getToken() : null); } public static ErrorDetail formatForSchema(String code, String message, Context context) { - return formatForSchema(code, message, context != null? context.getLocation() : null); - } - - public static ErrorDetail formatForSchema(String code, String message, Location location) { - return location == null - ? createError(code, SCHEMA_BASIC_FORMAT, message) - : createError(code, SCHEMA_DETAIL_FORMAT, message, location); + return formatForSchema(code, message, context != null? context.getToken() : null); } public static ErrorDetail formatForSchema(String code, String message, Token token) { return token == null - ? createError(code, SCHEMA_BASIC_FORMAT, message) - : createError(code, SCHEMA_DETAIL_FORMAT, message, token); + ? createError(code, SCHEMA_BASIC_FORMAT, message) + : createError(code, SCHEMA_DETAIL_FORMAT, message, token); } public static ErrorDetail formatForJson(String code, String message, JNode node) { - return formatForJson(code, message, node != null? node.getContext().getLocation() : null); + return formatForJson(code, message, node != null? node.getContext().getToken() : null); } public static ErrorDetail formatForJson(String code, String message, Context context) { - return formatForJson(code, message, context != null? context.getLocation() : null); + return formatForJson(code, message, context != null? context.getToken() : null); } - public static ErrorDetail formatForJson(String code, String message, Location location) { - return location == null - ? createError(code, JSON_BASIC_FORMAT, message) - : createError(code, JSON_DETAIL_FORMAT, message, location); + public static ErrorDetail formatForJson(String code, String message, Token token) { + return token == null + ? createError(code, JSON_BASIC_FORMAT, message) + : createError(code, JSON_DETAIL_FORMAT, message, token); } - private static ErrorDetail createError(String code, String format, String message) { - return new ErrorDetail(code, format.formatted(code, message)); + public static ErrorDetail formatForBoth(String code, String message, JNode schemaNode, + JNode jsonNode) { + return new ErrorDetail(code, BOTH_DETAIL_FORMAT.formatted( + getLocation(schemaNode.getContext().getToken()), + getLocation(jsonNode.getContext().getToken()), code, message)); } - private static ErrorDetail createError(String code, String format, String message, - Location location) { - return new ErrorDetail(code, format.formatted(location, code, message)); + private static ErrorDetail createError(String code, String format, String message) { + return new ErrorDetail(code, format.formatted(code, message)); } - private static ErrorDetail createError(String code, String format, String message, - Token token) { - return new ErrorDetail(code, format.formatted(token.getLine() + ":" - + token.getCharPositionInLine(), code, message)); + private static ErrorDetail createError(String code, String format, String message, Token token) { + return new ErrorDetail(code, format.formatted(getLocation(token), code, message)); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/tree/Context.java b/src/main/java/com/relogiclabs/jschema/tree/Context.java index 3a1f71e..1d2ae13 100644 --- a/src/main/java/com/relogiclabs/jschema/tree/Context.java +++ b/src/main/java/com/relogiclabs/jschema/tree/Context.java @@ -2,21 +2,19 @@ import lombok.Getter; import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.Token; @Getter public final class Context { private final ParserRuleContext parser; private final RuntimeContext runtime; - private final Location location; public Context(ParserRuleContext parser, RuntimeContext runtime) { this.parser = parser; this.runtime = runtime; - this.location = getLocation(parser); } - private Location getLocation(ParserRuleContext parser) { - var token = parser.getStart(); - return new Location(token.getLine(), token.getCharPositionInLine()); + public Token getToken() { + return parser.getStart(); } } \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/tree/FunctionRegistry.java b/src/main/java/com/relogiclabs/jschema/tree/FunctionRegistry.java index 2ca693c..9b5b80e 100644 --- a/src/main/java/com/relogiclabs/jschema/tree/FunctionRegistry.java +++ b/src/main/java/com/relogiclabs/jschema/tree/FunctionRegistry.java @@ -2,8 +2,8 @@ import com.relogiclabs.jschema.exception.DuplicateImportException; import com.relogiclabs.jschema.exception.FunctionNotFoundException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.exception.InvalidImportException; -import com.relogiclabs.jschema.exception.JsonSchemaException; import com.relogiclabs.jschema.exception.NoClassFoundException; import com.relogiclabs.jschema.function.FunctionProvider; import com.relogiclabs.jschema.function.FutureFunction; @@ -26,12 +26,12 @@ import static com.relogiclabs.jschema.internal.message.MessageHelper.getTypeName; import static com.relogiclabs.jschema.internal.util.CommonHelper.getDerived; -import static com.relogiclabs.jschema.message.ErrorCode.CLAS01; -import static com.relogiclabs.jschema.message.ErrorCode.CLAS02; -import static com.relogiclabs.jschema.message.ErrorCode.CLAS03; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC03; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC04; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC05; +import static com.relogiclabs.jschema.message.ErrorCode.FNCDEF01; +import static com.relogiclabs.jschema.message.ErrorCode.FNCDEF02; +import static com.relogiclabs.jschema.message.ErrorCode.FNTRGT01; +import static com.relogiclabs.jschema.message.ErrorCode.IMPCLS01; +import static com.relogiclabs.jschema.message.ErrorCode.IMPCLS02; +import static com.relogiclabs.jschema.message.ErrorCode.IMPDUP01; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; public final class FunctionRegistry { @@ -52,22 +52,26 @@ public JImport addClass(JImport importNode) { @SuppressWarnings("unchecked") public void addClass(String className, Context context) { - if(!imports.add(className)) throw new DuplicateImportException(formatForSchema(CLAS01, + if(!imports.add(className)) throw new DuplicateImportException(formatForSchema(IMPDUP01, "Class already imported " + className, context)); Class providerImpl; try { providerImpl = Class.forName(className); } catch(ClassNotFoundException ex) { - throw new NoClassFoundException(formatForSchema(CLAS02, "Not found " + throw new NoClassFoundException(formatForSchema(IMPCLS01, "Not found class " + className, context)); } var providerBase = FunctionProvider.class; if(!providerBase.isAssignableFrom(providerImpl)) - throw new InvalidImportException(formatForSchema(CLAS03, providerImpl.getName() + throw new InvalidImportException(formatForSchema(IMPCLS02, providerImpl.getName() + " needs to inherit " + providerBase.getName(), context)); functions.mergeWith(FunctionLoader.load((Class) providerImpl, context)); } + public void addFunction(ScriptFunction function) { + functions.add(function); + } + private boolean processResult(Object result) { return result instanceof FutureFunction future ? runtime.addFuture(future) @@ -92,13 +96,14 @@ public boolean invokeFunction(JFunction caller, JNode target) { mismatchTarget = targetType; } if(mismatchTarget != null) - return fail(new JsonSchemaException(new ErrorDetail(FUNC03, - "Function " + caller.getOutline() + " is incompatible with the target data type"), - new ExpectedDetail(caller, "applying to a supported data type such as " + return fail(new FunctionValidationException(new ErrorDetail(FNTRGT01, + "Function " + caller.getOutline() + " is incompatible with target data type"), + new ExpectedDetail(caller, "a supported data type such as " + getTypeName(mismatchTarget)), - new ActualDetail(target, "applied to an unsupported data type " + new ActualDetail(target, "found unsupported target " + getTypeName(target.getClass()) + " of " + target.getOutline()))); - return fail(new FunctionNotFoundException(formatForSchema(FUNC04, caller.getOutline(), caller))); + return fail(new FunctionNotFoundException(formatForSchema(FNCDEF02, + "Not found function " + caller.getOutline(), caller))); } private List getFunctions(JFunction caller) { @@ -113,14 +118,10 @@ private List getFunctions(JFunction caller) { if(list != null) return list; list = CoreLibrary.getFunctions(key2); if(list != null) return list; - throw new FunctionNotFoundException(formatForSchema(FUNC05, + throw new FunctionNotFoundException(formatForSchema(FNCDEF01, "Not found function " + caller.getOutline(), caller)); } - public void addFunction(ScriptFunction function) { - functions.add(function); - } - private static List addTarget(List arguments, JNode target) { arguments.add(0, getDerived(target)); return arguments; diff --git a/src/main/java/com/relogiclabs/jschema/tree/Location.java b/src/main/java/com/relogiclabs/jschema/tree/Location.java deleted file mode 100644 index 17480fd..0000000 --- a/src/main/java/com/relogiclabs/jschema/tree/Location.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.relogiclabs.jschema.tree; - -import org.antlr.v4.runtime.Token; - -public record Location(int line, int column) { - public static Location from(Token token) { - return new Location(token.getLine(), token.getCharPositionInLine()); - } - - @Override - public String toString() { - return line + ":" + column; - } -} \ No newline at end of file diff --git a/src/main/java/com/relogiclabs/jschema/tree/RuntimeContext.java b/src/main/java/com/relogiclabs/jschema/tree/RuntimeContext.java index 738623f..280616e 100644 --- a/src/main/java/com/relogiclabs/jschema/tree/RuntimeContext.java +++ b/src/main/java/com/relogiclabs/jschema/tree/RuntimeContext.java @@ -1,6 +1,6 @@ package com.relogiclabs.jschema.tree; -import com.relogiclabs.jschema.exception.DuplicateDefinitionException; +import com.relogiclabs.jschema.exception.DuplicateAliasException; import com.relogiclabs.jschema.function.FutureFunction; import com.relogiclabs.jschema.internal.engine.ScriptGlobalScope; import com.relogiclabs.jschema.message.MessageFormatter; @@ -10,10 +10,11 @@ import lombok.Getter; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; -import java.util.UUID; -import static com.relogiclabs.jschema.message.ErrorCode.DEFI01; +import static com.relogiclabs.jschema.message.ErrorCode.ALSDUP01; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; @Getter @@ -22,7 +23,7 @@ public final class RuntimeContext { private final PragmaRegistry pragmas; private final Map definitions; private final ExceptionRegistry exceptions; - private final Map futures; + private final List futures; private final ReceiverRegistry receivers; private final Map storage; private final MessageFormatter messageFormatter; @@ -36,13 +37,13 @@ public RuntimeContext(MessageFormatter messageFormatter, boolean throwException) this.exceptions = new ExceptionRegistry(throwException); this.receivers = new ReceiverRegistry(); this.storage = new HashMap<>(); - this.futures = new HashMap<>(); + this.futures = new LinkedList<>(); this.scriptGlobalScope = new ScriptGlobalScope(this); } public JDefinition addDefinition(JDefinition definition) { var previous = definitions.get(definition.getAlias()); - if(previous != null) throw new DuplicateDefinitionException(formatForSchema(DEFI01, + if(previous != null) throw new DuplicateAliasException(formatForSchema(ALSDUP01, "Duplicate definition of '" + definition.getAlias() + "' is found and already defined as " + previous.getOutline(), definition.getContext())); definitions.put(definition.getAlias(), definition.getValidator()); @@ -54,12 +55,12 @@ public boolean areEqual(double value1, double value2) { } public boolean addFuture(FutureFunction future) { - return futures.put(UUID.randomUUID().toString(), future) == null; + return futures.add(future); } public boolean invokeFutures() { var result = true; - for(var f : futures.values()) result &= f.invoke(); + for(var f : futures) result &= f.invoke(); return result; } From a29de051ba444f1c27c222ea42dc0be3b76016e7 Mon Sep 17 00:00:00 2001 From: Zahid Hossain <60911932+zhossain-info@users.noreply.github.com> Date: Sat, 17 Aug 2024 10:24:00 +0600 Subject: [PATCH 6/7] Add and update test cases --- .../internal/builder/JPragmaBuilder.java | 8 +- .../internal/library/LibraryFunctions2.java | 16 +- .../internal/time/DateTimeContext.java | 60 ++-- .../jschema/message/ErrorCode.java | 42 +-- .../test/external/ExternalFunctions.java | 89 +++-- .../test/negative/AggregatedTests.java | 87 ++++- .../jschema/test/negative/ArrayTests.java | 85 ++--- .../jschema/test/negative/BooleanTests.java | 33 +- .../jschema/test/negative/ComponentTests.java | 16 +- .../jschema/test/negative/DataTypeTests.java | 67 ++-- .../jschema/test/negative/DateTimeTests.java | 330 +++++++++--------- .../jschema/test/negative/FunctionTests.java | 46 +-- .../jschema/test/negative/IntegerTests.java | 64 ++-- .../jschema/test/negative/NumberTests.java | 80 ++--- .../jschema/test/negative/ObjectTests.java | 125 +++---- .../jschema/test/negative/OtherTests.java | 64 ++-- .../jschema/test/negative/PragmaTests.java | 266 ++++++++++---- .../jschema/test/negative/ReceiverTests.java | 38 +- .../test/negative/ScriptBasicTests.java | 145 +++++--- .../test/negative/ScriptFunctionTests.java | 163 ++++++--- .../test/negative/ScriptGeneralTests.java | 58 +-- .../test/negative/ScriptLiteralTests.java | 123 +++++++ .../jschema/test/negative/StringTests.java | 173 +++++---- .../test/positive/AggregatedTests.java | 21 +- .../jschema/test/positive/ComponentTests.java | 12 +- .../jschema/test/positive/ReceiverTests.java | 4 +- .../test/positive/ScriptBasicTests.java | 6 +- .../test/positive/ScriptFunctionTests.java | 34 +- .../test/positive/ScriptGeneralTests.java | 18 +- .../test/positive/ScriptLiteralTests.java | 36 +- .../jschema/test/positive/StringTests.java | 27 ++ 31 files changed, 1419 insertions(+), 917 deletions(-) create mode 100644 src/test/java/com/relogiclabs/jschema/test/negative/ScriptLiteralTests.java diff --git a/src/main/java/com/relogiclabs/jschema/internal/builder/JPragmaBuilder.java b/src/main/java/com/relogiclabs/jschema/internal/builder/JPragmaBuilder.java index f2fb3c3..4646044 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/builder/JPragmaBuilder.java +++ b/src/main/java/com/relogiclabs/jschema/internal/builder/JPragmaBuilder.java @@ -9,8 +9,8 @@ import lombok.Setter; import lombok.experimental.Accessors; -import static com.relogiclabs.jschema.message.ErrorCode.PRGCHK01; -import static com.relogiclabs.jschema.message.ErrorCode.PRGCHK02; +import static com.relogiclabs.jschema.message.ErrorCode.PRGDEF01; +import static com.relogiclabs.jschema.message.ErrorCode.PRGDEF02; import static com.relogiclabs.jschema.message.MessageFormatter.formatForSchema; @Getter @Setter @@ -22,10 +22,10 @@ public final class JPragmaBuilder extends JNodeBuilder { private void checkPragma() { var descriptor = PragmaDescriptor.from(name); if (descriptor == null) - throw new PragmaNotFoundException(formatForSchema(PRGCHK01, "Invalid pragma '" + throw new PragmaNotFoundException(formatForSchema(PRGDEF01, "Invalid pragma '" + name + "' with value " + value.getOutline() + " found", context())); if (!descriptor.matchType(value.getClass())) - throw new InvalidPragmaValueException(formatForSchema(PRGCHK02, "Invalid value " + throw new InvalidPragmaValueException(formatForSchema(PRGDEF02, "Invalid value " + value.getOutline() + " for pragma '" + name + "' found", value)); } diff --git a/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions2.java b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions2.java index 688759e..017ed1b 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions2.java +++ b/src/main/java/com/relogiclabs/jschema/internal/library/LibraryFunctions2.java @@ -35,10 +35,10 @@ import static com.relogiclabs.jschema.message.ErrorCode.FAILEX02; import static com.relogiclabs.jschema.message.ErrorCode.FAILEX03; import static com.relogiclabs.jschema.message.ErrorCode.FAILEX04; -import static com.relogiclabs.jschema.message.ErrorCode.FAILMB01; -import static com.relogiclabs.jschema.message.ErrorCode.FAILMB02; -import static com.relogiclabs.jschema.message.ErrorCode.FAILMB03; -import static com.relogiclabs.jschema.message.ErrorCode.FAILMB04; +import static com.relogiclabs.jschema.message.ErrorCode.FAILMV01; +import static com.relogiclabs.jschema.message.ErrorCode.FAILMV02; +import static com.relogiclabs.jschema.message.ErrorCode.FAILMV03; +import static com.relogiclabs.jschema.message.ErrorCode.FAILMV04; import static com.relogiclabs.jschema.message.MessageFormatter.formatForBoth; import static com.relogiclabs.jschema.type.EValue.VOID; @@ -113,26 +113,26 @@ static EValue failFunction4(ScriptScope scope, List arguments) { try { expectedNode = castMember(expected, Node_Id, JNode.class); } catch(InvalidArgumentException e) { - e.setContext(FAILMB01, Expected_Id); + e.setContext(FAILMV01, Expected_Id); throw e.failWithFunctionException(); } try { expectedMessage = castMember(expected, Message_Id, EString.class).getValue(); } catch(InvalidArgumentException e) { - e.setContext(FAILMB02, Expected_Id); + e.setContext(FAILMV02, Expected_Id); throw e.failWithFunctionException(); } try { actualNode = castMember(actual, Node_Id, JNode.class); } catch(InvalidArgumentException e) { - e.setContext(FAILMB03, Actual_Id); + e.setContext(FAILMV03, Actual_Id); throw e.failWithFunctionException(); } try { actualMessage = castMember(actual, Message_Id, EString.class).getValue(); } catch(InvalidArgumentException e) { - e.setContext(FAILMB04, Actual_Id); + e.setContext(FAILMV04, Actual_Id); throw e.failWithFunctionException(); } fail(scope, new FunctionValidationException( diff --git a/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeContext.java b/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeContext.java index 14b349d..59b8ce6 100644 --- a/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeContext.java +++ b/src/main/java/com/relogiclabs/jschema/internal/time/DateTimeContext.java @@ -11,23 +11,23 @@ import java.util.HashMap; import java.util.Map; -import static com.relogiclabs.jschema.message.ErrorCode.AMPMVD01; +import static com.relogiclabs.jschema.message.ErrorCode.AMPMMS01; import static com.relogiclabs.jschema.message.ErrorCode.CNFLDT01; -import static com.relogiclabs.jschema.message.ErrorCode.DAYVDF01; -import static com.relogiclabs.jschema.message.ErrorCode.DAYVDF02; -import static com.relogiclabs.jschema.message.ErrorCode.ERAVDF01; -import static com.relogiclabs.jschema.message.ErrorCode.HURVDF01; -import static com.relogiclabs.jschema.message.ErrorCode.HURVDF02; -import static com.relogiclabs.jschema.message.ErrorCode.HURVDF03; -import static com.relogiclabs.jschema.message.ErrorCode.HURVDF04; +import static com.relogiclabs.jschema.message.ErrorCode.DAYRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.DAYRNG02; +import static com.relogiclabs.jschema.message.ErrorCode.ERANMS01; +import static com.relogiclabs.jschema.message.ErrorCode.HURRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.HURRNG02; +import static com.relogiclabs.jschema.message.ErrorCode.HURRNG03; +import static com.relogiclabs.jschema.message.ErrorCode.HURRNG04; import static com.relogiclabs.jschema.message.ErrorCode.INVLDT02; -import static com.relogiclabs.jschema.message.ErrorCode.MNTVDF01; -import static com.relogiclabs.jschema.message.ErrorCode.MONVDF01; -import static com.relogiclabs.jschema.message.ErrorCode.SECVDF01; -import static com.relogiclabs.jschema.message.ErrorCode.UTCVDF01; -import static com.relogiclabs.jschema.message.ErrorCode.UTCVDF02; -import static com.relogiclabs.jschema.message.ErrorCode.WEKVDF01; -import static com.relogiclabs.jschema.message.ErrorCode.YARVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.MNTRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.MONRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.SECRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.UTCRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.UTCRNG02; +import static com.relogiclabs.jschema.message.ErrorCode.WEKDMS01; +import static com.relogiclabs.jschema.message.ErrorCode.YARRNG01; import static com.relogiclabs.jschema.time.JsonDateTime.UNSET; import static java.time.DayOfWeek.FRIDAY; import static java.time.DayOfWeek.MONDAY; @@ -105,13 +105,13 @@ public void setEra(String era) { var eraNum = switch(era.toUpperCase()) { case "BC" -> 1; case "AD" -> 2; - default -> throw failOnInvalidDateTime(ERAVDF01, "era input"); + default -> throw failOnInvalidDateTime(ERANMS01, "era input"); }; this.era = checkField(this.era, eraNum); } public void setYear(int year, int digitNum) { - if(year < 1 || year > 9999) throw failOnInvalidDateTime(YARVDF01, "year out of range"); + if(year < 1 || year > 9999) throw failOnInvalidDateTime(YARRNG01, "year out of range"); year = digitNum <= 2 ? toFourDigitYear(year) : year; this.year = checkField(this.year, year); } @@ -122,7 +122,7 @@ public void setMonth(String month) { } public void setMonth(int month) { - if(month < 1 || month > 12) throw failOnInvalidDateTime(MONVDF01, "month out of range"); + if(month < 1 || month > 12) throw failOnInvalidDateTime(MONRNG01, "month out of range"); this.month = checkField(this.month, month); } @@ -132,7 +132,7 @@ public void setWeekday(String weekday) { } public void setDay(int day) { - if(day < 1 || day > 31) throw failOnInvalidDateTime(DAYVDF01, "day out of range"); + if(day < 1 || day > 31) throw failOnInvalidDateTime(DAYRNG01, "day out of range"); this.day = checkField(this.day, day); } @@ -140,28 +140,28 @@ public void setAmPm(String amPm) { var amPmNum = switch(amPm.toLowerCase()) { case "am" -> 1; case "pm" -> 2; - default -> throw failOnInvalidDateTime(AMPMVD01, "hour AM/PM input"); + default -> throw failOnInvalidDateTime(AMPMMS01, "hour AM/PM input"); }; if(hour != UNSET && (hour < 1 || hour > 12)) - throw failOnInvalidDateTime(HURVDF01, "hour out of range for AM/PM"); + throw failOnInvalidDateTime(HURRNG01, "hour out of range for AM/PM"); this.amPm = checkField(this.amPm, amPmNum); } public void setHour(int hour) { if(amPm != UNSET && (this.hour < 1 || this.hour > 12)) - throw failOnInvalidDateTime(HURVDF02, "hour out of range for AM/PM"); + throw failOnInvalidDateTime(HURRNG02, "hour out of range for AM/PM"); if(hour < 0 || hour > 23) - throw failOnInvalidDateTime(HURVDF03, "hour out of range"); + throw failOnInvalidDateTime(HURRNG03, "hour out of range"); this.hour = checkField(this.hour, hour); } public void setMinute(int minute) { - if(minute < 0 || minute > 59) throw failOnInvalidDateTime(MNTVDF01, "minute out of range"); + if(minute < 0 || minute > 59) throw failOnInvalidDateTime(MNTRNG01, "minute out of range"); this.minute = checkField(this.minute, minute); } public void setSecond(int second) { - if(second < 0 || second > 59) throw failOnInvalidDateTime(SECVDF01, "second out of range"); + if(second < 0 || second > 59) throw failOnInvalidDateTime(SECRNG01, "second out of range"); this.second = checkField(this.second, second); } @@ -170,9 +170,9 @@ public void setFraction(int fraction) { } public void setUtcOffset(int hour, int minute) { - if(hour < -12 || hour > 12) throw failOnInvalidDateTime(UTCVDF01, + if(hour < -12 || hour > 12) throw failOnInvalidDateTime(UTCRNG01, "UTC offset hour out of range"); - if(minute < 0 || minute > 59) throw failOnInvalidDateTime(UTCVDF02, + if(minute < 0 || minute > 59) throw failOnInvalidDateTime(UTCRNG02, "UTC offset minute out of range"); utcHour = checkField(utcHour, hour); utcMinute = checkField(utcMinute, minute); @@ -194,14 +194,14 @@ public JsonDateTime validate() { if(isAllSet(year, month, day)) { DAYS_IN_MONTH[2] = isLeapYear(year)? 29 : 28; if(day < 1 || day > DAYS_IN_MONTH[month]) - throw failOnInvalidDateTime(DAYVDF02, "day out of range"); + throw failOnInvalidDateTime(DAYRNG02, "day out of range"); dateTime = new JsonDateTime(type, year, month, day); if(weekday != UNSET && dateTime.getDayOfWeek().getValue() != weekday) - throw failOnInvalidDateTime(WEKVDF01, "weekday not matched"); + throw failOnInvalidDateTime(WEKDMS01, "weekday not matched"); } if(isAllSet(hour, amPm)) convertTo24Hour(); if(hour != UNSET && (hour < 0 || hour > 23)) - throw failOnInvalidDateTime(HURVDF04, "hour out of range"); + throw failOnInvalidDateTime(HURRNG04, "hour out of range"); return new JsonDateTime(type, year, month, day, hour, minute, second, fraction, new JsonUtcOffset(utcHour, utcMinute)); } catch(InvalidDateTimeException e) { diff --git a/src/main/java/com/relogiclabs/jschema/message/ErrorCode.java b/src/main/java/com/relogiclabs/jschema/message/ErrorCode.java index 6a4e93f..5625912 100644 --- a/src/main/java/com/relogiclabs/jschema/message/ErrorCode.java +++ b/src/main/java/com/relogiclabs/jschema/message/ErrorCode.java @@ -14,7 +14,7 @@ public interface ErrorCode { String ALSDEF03 = "ALSDEF03"; String ALSDUP01 = "ALSDUP01"; String AMPMDT01 = "AMPMDT01"; - String AMPMVD01 = "AMPMVD01"; + String AMPMMS01 = "AMPMMS01"; String ARNSRT01 = "ARNSRT01"; String ARNSRT02 = "ARNSRT02"; String ARRASN01 = "ARRASN01"; @@ -53,8 +53,8 @@ public interface ErrorCode { String CNFLDT01 = "CNFLDT01"; String DAYNUM01 = "DAYNUM01"; String DAYNUM02 = "DAYNUM02"; - String DAYVDF01 = "DAYVDF01"; - String DAYVDF02 = "DAYVDF02"; + String DAYRNG01 = "DAYRNG01"; + String DAYRNG02 = "DAYRNG02"; String DECPRE01 = "DECPRE01"; String DECPRE02 = "DECPRE02"; String DECPRE03 = "DECPRE03"; @@ -82,7 +82,7 @@ public interface ErrorCode { String ENMNUM01 = "ENMNUM01"; String ENMSTR01 = "ENMSTR01"; String ERANAM01 = "ERANAM01"; - String ERAVDF01 = "ERAVDF01"; + String ERANMS01 = "ERANMS01"; String EXPRSE01 = "EXPRSE01"; String EXPRSE02 = "EXPRSE02"; String EXPTSF01 = "EXPTSF01"; @@ -96,10 +96,10 @@ public interface ErrorCode { String FAILEX02 = "FAILEX02"; String FAILEX03 = "FAILEX03"; String FAILEX04 = "FAILEX04"; - String FAILMB01 = "FAILMB01"; - String FAILMB02 = "FAILMB02"; - String FAILMB03 = "FAILMB03"; - String FAILMB04 = "FAILMB04"; + String FAILMV01 = "FAILMV01"; + String FAILMV02 = "FAILMV02"; + String FAILMV03 = "FAILMV03"; + String FAILMV04 = "FAILMV04"; String FILARR01 = "FILARR01"; String FLOTVL01 = "FLOTVL01"; String FNCDEF01 = "FNCDEF01"; @@ -134,10 +134,10 @@ public interface ErrorCode { String FRACDT07 = "FRACDT07"; String HURNUM01 = "HURNUM01"; String HURNUM02 = "HURNUM02"; - String HURVDF01 = "HURVDF01"; - String HURVDF02 = "HURVDF02"; - String HURVDF03 = "HURVDF03"; - String HURVDF04 = "HURVDF04"; + String HURRNG01 = "HURRNG01"; + String HURRNG02 = "HURRNG02"; + String HURRNG03 = "HURRNG03"; + String HURRNG04 = "HURRNG04"; String IDXASN01 = "IDXASN01"; String IDXUPD01 = "IDXUPD01"; String IFSTMT01 = "IFSTMT01"; @@ -191,7 +191,7 @@ public interface ErrorCode { String MINICF03 = "MINICF03"; String MNTNUM01 = "MNTNUM01"; String MNTNUM02 = "MNTNUM02"; - String MNTVDF01 = "MNTVDF01"; + String MNTRNG01 = "MNTRNG01"; String MODASN01 = "MODASN01"; String MODASN02 = "MODASN02"; String MODASN03 = "MODASN03"; @@ -199,7 +199,7 @@ public interface ErrorCode { String MONNAM02 = "MONNAM02"; String MONNUM01 = "MONNUM01"; String MONNUM02 = "MONNUM02"; - String MONVDF01 = "MONVDF01"; + String MONRNG01 = "MONRNG01"; String MTHNVK01 = "MTHNVK01"; String MTHNVK02 = "MTHNVK02"; String MULASN01 = "MULASN01"; @@ -242,8 +242,8 @@ public interface ErrorCode { String POSICF01 = "POSICF01"; String POSICF02 = "POSICF02"; String POWNUM01 = "POWNUM01"; - String PRGCHK01 = "PRGCHK01"; - String PRGCHK02 = "PRGCHK02"; + String PRGDEF01 = "PRGDEF01"; + String PRGDEF02 = "PRGDEF02"; String PRGDUP01 = "PRGDUP01"; String PROPMS01 = "PROPMS01"; String PROPMS02 = "PROPMS02"; @@ -286,7 +286,7 @@ public interface ErrorCode { String SCMPRS01 = "SCMPRS01"; String SECNUM01 = "SECNUM01"; String SECNUM02 = "SECNUM02"; - String SECVDF01 = "SECVDF01"; + String SECRNG01 = "SECRNG01"; String SRPTSE01 = "SRPTSE01"; String SRPTSE02 = "SRPTSE02"; String STRASN01 = "STRASN01"; @@ -313,11 +313,11 @@ public interface ErrorCode { String URLADR02 = "URLADR02"; String URLSCM01 = "URLSCM01"; String URLSCM02 = "URLSCM02"; + String UTCRNG01 = "UTCRNG01"; + String UTCRNG02 = "UTCRNG02"; String UTCTIM01 = "UTCTIM01"; String UTCTIM02 = "UTCTIM02"; String UTCTIM03 = "UTCTIM03"; - String UTCVDF01 = "UTCVDF01"; - String UTCVDF02 = "UTCVDF02"; String VALDFL01 = "VALDFL01"; String VALFND01 = "VALFND01"; String VARDEC01 = "VARDEC01"; @@ -325,12 +325,12 @@ public interface ErrorCode { String VARDUP01 = "VARDUP01"; String VARRES01 = "VARRES01"; String VARRES02 = "VARRES02"; + String WEKDMS01 = "WEKDMS01"; String WEKNAM01 = "WEKNAM01"; String WEKNAM02 = "WEKNAM02"; - String WEKVDF01 = "WEKVDF01"; String WHILSE01 = "WHILSE01"; String WSPACE01 = "WSPACE01"; String YARNUM01 = "YARNUM01"; String YARNUM02 = "YARNUM02"; - String YARVDF01 = "YARVDF01"; + String YARRNG01 = "YARRNG01"; } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/external/ExternalFunctions.java b/src/test/java/com/relogiclabs/jschema/test/external/ExternalFunctions.java index 840f882..6440ee5 100644 --- a/src/test/java/com/relogiclabs/jschema/test/external/ExternalFunctions.java +++ b/src/test/java/com/relogiclabs/jschema/test/external/ExternalFunctions.java @@ -1,8 +1,9 @@ package com.relogiclabs.jschema.test.external; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.function.FunctionProvider; import com.relogiclabs.jschema.function.FutureFunction; +import com.relogiclabs.jschema.internal.function.IPAddress; import com.relogiclabs.jschema.message.ActualDetail; import com.relogiclabs.jschema.message.ErrorDetail; import com.relogiclabs.jschema.message.ExpectedDetail; @@ -18,20 +19,24 @@ // Functions for positive (valid) test cases public class ExternalFunctions extends FunctionProvider { - public static final String EVENFUNC01 = "EVENFUNC01"; - public static final String ERRACCESS01 = "ERRACCESS01"; - public static final String CONDFUNC01 = "CONDFUNC01"; - public static final String CONDFUNC02 = "CONDFUNC02"; - public static final String SUMEQUAL01 = "SUMEQUAL01"; - public static final String MINMAX01 = "MINMAX01"; + public static final String EX_EVENFUNC01 = "EX_EVENFUNC01"; + public static final String EX_ERRACCESS01 = "EX_ERRACCESS01"; + public static final String EX_ERRORIP01 = "EX_ERRORIP01"; + public static final String EX_ERRORIP02 = "EX_ERRORIP02"; + public static final String EX_CONDFUNC01 = "EX_CONDFUNC01"; + public static final String EX_CONDFUNC02 = "EX_CONDFUNC02"; + public static final String EX_SUMEQUAL01 = "EX_SUMEQUAL01"; + public static final String EX_MINMAX01 = "EX_MINMAX01"; + + private static final String INTERNAL_IP_PREFIX = "0."; public boolean even(JNumber target) { // Precision loss is not considered here if(target.toDouble() % 2 == 0) return true; - return fail(new JsonSchemaException( - new ErrorDetail(EVENFUNC01, "Number is not even"), - new ExpectedDetail(caller, "even number"), - new ActualDetail(target, "number " + target + " is odd"))); + return fail(new FunctionValidationException( + new ErrorDetail(EX_EVENFUNC01, "Target number is not even"), + new ExpectedDetail(caller, "an even number"), + new ActualDetail(target, "target " + target + " is odd"))); } public boolean canTest(JNumber target, JString str1, JBoolean bool1, JNumber... args) { @@ -42,12 +47,28 @@ public boolean canTest(JNumber target, JString str1, JBoolean bool1, JNumber... return true; } - public boolean checkAccess(JInteger target, JReceiver userRole) { + public boolean checkDataAccess(JInteger target, JReceiver userRole) { var role = userRole.getValueNode().getValue(); - if(role.equals("user") && target.getValue() > 5) return fail(new JsonSchemaException( - new ErrorDetail(ERRACCESS01, "Data access incompatible with 'user' role"), - new ExpectedDetail(caller, "an access at most 5 for 'user' role"), - new ActualDetail(target, "found access " + target + " which is greater than 5"))); + if(role.equals("user") && target.getValue() > 5) + return fail(new FunctionValidationException( + new ErrorDetail(EX_ERRACCESS01, "Data access incompatible with 'user' role"), + new ExpectedDetail(caller, "an access at most 5 for 'user' role"), + new ActualDetail(target, "found access " + target + " that is greater than 5"))); + return true; + } + + public boolean checkIPAddress(JString target) { + if(!IPAddress.isValidIPv4(target.getValue())) + return fail(new FunctionValidationException( + new ErrorDetail(EX_ERRORIP01, "Target IP address invalid"), + new ExpectedDetail(caller, "a valid IP address"), + new ActualDetail(target, "found invalid target " + target))); + // Might have any IP restrictions depending on requirements + if(target.getValue().startsWith(INTERNAL_IP_PREFIX)) + return fail(new FunctionValidationException( + new ErrorDetail(EX_ERRORIP02, "Target IP address must not be in 0.0.0.0/8"), + new ExpectedDetail(caller, "a valid IP address not in 0.0.0.0/8"), + new ActualDetail(target, "found invalid target " + target))); return true; } @@ -55,23 +76,23 @@ public boolean condition(JInteger target, JReceiver receiver) { var threshold = receiver.getValueNode().getValue(); System.out.println("Received integer: " + threshold); if(threshold < target.getValue()) return true; - return fail(new JsonSchemaException( - new ErrorDetail(CONDFUNC01, "Number does not satisfy the condition"), + return fail(new FunctionValidationException( + new ErrorDetail(EX_CONDFUNC01, "Target number does not satisfy the condition"), new ExpectedDetail(caller, "a number > " + threshold + " of " + receiver), - new ActualDetail(target, "found number " + target + " <= " + threshold))); + new ActualDetail(target, "found target " + target + " <= " + threshold))); } - public boolean conditionAll(JInteger target, JReceiver receiver) { + public boolean conditionMany(JInteger target, JReceiver receiver) { var list = receiver.getValueNodes(); var values = join(list, ",", "[", "]"); System.out.println("Target: " + target); System.out.println("Received integers: " + values); boolean result = list.stream().allMatch(i -> i.getValue() < target.getValue()); if(result) return true; - return fail(new JsonSchemaException( - new ErrorDetail(CONDFUNC02, "Number does not satisfy the condition"), - new ExpectedDetail(caller, "a number > any of " + values + " of " + receiver), - new ActualDetail(target, "found number " + target + " <= some of " + values))); + return fail(new FunctionValidationException( + new ErrorDetail(EX_CONDFUNC02, "Target number does not satisfy the condition"), + new ExpectedDetail(caller, "a number > any of " + values + " in " + receiver), + new ActualDetail(target, "found target " + target + " <= some of " + values))); } public FutureFunction sumEqual(JInteger target, JReceiver receiver) { @@ -79,15 +100,15 @@ public FutureFunction sumEqual(JInteger target, JReceiver receiver) { var current = caller; return () -> { var values = receiver.getValueNodes(); - var expression = join(values, "+"); System.out.println("Target: " + target); + long sum = values.stream().mapToLong(JInteger::getValue).sum(); + var expression = join(values, " + ") + " = " + sum; System.out.println("Received values: " + expression); - long result = values.stream().mapToLong(JInteger::getValue).sum(); - if(result == target.getValue()) return true; - return fail(new JsonSchemaException( - new ErrorDetail(SUMEQUAL01, "Number != sum of " + expression + " = " + result), - new ExpectedDetail(current, "a number = sum of numbers " + result), - new ActualDetail(target, "found number " + target + " != " + result))); + if(sum == target.getValue()) return true; + return fail(new FunctionValidationException( + new ErrorDetail(EX_SUMEQUAL01, "Target number != sum of " + expression), + new ExpectedDetail(current, "a number = sum of numbers " + sum), + new ActualDetail(target, "found target " + target + " != " + sum))); }; } @@ -100,10 +121,10 @@ public FutureFunction minmax(JInteger target, JReceiver min, JReceiver max) { System.out.println("Target: " + target); System.out.println("Received min: " + intMin + ", max: " + intMax); if(target.getValue() >= intMin && target.getValue() <= intMax) return true; - return fail(new JsonSchemaException( - new ErrorDetail(MINMAX01, "Number is outside of range"), + return fail(new FunctionValidationException( + new ErrorDetail(EX_MINMAX01, "Target number is outside of range"), new ExpectedDetail(current, "a number in range [" + intMin + ", " + intMax + "]"), - new ActualDetail(target, "found number " + target + " not in range"))); + new ActualDetail(target, "found target " + target + " not in range"))); }; } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/AggregatedTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/AggregatedTests.java index 3da4387..af6dd51 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/AggregatedTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/AggregatedTests.java @@ -2,20 +2,22 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.DataTypeValidationException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.test.external.ExternalFunctions.ERRACCESS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.test.external.ExternalFunctions.EX_ERRACCESS01; +import static com.relogiclabs.jschema.test.external.ExternalFunctions.EX_ERRORIP02; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; public class AggregatedTests { @Test - public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown() { + public void When_AggregatedTestWithWrongDataType_ExceptionThrown() { var schema = """ - %title: "User Profile Response" + %title: "User Profile API Request" %version: "1.0.0-basic" %schema: { @@ -69,9 +71,9 @@ public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -113,7 +115,7 @@ public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown() { "role": @enum("user", "admin") #string &role, "isActive": #boolean, //user account current status "registeredAt": @time("DD-MM-YYYY hh:mm:ss") #string, - "dataAccess": @checkAccess(&role) #integer, + "dataAccess": @checkDataAccess(&role) #integer, "profile": { "firstName": @regex("[A-Za-z]{3,50}") #string, "lastName": @regex("[A-Za-z]{3,50}") #string, @@ -181,7 +183,7 @@ public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown() { "title": "Working with JSON in Java", "content": "Java provides great support for working with JSON...", "tags": [ - "CSharp", + "Java", "JSON", "tutorial" ] @@ -233,9 +235,57 @@ public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals(EX_ERRACCESS01, exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_AggregatedTestWithWrongIP_ExceptionThrown() { + var schema = + """ + %title: "Profile Dashboard Request" + %version: "1.0.0-IPTest" + %import: com.relogiclabs.jschema.test.external.ExternalFunctions + + %schema: + { + "user": { + /*username does not allow special characters*/ + "username": @regex("[a-z_]{3,30}") #string, + /*currently only one role is allowed by system*/ + "role": "user" #string &role, + "dataAccess": @checkDataAccess(&role) #integer, + "ipAddress": @checkIPAddress #string, + "profile": { + "firstName": @regex("[A-Za-z ]{3,50}") #string, + "lastName": @regex("[A-Za-z ]{3,50}") #string, + "dateOfBirth": #date + } + } + } + """; + var json = + """ + { + "user": { + "username": "johndoe", + "role": "user", + "dataAccess": 5, + "ipAddress": "0.192.168.1", + "profile": { + "firstName": "John", + "lastName": "Doe", + "dateOfBirth": "1911-06-17" + } + } + } + """; + JsonSchema.isValid(schema, json); + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ERRACCESS01, exception.getCode()); + assertEquals(EX_ERRORIP02, exception.getCode()); exception.printStackTrace(); } @@ -245,6 +295,7 @@ public void When_ExtendedAggregatedScriptTestWithInvalidAccess_ExceptionThrown() """ %title: "Extended User Profile Dashboard API Response" %version: "2.0.0-extended" + %import: com.relogiclabs.jschema.test.external.ExternalFunctions %pragma DateDataTypeFormat: "DD-MM-YYYY" %pragma TimeDataTypeFormat: "DD-MM-YYYY hh:mm:ss" @@ -283,6 +334,7 @@ public void When_ExtendedAggregatedScriptTestWithInvalidAccess_ExceptionThrown() "isActive": #boolean, //user account current status "registeredAt": @after("01-01-2010 00:00:00") #time, "dataAccess": @checkAccess(&role) #integer, + "ipAddress": @checkIPAddress #string, "profile": { "firstName": @regex("[A-Za-z]{3,50}") #string, "lastName": @regex("[A-Za-z]{3,50}") #string, @@ -316,9 +368,9 @@ future constraint checkAccess(role) { // Auto-unpacking turns the single-value '&role' array into the value itself // 'target' keyword refers to the target JSON value if(role == "user" && target > 5) return fail( - "ERRACCESS01", "Data access incompatible with 'user' role", + "EX_ERRACCESS01", "Data access incompatible with 'user' role", expected("an access at most 5 for 'user' role"), - actual("found access " + target + " which is greater than 5")); + actual("found access " + target + " that is greater than 5")); } } """; @@ -332,6 +384,7 @@ future constraint checkAccess(role) { "isActive": true, "registeredAt": "06-09-2023 15:10:30", "dataAccess": 6, + "ipAddress": "127.0.0.1", "profile": { "firstName": "John", "lastName": "Doe", @@ -361,7 +414,7 @@ future constraint checkAccess(role) { "title": "Working with JSON in Java", "content": "Java provides great support for working with JSON...", "tags": [ - "CSharp", + "Java", "JSON", "tutorial" ] @@ -399,7 +452,7 @@ future constraint checkAccess(role) { "price": 1299.99, "inStock": false, "specs": { - "cpu": "Ryzen 11", + "cpu": "Ryzen", "ram": "11GB", "storage": "11GB SSD" } @@ -413,9 +466,9 @@ future constraint checkAccess(role) { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ERRACCESS01, exception.getCode()); + assertEquals("EX_ERRACCESS01", exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/ArrayTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/ArrayTests.java index c52a59c..af5ca6f 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/ArrayTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/ArrayTests.java @@ -2,21 +2,22 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; +import com.relogiclabs.jschema.exception.DataTypeValidationException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.exception.JsonParserException; -import com.relogiclabs.jschema.exception.JsonSchemaException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN01; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN02; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN03; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN04; -import static com.relogiclabs.jschema.message.ErrorCode.ALEN05; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP06; -import static com.relogiclabs.jschema.message.ErrorCode.ELEM01; -import static com.relogiclabs.jschema.message.ErrorCode.ENUM02; -import static com.relogiclabs.jschema.message.ErrorCode.JPRS01; -import static com.relogiclabs.jschema.message.ErrorCode.NEMT02; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS02; +import static com.relogiclabs.jschema.message.ErrorCode.ELEMCF01; +import static com.relogiclabs.jschema.message.ErrorCode.EMPTCF02; +import static com.relogiclabs.jschema.message.ErrorCode.ENMNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.JSNPRS01; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR01; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR02; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR03; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR04; +import static com.relogiclabs.jschema.message.ErrorCode.LENARR05; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -27,9 +28,9 @@ public void When_JsonNotArray_ExceptionThrown() { var json = "10"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -52,9 +53,9 @@ public void When_JsonNotArrayInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -69,9 +70,9 @@ public void When_JsonNotArrayInArray_ExceptionThrown() { [{}, "value1", 10.5] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -86,9 +87,9 @@ public void When_NestedJsonNotArrayInArray_ExceptionThrown() { [true, "value1", false] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -107,9 +108,9 @@ public void When_NestedJsonNotArrayInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -119,9 +120,9 @@ public void When_ElementsWithWrongArray_ExceptionThrown() { var json = "[5, 10, 15, 20, 25]"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ELEM01, exception.getCode()); + assertEquals(ELEMCF01, exception.getCode()); exception.printStackTrace(); } @@ -131,9 +132,9 @@ public void When_NestedElementsWithWrongArrayInArray_ExceptionThrown() { var json = "[[5, 10], [], [5, 10, 15, 20]]"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ELEM01, exception.getCode()); + assertEquals(ELEMCF01, exception.getCode()); exception.printStackTrace(); } @@ -149,12 +150,12 @@ public void When_EnumWithWrongValueInArray_ExceptionThrown() { """; var json = """ - [11, 102, "efg"] + [11, 102, "efg"] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ENUM02, exception.getCode()); + assertEquals(ENMNUM01, exception.getCode()); exception.printStackTrace(); } @@ -166,7 +167,7 @@ public void When_InvalidJsonInArray_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(JsonParserException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(JPRS01, exception.getCode()); + assertEquals(JSNPRS01, exception.getCode()); exception.printStackTrace(); } @@ -187,9 +188,9 @@ public void When_EmptyArrayInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(NEMT02, exception.getCode()); + assertEquals(EMPTCF02, exception.getCode()); exception.printStackTrace(); } @@ -207,9 +208,9 @@ public void When_JsonWrongLengthInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ALEN01, exception.getCode()); + assertEquals(LENARR01, exception.getCode()); exception.printStackTrace(); } @@ -227,9 +228,9 @@ public void When_JsonWrongMinimumLengthInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ALEN02, exception.getCode()); + assertEquals(LENARR02, exception.getCode()); exception.printStackTrace(); } @@ -247,9 +248,9 @@ public void When_JsonWrongMaximumLengthInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ALEN03, exception.getCode()); + assertEquals(LENARR03, exception.getCode()); exception.printStackTrace(); } @@ -267,9 +268,9 @@ public void When_JsonWrongMinimumLengthWithUndefinedInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ALEN04, exception.getCode()); + assertEquals(LENARR04, exception.getCode()); exception.printStackTrace(); } @@ -287,9 +288,9 @@ public void When_JsonWrongMaximumLengthWithUndefinedInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ALEN05, exception.getCode()); + assertEquals(LENARR05, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/BooleanTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/BooleanTests.java index c7c5bff..c826391 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/BooleanTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/BooleanTests.java @@ -2,12 +2,13 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.DataTypeValidationException; +import com.relogiclabs.jschema.exception.ValueValidationException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.BOOL01; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP06; +import static com.relogiclabs.jschema.message.ErrorCode.BOOLVL01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS02; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -18,9 +19,9 @@ public void When_JsonNotBoolean_ExceptionThrown() { var json = "5"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -30,9 +31,9 @@ public void When_JsonValueNotEqualForBoolean_ExceptionThrown() { var json = "false"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(ValueValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(BOOL01, exception.getCode()); + assertEquals(BOOLVL01, exception.getCode()); exception.printStackTrace(); } @@ -55,9 +56,9 @@ public void When_JsonNotBooleanInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -72,9 +73,9 @@ public void When_JsonNotBooleanInArray_ExceptionThrown() { [[], 11.5, "false"] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -89,9 +90,9 @@ public void When_NestedJsonNotBooleanInArray_ExceptionThrown() { ["true", {}, [true]] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -110,9 +111,9 @@ public void When_NestedJsonNotBooleanInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/ComponentTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/ComponentTests.java index 0866b66..fd6312e 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/ComponentTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/ComponentTests.java @@ -1,12 +1,12 @@ package com.relogiclabs.jschema.test.negative; import com.relogiclabs.jschema.JsonAssert; -import com.relogiclabs.jschema.exception.DefinitionNotFoundException; -import com.relogiclabs.jschema.exception.DuplicateDefinitionException; +import com.relogiclabs.jschema.exception.AliasNotFoundException; +import com.relogiclabs.jschema.exception.DuplicateAliasException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.DEFI01; -import static com.relogiclabs.jschema.message.ErrorCode.DEFI02; +import static com.relogiclabs.jschema.message.ErrorCode.ALSDEF01; +import static com.relogiclabs.jschema.message.ErrorCode.ALSDUP01; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -28,9 +28,9 @@ public void When_ComponentNotDefined_ExceptionThrown() { } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(DefinitionNotFoundException.class, + var exception = assertThrows(AliasNotFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DEFI02, exception.getCode()); + assertEquals(ALSDEF01, exception.getCode()); exception.printStackTrace(); } @@ -53,9 +53,9 @@ public void When_ComponentWithDuplicateDefinition_ExceptionThrown() { } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(DuplicateDefinitionException.class, + var exception = assertThrows(DuplicateAliasException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DEFI01, exception.getCode()); + assertEquals(ALSDUP01, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/DataTypeTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/DataTypeTests.java index 3efd23d..38cf075 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/DataTypeTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/DataTypeTests.java @@ -2,21 +2,22 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; -import com.relogiclabs.jschema.exception.DefinitionNotFoundException; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.AliasNotFoundException; +import com.relogiclabs.jschema.exception.DataTypeValidationException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.DEFI03; -import static com.relogiclabs.jschema.message.ErrorCode.DEFI04; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP03; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP06; +import static com.relogiclabs.jschema.message.ErrorCode.ALSDEF02; +import static com.relogiclabs.jschema.message.ErrorCode.ALSDEF03; +import static com.relogiclabs.jschema.message.ErrorCode.DTYCPS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS02; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; public class DataTypeTests { @Test - public void When_WrongJsonWithDirectDataType_ExceptionThrown() { + public void When_DirectDataTypeWithWrongJson_ExceptionThrown() { var schema = """ #string* #array @@ -26,14 +27,14 @@ public void When_WrongJsonWithDirectDataType_ExceptionThrown() { 10 """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_WrongJsonWithNestedDataType_ExceptionThrown() { + public void When_NestedDataTypeWithWrongJson_ExceptionThrown() { var schema = """ #string* #array @@ -43,9 +44,9 @@ public void When_WrongJsonWithNestedDataType_ExceptionThrown() { [10, 20] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -60,14 +61,14 @@ public void When_NestedTypeWithNonCompositeJson_ExceptionThrown() { 10 """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP03, exception.getCode()); + assertEquals(DTYCPS01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_UndefinedDataTypeArgument_ExceptionThrown() { + public void When_DirectDataTypeWithUndefinedAliasArgument_ExceptionThrown() { var schema = """ #array($undefined) @@ -77,14 +78,14 @@ public void When_UndefinedDataTypeArgument_ExceptionThrown() { [10, 20] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(DefinitionNotFoundException.class, + var exception = assertThrows(AliasNotFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DEFI03, exception.getCode()); + assertEquals(ALSDEF02, exception.getCode()); exception.printStackTrace(); } @Test - public void When_UndefinedNestedDataTypeArgument_ExceptionThrown() { + public void When_NestedDataTypeWithUndefinedAliasArgument_ExceptionThrown() { var schema = """ #integer*($undefined) #array @@ -94,9 +95,9 @@ public void When_UndefinedNestedDataTypeArgument_ExceptionThrown() { [10, 20] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(DefinitionNotFoundException.class, + var exception = assertThrows(AliasNotFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DEFI04, exception.getCode()); + assertEquals(ALSDEF03, exception.getCode()); exception.printStackTrace(); } @@ -112,9 +113,9 @@ public void When_DataTypeArgumentWithValidationFailed_ExceptionThrown() { {"k1": 10} """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -130,9 +131,9 @@ public void When_NestedDataTypeArgumentWithValidationFailed_ExceptionThrown() { [{"k1": 10}] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -155,9 +156,9 @@ public void When_MultipleNestedDataTypeWithWrongValueInObject_ExceptionThrown() } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -180,11 +181,11 @@ public void When_DataTypeExceptionCountInObject_ExceptionThrown() { } """; var jsonSchema = new JsonSchema(schema); - if(!jsonSchema.isValid(json)) jsonSchema.writeError(); - assertEquals(8, jsonSchema.getExceptions().getCount()); - var exception = assertThrows(JsonSchemaException.class, + if(jsonSchema.isValid(json)) fail("Test failed"); + assertEquals(7, jsonSchema.writeError()); + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -211,9 +212,9 @@ public void When_MultipleDataTypeWithWrongValueInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/DateTimeTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/DateTimeTests.java index 4f77bbd..c5647e7 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/DateTimeTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/DateTimeTests.java @@ -2,96 +2,93 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.DataTypeValidationException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.AFTR01; -import static com.relogiclabs.jschema.message.ErrorCode.BFOR01; -import static com.relogiclabs.jschema.message.ErrorCode.DCNF01; -import static com.relogiclabs.jschema.message.ErrorCode.DDAY03; -import static com.relogiclabs.jschema.message.ErrorCode.DDAY04; -import static com.relogiclabs.jschema.message.ErrorCode.DERA01; -import static com.relogiclabs.jschema.message.ErrorCode.DFRC04; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR01; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR02; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR03; -import static com.relogiclabs.jschema.message.ErrorCode.DHUR06; -import static com.relogiclabs.jschema.message.ErrorCode.DINV02; -import static com.relogiclabs.jschema.message.ErrorCode.DLEX01; -import static com.relogiclabs.jschema.message.ErrorCode.DMIN01; -import static com.relogiclabs.jschema.message.ErrorCode.DMIN03; -import static com.relogiclabs.jschema.message.ErrorCode.DMON01; -import static com.relogiclabs.jschema.message.ErrorCode.DMON02; -import static com.relogiclabs.jschema.message.ErrorCode.DMON03; -import static com.relogiclabs.jschema.message.ErrorCode.DMON05; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG01; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG02; -import static com.relogiclabs.jschema.message.ErrorCode.DSEC01; -import static com.relogiclabs.jschema.message.ErrorCode.DSEC03; -import static com.relogiclabs.jschema.message.ErrorCode.DTAP01; -import static com.relogiclabs.jschema.message.ErrorCode.DTXT01; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.DUTC01; -import static com.relogiclabs.jschema.message.ErrorCode.DUTC04; -import static com.relogiclabs.jschema.message.ErrorCode.DUTC05; -import static com.relogiclabs.jschema.message.ErrorCode.DWKD02; -import static com.relogiclabs.jschema.message.ErrorCode.DWKD03; -import static com.relogiclabs.jschema.message.ErrorCode.DWTS01; -import static com.relogiclabs.jschema.message.ErrorCode.DYAR01; -import static com.relogiclabs.jschema.message.ErrorCode.DYAR02; -import static com.relogiclabs.jschema.message.ErrorCode.DYAR03; -import static com.relogiclabs.jschema.message.ErrorCode.ENDE01; -import static com.relogiclabs.jschema.message.ErrorCode.ENDE02; -import static com.relogiclabs.jschema.message.ErrorCode.STRT01; +import static com.relogiclabs.jschema.message.ErrorCode.AFTRDT01; +import static com.relogiclabs.jschema.message.ErrorCode.AMPMDT01; +import static com.relogiclabs.jschema.message.ErrorCode.BFORDT01; +import static com.relogiclabs.jschema.message.ErrorCode.CNFLDT01; +import static com.relogiclabs.jschema.message.ErrorCode.DAYRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.DAYRNG02; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.ENDFDT01; +import static com.relogiclabs.jschema.message.ErrorCode.ENDFDT02; +import static com.relogiclabs.jschema.message.ErrorCode.ERANAM01; +import static com.relogiclabs.jschema.message.ErrorCode.FRACDT04; +import static com.relogiclabs.jschema.message.ErrorCode.HURNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.HURNUM02; +import static com.relogiclabs.jschema.message.ErrorCode.HURRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.HURRNG03; +import static com.relogiclabs.jschema.message.ErrorCode.INVLDT01; +import static com.relogiclabs.jschema.message.ErrorCode.LEXRDT01; +import static com.relogiclabs.jschema.message.ErrorCode.MNTNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.MNTRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.MONNAM01; +import static com.relogiclabs.jschema.message.ErrorCode.MONNAM02; +import static com.relogiclabs.jschema.message.ErrorCode.MONNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.MONRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.RNDEND02; +import static com.relogiclabs.jschema.message.ErrorCode.RNDSTA01; +import static com.relogiclabs.jschema.message.ErrorCode.SECNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.SECRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.STRTDT01; +import static com.relogiclabs.jschema.message.ErrorCode.TEXTDT01; +import static com.relogiclabs.jschema.message.ErrorCode.UTCRNG01; +import static com.relogiclabs.jschema.message.ErrorCode.UTCRNG02; +import static com.relogiclabs.jschema.message.ErrorCode.UTCTIM01; +import static com.relogiclabs.jschema.message.ErrorCode.WEKDMS01; +import static com.relogiclabs.jschema.message.ErrorCode.WEKNAM02; +import static com.relogiclabs.jschema.message.ErrorCode.WSPACE01; +import static com.relogiclabs.jschema.message.ErrorCode.YARNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.YARNUM02; +import static com.relogiclabs.jschema.message.ErrorCode.YARRNG01; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; public class DateTimeTests { - @Test - public void When_JsonNotDate_ExceptionThrown() { - var schema = "#date"; - var json = "\"This is not a date\""; - - JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, - () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); - exception.printStackTrace(); - } - @Test public void When_JsonNotValidDate_ExceptionThrown() { - var schema = "#date"; - var json = "\"1939-02-29\""; - - JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, - () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); - exception.printStackTrace(); - } - - @Test - public void When_JsonNotTime_ExceptionThrown() { - var schema = "#time"; - var json = "\"This is not a time\""; + var schema = + """ + [ #date, #date, #date ] + """; + var json = + """ + [ + "This is not a date", + "1939-13-10", + "1939-02-29" + ] + """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @Test public void When_JsonNotValidTime_ExceptionThrown() { - var schema = "#time"; - var json = "\"1939-09-02T2:12:12.000Z\""; + var schema = + """ + [ #time, #time, #time ] + """; + var json = + """ + [ + "This is not a time", + "1939-09-02T24:02:02.000Z", + "1939-09-20T02:60:09.000Z" + ] + """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -106,9 +103,9 @@ public void When_DateInputWrong_ExceptionThrown() { "99-09-01" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DDAY04, exception.getCode()); + assertEquals(DAYRNG01, exception.getCode()); exception.printStackTrace(); } @@ -123,9 +120,9 @@ public void When_TimeInputWrong_ExceptionThrown() { "13:10:10 PM" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DHUR03, exception.getCode()); + assertEquals(HURRNG01, exception.getCode()); exception.printStackTrace(); } @@ -140,9 +137,9 @@ public void When_DateDayOutOfRange_ExceptionThrown() { "29-02-1939" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DDAY03, exception.getCode()); + assertEquals(DAYRNG02, exception.getCode()); exception.printStackTrace(); } @@ -157,9 +154,9 @@ public void When_DateDayOutOfRange2_ExceptionThrown() { "32-12-1939" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DDAY04, exception.getCode()); + assertEquals(DAYRNG01, exception.getCode()); exception.printStackTrace(); } @@ -174,9 +171,9 @@ public void When_InvalidDateMonthFullName_ExceptionThrown() { "Septembar 01, 1939 AD" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DMON01, exception.getCode()); + assertEquals(MONNAM01, exception.getCode()); exception.printStackTrace(); } @@ -191,9 +188,9 @@ public void When_InvalidDateMonthShortName_ExceptionThrown() { "Sap 01, 1939 AD" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DMON02, exception.getCode()); + assertEquals(MONNAM02, exception.getCode()); exception.printStackTrace(); } @@ -208,9 +205,9 @@ public void When_InvalidDateMonthNumber_ExceptionThrown() { "Sep-01, 1939 AD" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DMON03, exception.getCode()); + assertEquals(MONNUM01, exception.getCode()); exception.printStackTrace(); } @@ -225,9 +222,9 @@ public void When_InvalidDateMonthNumberRange_ExceptionThrown() { "13-01, 1939 AD" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DMON05, exception.getCode()); + assertEquals(MONRNG01, exception.getCode()); exception.printStackTrace(); } @@ -242,9 +239,9 @@ public void When_InvalidDateWeekdayInput_ExceptionThrown() { "Fry, Sep 01, 1939 AD" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DWKD02, exception.getCode()); + assertEquals(WEKNAM02, exception.getCode()); exception.printStackTrace(); } @@ -259,9 +256,9 @@ public void When_ConflictingDateInfoInInput_ExceptionThrown() { "January, 01-12-1939" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DCNF01, exception.getCode()); + assertEquals(CNFLDT01, exception.getCode()); exception.printStackTrace(); } @@ -276,14 +273,14 @@ public void When_ConflictingTimeInfoInInput_ExceptionThrown() { "12, 11:10:12" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DCNF01, exception.getCode()); + assertEquals(CNFLDT01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_InvalidDateWeekday_ExceptionThrown() { + public void When_InvalidDateWeekdayMismatched_ExceptionThrown() { var schema = """ @date("DDD, MMM DD, YYYY G") @@ -293,9 +290,9 @@ public void When_InvalidDateWeekday_ExceptionThrown() { "Sat, Sep 01, 1939 AD" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DWKD03, exception.getCode()); + assertEquals(WEKDMS01, exception.getCode()); exception.printStackTrace(); } @@ -310,9 +307,9 @@ public void When_InvalidDateYearInput_ExceptionThrown() { "01-09-Twenty" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DYAR02, exception.getCode()); + assertEquals(YARNUM02, exception.getCode()); exception.printStackTrace(); } @@ -327,9 +324,9 @@ public void When_InvalidDateYearInput2_ExceptionThrown() { "01-09-0000" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DYAR03, exception.getCode()); + assertEquals(YARRNG01, exception.getCode()); exception.printStackTrace(); } @@ -344,9 +341,9 @@ public void When_InvalidDateYearInput3_ExceptionThrown() { "01-09-1939" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DINV02, exception.getCode()); + assertEquals(INVLDT01, exception.getCode()); exception.printStackTrace(); } @@ -361,9 +358,9 @@ public void When_InvalidDateEraInput_ExceptionThrown() { "02-12-1939 AA" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DERA01, exception.getCode()); + assertEquals(ERANAM01, exception.getCode()); exception.printStackTrace(); } @@ -378,9 +375,9 @@ public void When_InvalidTimeTextMissing_ExceptionThrown() { "01-11-1939 10:00:00" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTXT01, exception.getCode()); + assertEquals(TEXTDT01, exception.getCode()); exception.printStackTrace(); } @@ -395,9 +392,9 @@ public void When_InvalidTimeHourInput_ExceptionThrown() { "Twelve:00:00" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DHUR01, exception.getCode()); + assertEquals(HURNUM01, exception.getCode()); exception.printStackTrace(); } @@ -412,9 +409,9 @@ public void When_InvalidTimeHourRange_ExceptionThrown() { "24:00:00" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DHUR06, exception.getCode()); + assertEquals(HURRNG03, exception.getCode()); exception.printStackTrace(); } @@ -429,9 +426,9 @@ public void When_InvalidTimeMinuteInput_ExceptionThrown() { "23:one:00" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DMIN01, exception.getCode()); + assertEquals(MNTNUM01, exception.getCode()); exception.printStackTrace(); } @@ -446,9 +443,9 @@ public void When_InvalidTimeMinuteRange_ExceptionThrown() { "23:60:00" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DMIN03, exception.getCode()); + assertEquals(MNTRNG01, exception.getCode()); exception.printStackTrace(); } @@ -463,9 +460,9 @@ public void When_InvalidTimeSecondInput_ExceptionThrown() { "23:59:Three" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DSEC01, exception.getCode()); + assertEquals(SECNUM01, exception.getCode()); exception.printStackTrace(); } @@ -480,9 +477,9 @@ public void When_InvalidTimeSecondRange_ExceptionThrown() { "23:59:60" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DSEC03, exception.getCode()); + assertEquals(SECRNG01, exception.getCode()); exception.printStackTrace(); } @@ -497,9 +494,9 @@ public void When_InvalidTimeSecondFraction_ExceptionThrown() { "23:59:00.11" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DFRC04, exception.getCode()); + assertEquals(FRACDT04, exception.getCode()); exception.printStackTrace(); } @@ -514,9 +511,9 @@ public void When_InvalidTimeNoHourInput_ExceptionThrown() { ":3:8" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DHUR02, exception.getCode()); + assertEquals(HURNUM02, exception.getCode()); exception.printStackTrace(); } @@ -531,9 +528,9 @@ public void When_InvalidTimeInput_ExceptionThrown() { "01:10:08" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DWTS01, exception.getCode()); + assertEquals(WSPACE01, exception.getCode()); exception.printStackTrace(); } @@ -548,9 +545,9 @@ public void When_InvalidTimeAmPmInput_ExceptionThrown() { "12:00:00 AD" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTAP01, exception.getCode()); + assertEquals(AMPMDT01, exception.getCode()); exception.printStackTrace(); } @@ -565,9 +562,9 @@ public void When_InvalidTime12HourInput_ExceptionThrown() { "13:00:00 AM" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DHUR03, exception.getCode()); + assertEquals(HURRNG01, exception.getCode()); exception.printStackTrace(); } @@ -582,9 +579,9 @@ public void When_InvalidTimeAmPmMissing_ExceptionThrown() { "11:11:11" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTAP01, exception.getCode()); + assertEquals(AMPMDT01, exception.getCode()); exception.printStackTrace(); } @@ -599,9 +596,9 @@ public void When_InvalidTimeUTCOffsetInput_ExceptionThrown() { "11:00:00 Six" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DUTC01, exception.getCode()); + assertEquals(UTCTIM01, exception.getCode()); exception.printStackTrace(); } @@ -616,9 +613,9 @@ public void When_InvalidTimeUTCOffsetHourRange_ExceptionThrown() { "11:00:00 +14" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DUTC04, exception.getCode()); + assertEquals(UTCRNG01, exception.getCode()); exception.printStackTrace(); } @@ -633,43 +630,43 @@ public void When_InvalidTimeUTCOffsetMinuteRange_ExceptionThrown() { "11:00:00 +10:60" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DUTC05, exception.getCode()); + assertEquals(UTCRNG02, exception.getCode()); exception.printStackTrace(); } @Test - public void When_InvalidDatePatternCauseLexerError_ExceptionThrown() { + public void When_InvalidDatePatternFormat_ExceptionThrown() { var schema = """ - @date("ABCD") + @date("TEST") """; var json = """ "23-09-01T14:35:10.555" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DLEX01, exception.getCode()); + assertEquals(LEXRDT01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_InvalidTimePatternCauseLexerError_ExceptionThrown() { + public void When_InvalidTimePatternFormat_ExceptionThrown() { var schema = """ - @time("ABCD") + @time("TEST") """; var json = """ "23-09-01T14:35:10.555" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DLEX01, exception.getCode()); + assertEquals(LEXRDT01, exception.getCode()); exception.printStackTrace(); } @@ -690,9 +687,9 @@ public void When_JsonDateNotValidWithBothRange_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DRNG01, exception.getCode()); + assertEquals(RNDSTA01, exception.getCode()); exception.printStackTrace(); } @@ -703,6 +700,7 @@ public void When_JsonTimeNotValidWithBothRange_ExceptionThrown() { @range*("2010-01-01T00:00:00.000Z", "2010-12-31T23:59:59.999Z") #time* #array + // range also consider UTC offset if given """; var json = """ @@ -712,14 +710,14 @@ public void When_JsonTimeNotValidWithBothRange_ExceptionThrown() { "2010-06-30T12:01:07.999999Z", "2009-12-31T22:39:50.0-04:00", "2011-01-01T02:10:00.0+06:00", - "2009-12-31T22:59:59.000Z", - "2011-01-01T00:00:00.000Z" + "2011-01-01T00:00:00.000Z", + "2009-12-31T22:59:59.000Z" ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DRNG02, exception.getCode()); + assertEquals(RNDEND02, exception.getCode()); exception.printStackTrace(); } @@ -740,9 +738,9 @@ public void When_JsonDateNotValidWithStart_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(STRT01, exception.getCode()); + assertEquals(STRTDT01, exception.getCode()); exception.printStackTrace(); } @@ -763,9 +761,9 @@ public void When_JsonDateNotValidWithEnd_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ENDE01, exception.getCode()); + assertEquals(ENDFDT01, exception.getCode()); exception.printStackTrace(); } @@ -786,9 +784,9 @@ public void When_JsonDateNotValidWithBefore_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(BFOR01, exception.getCode()); + assertEquals(BFORDT01, exception.getCode()); exception.printStackTrace(); } @@ -809,9 +807,9 @@ public void When_JsonDateNotValidWithAfter_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(AFTR01, exception.getCode()); + assertEquals(AFTRDT01, exception.getCode()); exception.printStackTrace(); } @@ -830,14 +828,14 @@ public void When_JsonTimeNotValidWithEndInFraction_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ENDE02, exception.getCode()); + assertEquals(ENDFDT02, exception.getCode()); exception.printStackTrace(); } @Test - public void When_SchemaDateNotValidWithBefore_ExceptionThrown() { + public void When_SchemaDateNotValidFormatWithBefore_ExceptionThrown() { var schema = """ @before*("01-01-2011") #date* #array @@ -851,14 +849,14 @@ public void When_SchemaDateNotValidWithBefore_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DYAR01, exception.getCode()); + assertEquals(YARNUM01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_SchemaDateNotValidWithAfter_ExceptionThrown() { + public void When_SchemaDateNotValidFormatWithAfter_ExceptionThrown() { var schema = """ @after*("12-31-2009") #date* #array @@ -872,9 +870,9 @@ public void When_SchemaDateNotValidWithAfter_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DYAR01, exception.getCode()); + assertEquals(YARNUM01, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/FunctionTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/FunctionTests.java index 7d74df7..bb223f7 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/FunctionTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/FunctionTests.java @@ -5,26 +5,26 @@ import com.relogiclabs.jschema.exception.ClassInstantiationException; import com.relogiclabs.jschema.exception.DuplicateImportException; import com.relogiclabs.jschema.exception.FunctionNotFoundException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.exception.InvalidFunctionException; import com.relogiclabs.jschema.exception.InvalidImportException; -import com.relogiclabs.jschema.exception.JsonSchemaException; import com.relogiclabs.jschema.exception.NoClassFoundException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.CLAS01; -import static com.relogiclabs.jschema.message.ErrorCode.CLAS02; -import static com.relogiclabs.jschema.message.ErrorCode.CLAS03; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC01; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC02; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC03; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC05; -import static com.relogiclabs.jschema.message.ErrorCode.INST03; +import static com.relogiclabs.jschema.message.ErrorCode.FNCDEF01; +import static com.relogiclabs.jschema.message.ErrorCode.FNCSIG01; +import static com.relogiclabs.jschema.message.ErrorCode.FNCSIG02; +import static com.relogiclabs.jschema.message.ErrorCode.FNTRGT01; +import static com.relogiclabs.jschema.message.ErrorCode.IMPCLS01; +import static com.relogiclabs.jschema.message.ErrorCode.IMPCLS02; +import static com.relogiclabs.jschema.message.ErrorCode.IMPDUP01; +import static com.relogiclabs.jschema.message.ErrorCode.INSTCR03; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; public class FunctionTests { @Test - public void When_FunctionAppliedOnWrongType_ExceptionThrown() { + public void When_FunctionAppliedOnWrongTargetType_ExceptionThrown() { var schema = """ %schema: @range(10, 20) @@ -35,9 +35,9 @@ public void When_FunctionAppliedOnWrongType_ExceptionThrown() { """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUNC03, exception.getCode()); + assertEquals(FNTRGT01, exception.getCode()); exception.printStackTrace(); } @@ -53,7 +53,7 @@ public void When_ExternalImportNotInheritBaseClass_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(InvalidImportException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(CLAS03, exception.getCode()); + assertEquals(IMPCLS02, exception.getCode()); exception.printStackTrace(); } @@ -69,7 +69,7 @@ public void When_ExternalImportNotExisting_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(NoClassFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(CLAS02, exception.getCode()); + assertEquals(IMPCLS01, exception.getCode()); exception.printStackTrace(); } @@ -86,7 +86,7 @@ public void When_ExternalImportDuplicationOccurred_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(DuplicateImportException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(CLAS01, exception.getCode()); + assertEquals(IMPDUP01, exception.getCode()); exception.printStackTrace(); } @@ -102,7 +102,7 @@ public void When_ExternalImportInstantiationNotSuccessful_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(ClassInstantiationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(INST03, exception.getCode()); + assertEquals(INSTCR03, exception.getCode()); exception.printStackTrace(); } @@ -118,12 +118,12 @@ public void When_ExternalFunctionWrongReturnType_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(InvalidFunctionException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUNC01, exception.getCode()); + assertEquals(FNCSIG01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_ExternalFunctionWrongParameterNumber_ExceptionThrown() { + public void When_ExternalFunctionMissingMandatoryTargetParameter_ExceptionThrown() { var schema = """ %import: com.relogiclabs.jschema.test.external.ExternalFunctions3 @@ -134,7 +134,7 @@ public void When_ExternalFunctionWrongParameterNumber_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(InvalidFunctionException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUNC02, exception.getCode()); + assertEquals(FNCSIG02, exception.getCode()); exception.printStackTrace(); } @@ -150,12 +150,12 @@ public void When_ExternalFunctionNotExists_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(FunctionNotFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUNC05, exception.getCode()); + assertEquals(FNCDEF01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_FunctionThrowArbitraryException_ExceptionThrown() { + public void When_ExternalFunctionThrowArbitraryException_ExceptionThrown() { var schema = """ %import: com.relogiclabs.jschema.test.external.ExternalFunctions4 @@ -181,9 +181,9 @@ public void When_IncompatibleTargetForExternalFunction_ExceptionThrown() { "test" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUNC03, exception.getCode()); + assertEquals(FNTRGT01, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/IntegerTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/IntegerTests.java index 53506a6..05d16b1 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/IntegerTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/IntegerTests.java @@ -2,16 +2,18 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.DataTypeValidationException; +import com.relogiclabs.jschema.exception.FunctionValidationException; +import com.relogiclabs.jschema.exception.ValueValidationException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP06; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC06; -import static com.relogiclabs.jschema.message.ErrorCode.INTE01; -import static com.relogiclabs.jschema.message.ErrorCode.RANG01; -import static com.relogiclabs.jschema.message.ErrorCode.RANG03; -import static com.relogiclabs.jschema.message.ErrorCode.RANG04; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS02; +import static com.relogiclabs.jschema.message.ErrorCode.FNCFAL01; +import static com.relogiclabs.jschema.message.ErrorCode.INTVAL01; +import static com.relogiclabs.jschema.message.ErrorCode.RNGNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.RNGNUM03; +import static com.relogiclabs.jschema.message.ErrorCode.RNGNUM04; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -22,9 +24,9 @@ public void When_JsonNotInteger_ExceptionThrown() { var json = "10.5"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -34,9 +36,9 @@ public void When_JsonValueNotEqualForInteger_ExceptionThrown() { var json = "9"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(ValueValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(INTE01, exception.getCode()); + assertEquals(INTVAL01, exception.getCode()); exception.printStackTrace(); } @@ -59,9 +61,9 @@ public void When_JsonNotIntegerInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -76,9 +78,9 @@ public void When_JsonNotIntegerInArray_ExceptionThrown() { [true, -4568.57, 100] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -93,9 +95,9 @@ public void When_NestedJsonNotIntegerInArray_ExceptionThrown() { [null, 2.2, "40000000"] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -114,9 +116,9 @@ public void When_NestedJsonNotIntegerInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -135,14 +137,14 @@ public void When_NestedRangeWithJsonNotIntegerInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @Test - public void When_NestedRangeWithNonCompositeJsonInObject_ExceptionThrown() { + public void When_NestedRangeWithNonCompositeJsonValue_ExceptionThrown() { var schema = """ @range*(100, !) @@ -152,9 +154,9 @@ public void When_NestedRangeWithNonCompositeJsonInObject_ExceptionThrown() { "value1" """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUNC06, exception.getCode()); + assertEquals(FNCFAL01, exception.getCode()); exception.printStackTrace(); } @@ -173,9 +175,9 @@ public void When_NestedRangeWithJsonWrongIntegerInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(RANG01, exception.getCode()); + assertEquals(RNGNUM01, exception.getCode()); exception.printStackTrace(); } @@ -190,9 +192,9 @@ public void When_NestedRangeWithMinUndefinedAndWrongIntegerInArray_ExceptionThro [100, 500, 900] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(RANG04, exception.getCode()); + assertEquals(RNGNUM04, exception.getCode()); exception.printStackTrace(); } @@ -207,9 +209,9 @@ public void When_NestedRangeWithMaxUndefinedAndWrongIntegerInArray_ExceptionThro [2000, 1000, 900] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(RANG03, exception.getCode()); + assertEquals(RNGNUM03, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/NumberTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/NumberTests.java index b68ea4f..c27c307 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/NumberTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/NumberTests.java @@ -2,17 +2,17 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.MAXI01; -import static com.relogiclabs.jschema.message.ErrorCode.MAXI03; -import static com.relogiclabs.jschema.message.ErrorCode.MINI01; -import static com.relogiclabs.jschema.message.ErrorCode.MINI03; -import static com.relogiclabs.jschema.message.ErrorCode.NEGI01; -import static com.relogiclabs.jschema.message.ErrorCode.NEGI02; -import static com.relogiclabs.jschema.message.ErrorCode.POSI01; -import static com.relogiclabs.jschema.message.ErrorCode.POSI02; +import static com.relogiclabs.jschema.message.ErrorCode.MAXICF01; +import static com.relogiclabs.jschema.message.ErrorCode.MAXICF03; +import static com.relogiclabs.jschema.message.ErrorCode.MINICF01; +import static com.relogiclabs.jschema.message.ErrorCode.MINICF03; +import static com.relogiclabs.jschema.message.ErrorCode.NEGICF01; +import static com.relogiclabs.jschema.message.ErrorCode.NEGICF02; +import static com.relogiclabs.jschema.message.ErrorCode.POSICF01; +import static com.relogiclabs.jschema.message.ErrorCode.POSICF02; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -28,9 +28,9 @@ public void When_JsonLessThanMinimumFloat_ExceptionThrown() { 9.999999 """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MINI01, exception.getCode()); + assertEquals(MINICF01, exception.getCode()); exception.printStackTrace(); } @@ -45,14 +45,14 @@ public void When_JsonGreaterThanMaximumInteger_ExceptionThrown() { 1000 """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MAXI01, exception.getCode()); + assertEquals(MAXICF01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_WrongJsonWithNestedMinimumIntegerInArray_ExceptionThrown() { + public void When_NestedMinimumWithWrongIntegerInArray_ExceptionThrown() { var schema = """ @minimum*(10.5) #integer* @@ -66,14 +66,14 @@ public void When_WrongJsonWithNestedMinimumIntegerInArray_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MINI01, exception.getCode()); + assertEquals(MINICF01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_WrongJsonWithNestedMinimumFloatInObject_ExceptionThrown() { + public void When_NestedMinimumWithWrongFloatInObject_ExceptionThrown() { var schema = """ @minimum*(100) #number* @@ -87,14 +87,14 @@ public void When_WrongJsonWithNestedMinimumFloatInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MINI01, exception.getCode()); + assertEquals(MINICF01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_WrongJsonWithNestedMaximumNumberInArray_ExceptionThrown() { + public void When_NestedMaximumWithWrongNumberInArray_ExceptionThrown() { var schema = """ @maximum*(1000.05) #number* @@ -108,14 +108,14 @@ public void When_WrongJsonWithNestedMaximumNumberInArray_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MAXI01, exception.getCode()); + assertEquals(MAXICF01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_NestedMaximumWrongFloatInObject_ExceptionThrown() { + public void When_NestedMaximumWithWrongFloatInObject_ExceptionThrown() { var schema = """ @maximum*(100) #float* @@ -129,14 +129,14 @@ public void When_NestedMaximumWrongFloatInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MAXI01, exception.getCode()); + assertEquals(MAXICF01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_NestedMinimumExclusiveWrongFloatInObject_ExceptionThrown() { + public void When_NestedMinimumExclusiveWithWrongFloatInObject_ExceptionThrown() { var schema = """ @minimum*(100, true) #float* @@ -150,17 +150,17 @@ public void When_NestedMinimumExclusiveWrongFloatInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MINI03, exception.getCode()); + assertEquals(MINICF03, exception.getCode()); exception.printStackTrace(); } @Test - public void When_NestedMaximumExclusiveWrongFloatInObject_ExceptionThrown() { + public void When_NestedMinMaxExclusiveWithWrongFloatInObject_ExceptionThrown() { var schema = """ - @maximum*(100, true) #float* + @minimum*(10, true) @maximum*(100, true) #float* """; var json = """ @@ -171,9 +171,9 @@ public void When_NestedMaximumExclusiveWrongFloatInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MAXI03, exception.getCode()); + assertEquals(MAXICF03, exception.getCode()); exception.printStackTrace(); } @@ -188,9 +188,9 @@ public void When_NestedPositiveWithWrongNumberInArray_ExceptionThrown() { [1, 100.5, -500] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(POSI01, exception.getCode()); + assertEquals(POSICF01, exception.getCode()); exception.printStackTrace(); } @@ -205,9 +205,9 @@ public void When_NestedPositiveReferenceWithWrongNumberInArray_ExceptionThrown() [0, 100, 0.1, -1] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(POSI02, exception.getCode()); + assertEquals(POSICF02, exception.getCode()); exception.printStackTrace(); } @@ -222,9 +222,9 @@ public void When_NestedNegativeWithWrongNumberInArray_ExceptionThrown() { [-100, -500, -0.1, 0] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(NEGI01, exception.getCode()); + assertEquals(NEGICF01, exception.getCode()); exception.printStackTrace(); } @@ -239,9 +239,9 @@ public void When_NestedNegativeReferenceWithWrongNumberInArray_ExceptionThrown() [-100, -500, -0.01, 1] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(NEGI02, exception.getCode()); + assertEquals(NEGICF02, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/ObjectTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/ObjectTests.java index 8c0be33..289f338 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/ObjectTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/ObjectTests.java @@ -2,22 +2,23 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; +import com.relogiclabs.jschema.exception.DataTypeValidationException; import com.relogiclabs.jschema.exception.DuplicatePropertyKeyException; -import com.relogiclabs.jschema.exception.JsonSchemaException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP06; -import static com.relogiclabs.jschema.message.ErrorCode.ENUM02; -import static com.relogiclabs.jschema.message.ErrorCode.KEYS01; -import static com.relogiclabs.jschema.message.ErrorCode.NEMT03; -import static com.relogiclabs.jschema.message.ErrorCode.OLEN01; -import static com.relogiclabs.jschema.message.ErrorCode.OLEN02; -import static com.relogiclabs.jschema.message.ErrorCode.OLEN04; -import static com.relogiclabs.jschema.message.ErrorCode.OLEN05; -import static com.relogiclabs.jschema.message.ErrorCode.PROP03; -import static com.relogiclabs.jschema.message.ErrorCode.PROP04; -import static com.relogiclabs.jschema.message.ErrorCode.VALU01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS02; +import static com.relogiclabs.jschema.message.ErrorCode.EMPTCF03; +import static com.relogiclabs.jschema.message.ErrorCode.ENMNUM01; +import static com.relogiclabs.jschema.message.ErrorCode.KEYFND01; +import static com.relogiclabs.jschema.message.ErrorCode.LENOBJ01; +import static com.relogiclabs.jschema.message.ErrorCode.LENOBJ02; +import static com.relogiclabs.jschema.message.ErrorCode.LENOBJ04; +import static com.relogiclabs.jschema.message.ErrorCode.LENOBJ05; +import static com.relogiclabs.jschema.message.ErrorCode.PRTDUP01; +import static com.relogiclabs.jschema.message.ErrorCode.PRTDUP02; +import static com.relogiclabs.jschema.message.ErrorCode.VALFND01; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -28,9 +29,9 @@ public void When_JsonNotObject_ExceptionThrown() { var json = "100"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -53,9 +54,9 @@ public void When_JsonNotObjectInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -70,9 +71,9 @@ public void When_JsonNotObjectInArray_ExceptionThrown() { [null, "value1", true] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -87,9 +88,9 @@ public void When_NestedJsonNotObjectInArray_ExceptionThrown() { [ 100, true, false ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -108,9 +109,9 @@ public void When_NestedJsonNotObjectInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -129,9 +130,9 @@ public void When_KeysWithWrongObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(KEYS01, exception.getCode()); + assertEquals(KEYFND01, exception.getCode()); exception.printStackTrace(); } @@ -150,9 +151,9 @@ public void When_ValuesWithWrongObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(VALU01, exception.getCode()); + assertEquals(VALFND01, exception.getCode()); exception.printStackTrace(); } @@ -165,15 +166,15 @@ public void When_NestedKeysWithWrongObjectInObject_ExceptionThrown() { var json = """ { - "key1": {"value": 10}, - "key2": {"value": 150}, - "key3": {"value": 1000} + "key1": {"number": 10}, + "key2": {"number": 150}, + "key3": {"number": 1000} } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(KEYS01, exception.getCode()); + assertEquals(KEYFND01, exception.getCode()); exception.printStackTrace(); } @@ -185,37 +186,37 @@ public void When_NestedKeysAndValuesWithWrongObjectInArray_ExceptionThrown() { """; var json = """ - [{"value": 10}, {"value": 20}, {"value": 30}] + [{"number": 10}, {"number": 20}, {"number": 30}] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(KEYS01, exception.getCode()); + assertEquals(KEYFND01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_EnumWithWrongObject_ExceptionThrown() { + public void When_EnumWithWrongValuesInObject_ExceptionThrown() { var schema = """ { - "key1": @enum(5, 10, 15), - "key2": @enum(100, 150, 200), - "key3": @enum("abc", "pqr", "xyz") + "key1": @enum(5, 10, 15), + "key2": @enum(100, 150, 200), + "key3": @enum("abc", "pqr", "xyz") } #object """; var json = """ { - "key1": 1, - "key2": 10, - "key3": "efg" + "key1": 1, + "key2": 10, + "key3": "efg" } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ENUM02, exception.getCode()); + assertEquals(ENMNUM01, exception.getCode()); exception.printStackTrace(); } @@ -240,7 +241,7 @@ public void When_DuplicateJsonPropertyInObject_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(DuplicatePropertyKeyException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(PROP03, exception.getCode()); + assertEquals(PRTDUP01, exception.getCode()); exception.printStackTrace(); } @@ -265,7 +266,7 @@ public void When_DuplicateSchemaPropertyInObject_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(DuplicatePropertyKeyException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(PROP04, exception.getCode()); + assertEquals(PRTDUP02, exception.getCode()); exception.printStackTrace(); } @@ -286,14 +287,14 @@ public void When_EmptyObjectInArray_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(NEMT03, exception.getCode()); + assertEquals(EMPTCF03, exception.getCode()); exception.printStackTrace(); } @Test - public void When_WrongLengthOfObjectInArray1_ExceptionThrown() { + public void When_FixedLengthForWrongObjectInArray_ExceptionThrown() { var schema = """ [ @@ -307,35 +308,35 @@ public void When_WrongLengthOfObjectInArray1_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(OLEN01, exception.getCode()); + assertEquals(LENOBJ01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_WrongLengthOfObjectInArray2_ExceptionThrown() { + public void When_LengthRangeForWrongObjectInArray_ExceptionThrown() { var schema = """ [ - @length(1, 4) #object + @length(2, 4) #object ] """; var json = """ [ - { } + { "key1": 10 } ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(OLEN02, exception.getCode()); + assertEquals(LENOBJ02, exception.getCode()); exception.printStackTrace(); } @Test - public void When_WrongLengthOfObjectInArray3_ExceptionThrown() { + public void When_LengthWithMinUndefinedForWrongObjectInArray_ExceptionThrown() { var schema = """ [ @@ -349,14 +350,14 @@ public void When_WrongLengthOfObjectInArray3_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(OLEN05, exception.getCode()); + assertEquals(LENOBJ05, exception.getCode()); exception.printStackTrace(); } @Test - public void When_WrongLengthOfObjectInArray4_ExceptionThrown() { + public void When_LengthWithMaxUndefinedForWrongObjectInArray_ExceptionThrown() { var schema = """ [ @@ -370,9 +371,9 @@ public void When_WrongLengthOfObjectInArray4_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(OLEN04, exception.getCode()); + assertEquals(LENOBJ04, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/OtherTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/OtherTests.java index 5b794b2..9af144f 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/OtherTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/OtherTests.java @@ -2,18 +2,19 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; +import com.relogiclabs.jschema.exception.DataTypeValidationException; import com.relogiclabs.jschema.exception.InvalidDataTypeException; -import com.relogiclabs.jschema.exception.JsonSchemaException; import com.relogiclabs.jschema.exception.MisplacedOptionalException; +import com.relogiclabs.jschema.exception.ValueValidationException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.ARRY01; -import static com.relogiclabs.jschema.message.ErrorCode.ARRY02; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP01; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.DUBL01; -import static com.relogiclabs.jschema.message.ErrorCode.FLOT01; -import static com.relogiclabs.jschema.message.ErrorCode.PROP05; +import static com.relogiclabs.jschema.message.ErrorCode.ARRELM01; +import static com.relogiclabs.jschema.message.ErrorCode.ARROPT01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYVDF01; +import static com.relogiclabs.jschema.message.ErrorCode.DUBLVL01; +import static com.relogiclabs.jschema.message.ErrorCode.FLOTVL01; +import static com.relogiclabs.jschema.message.ErrorCode.PRTFND01; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -26,7 +27,7 @@ public void When_DataTypeNotValid_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(InvalidDataTypeException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP01, exception.getCode()); + assertEquals(DTYVDF01, exception.getCode()); exception.printStackTrace(); } @@ -36,9 +37,9 @@ public void When_JsonNotFloat_ExceptionThrown() { var json = "2.5E10"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -48,9 +49,9 @@ public void When_JsonNotDouble_ExceptionThrown() { var json = "\"string\""; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -60,9 +61,9 @@ public void When_JsonNotNull_ExceptionThrown() { var json = "0"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -72,9 +73,9 @@ public void When_JsonValueNotEqualForFloat_ExceptionThrown() { var json = "2.5"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(ValueValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FLOT01, exception.getCode()); + assertEquals(FLOTVL01, exception.getCode()); exception.printStackTrace(); } @@ -84,14 +85,14 @@ public void When_JsonValueNotEqualForDouble_ExceptionThrown() { var json = "2.5E1"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(ValueValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DUBL01, exception.getCode()); + assertEquals(DUBLVL01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_NonStaticValidMethodWithWrongJson_ExceptionThrown() { + public void When_NonStaticValidMethodWithMultipleJson_ExceptionThrown() { var schema = """ { @@ -114,32 +115,33 @@ public void When_NonStaticValidMethodWithWrongJson_ExceptionThrown() { } """; var jsonAssert = new JsonAssert(schema); + // json1 is a valid JSON and not throw any exception jsonAssert.isValid(json1); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> jsonAssert.isValid(json2)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_MandatoryValueMissingInArray_ExceptionThrown() { - var schema = "[@range(1, 10) #number, @range(10, 100) #number?, #number?]"; - var json = "[]"; + public void When_MandatoryElementMissingInArray_ExceptionThrown() { + var schema = "[@range(1, 10) #number, @range(10, 100) #number, #number?]"; + var json = "[10]"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(ValueValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ARRY01, exception.getCode()); + assertEquals(ARRELM01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_OptionalValidatorMisplacedInArray_ExceptionThrown() { + public void When_OptionalElementMisplacedInArray_ExceptionThrown() { var schema = "[#number, #number?, #number]"; var json = "[10, 20]"; JsonSchema.isValid(schema, json); var exception = assertThrows(MisplacedOptionalException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ARRY02, exception.getCode()); + assertEquals(ARROPT01, exception.getCode()); exception.printStackTrace(); } @@ -159,9 +161,9 @@ public void When_MandatoryPropertyMissingInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(ValueValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(PROP05, exception.getCode()); + assertEquals(PRTFND01, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/PragmaTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/PragmaTests.java index 0b97004..5fa6463 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/PragmaTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/PragmaTests.java @@ -2,36 +2,42 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; +import com.relogiclabs.jschema.exception.DataTypeValidationException; import com.relogiclabs.jschema.exception.DateTimeLexerException; import com.relogiclabs.jschema.exception.DuplicatePragmaException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.exception.InvalidPragmaValueException; -import com.relogiclabs.jschema.exception.JsonSchemaException; import com.relogiclabs.jschema.exception.PragmaNotFoundException; import com.relogiclabs.jschema.exception.SchemaParserException; +import com.relogiclabs.jschema.exception.ValueValidationException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.AFTR01; -import static com.relogiclabs.jschema.message.ErrorCode.AFTR02; -import static com.relogiclabs.jschema.message.ErrorCode.BFOR01; -import static com.relogiclabs.jschema.message.ErrorCode.BFOR02; -import static com.relogiclabs.jschema.message.ErrorCode.DLEX01; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG01; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG02; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG05; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG06; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG07; -import static com.relogiclabs.jschema.message.ErrorCode.DRNG08; -import static com.relogiclabs.jschema.message.ErrorCode.DSYM01; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.FLOT01; -import static com.relogiclabs.jschema.message.ErrorCode.PRAG01; -import static com.relogiclabs.jschema.message.ErrorCode.PRAG02; -import static com.relogiclabs.jschema.message.ErrorCode.PRAG03; -import static com.relogiclabs.jschema.message.ErrorCode.PROP06; -import static com.relogiclabs.jschema.message.ErrorCode.PROP07; -import static com.relogiclabs.jschema.message.ErrorCode.SPRS01; +import static com.relogiclabs.jschema.message.ErrorCode.AFTRDT01; +import static com.relogiclabs.jschema.message.ErrorCode.AFTRDT02; +import static com.relogiclabs.jschema.message.ErrorCode.BFORDT01; +import static com.relogiclabs.jschema.message.ErrorCode.BFORDT02; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.EMALCF01; +import static com.relogiclabs.jschema.message.ErrorCode.FLOTVL01; +import static com.relogiclabs.jschema.message.ErrorCode.LEXRDT01; +import static com.relogiclabs.jschema.message.ErrorCode.PHONCF01; +import static com.relogiclabs.jschema.message.ErrorCode.PRGDEF01; +import static com.relogiclabs.jschema.message.ErrorCode.PRGDEF02; +import static com.relogiclabs.jschema.message.ErrorCode.PRGDUP01; +import static com.relogiclabs.jschema.message.ErrorCode.PRTORD01; +import static com.relogiclabs.jschema.message.ErrorCode.PRTUDF01; +import static com.relogiclabs.jschema.message.ErrorCode.RNDEND03; +import static com.relogiclabs.jschema.message.ErrorCode.RNDEND04; +import static com.relogiclabs.jschema.message.ErrorCode.RNDSTA01; +import static com.relogiclabs.jschema.message.ErrorCode.RNDSTA02; +import static com.relogiclabs.jschema.message.ErrorCode.RNDSTA03; +import static com.relogiclabs.jschema.message.ErrorCode.RNDSTA04; +import static com.relogiclabs.jschema.message.ErrorCode.SCMPRS01; +import static com.relogiclabs.jschema.message.ErrorCode.SYMBDT01; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class PragmaTests { @Test @@ -48,18 +54,18 @@ public void When_UndefinedPropertyOfObject_ExceptionThrown() { """ { "key1": 10, - "key2": "value1" + "key2": "value2" } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(ValueValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(PROP06, exception.getCode()); + assertEquals(PRTUDF01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_InvalidUndefinedPropertyValueMissing_ExceptionThrown() { + public void When_PragmaValueMissing_ExceptionThrown() { var schema = """ %pragma IgnoreUndefinedProperties: @@ -72,21 +78,21 @@ public void When_InvalidUndefinedPropertyValueMissing_ExceptionThrown() { """ { "key1": 10, - "key2": "value1" + "key2": "value2" } """; //JsonSchema.isValid(schema, json); var exception = assertThrows(SchemaParserException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(SPRS01, exception.getCode()); + assertEquals(SCMPRS01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_IgnoreUndefinedPropertiesMalformed_ExceptionThrown() { + public void When_MisspelledOrUnknownPragma_ExceptionThrown() { var schema = """ - %pragma IgnoreUndefinedProperty: true + %pragma IgnoreUndefinedProp: true %schema: { "key1": #integer @@ -96,18 +102,18 @@ public void When_IgnoreUndefinedPropertiesMalformed_ExceptionThrown() { """ { "key1": 10, - "key2": "value1" + "key2": "value2" } """; //JsonSchema.isValid(schema, json); var exception = assertThrows(PragmaNotFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(PRAG01, exception.getCode()); + assertEquals(PRGDEF01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_InvalidUndefinedPropertyValue_ExceptionThrown() { + public void When_InvalidPragmaValueType_ExceptionThrown() { var schema = """ %pragma IgnoreUndefinedProperties: 1 @@ -120,18 +126,18 @@ public void When_InvalidUndefinedPropertyValue_ExceptionThrown() { """ { "key1": 10, - "key2": "value1" + "key2": "value2" } """; //JsonSchema.isValid(schema, json); var exception = assertThrows(InvalidPragmaValueException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(PRAG02, exception.getCode()); + assertEquals(PRGDEF02, exception.getCode()); exception.printStackTrace(); } @Test - public void When_IgnorePropertyOrderOfObject_ExceptionThrown() { + public void When_NotIgnorePropertyOrderOfObject_ExceptionThrown() { var schema = """ %pragma IgnoreObjectPropertyOrder: false @@ -147,13 +153,13 @@ public void When_IgnorePropertyOrderOfObject_ExceptionThrown() { { "key1": 10, "key3": 2.1, - "key2": "value1" + "key2": "value2" } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(ValueValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(PROP07, exception.getCode()); + assertEquals(PRTORD01, exception.getCode()); exception.printStackTrace(); } @@ -176,9 +182,9 @@ public void When_FloatingPointToleranceOfNumber_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(ValueValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FLOT01, exception.getCode()); + assertEquals(FLOTVL01, exception.getCode()); exception.printStackTrace(); } @@ -198,21 +204,21 @@ public void When_DuplicatePragmaAssign_ExceptionThrown() { """ { "key1": 10, - "key2": "value1" + "key2": "value2" } """; //JsonSchema.isValid(schema, json); var exception = assertThrows(DuplicatePragmaException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(PRAG03, exception.getCode()); + assertEquals(PRGDUP01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_InvalidDateTypeFormat_ExceptionThrown() { + public void When_InvalidDateDataTypeFormat_ExceptionThrown() { var schema = """ - %pragma DateDataTypeFormat: "ABCD" + %pragma DateDataTypeFormat: "TEST" %schema: { "key1": #date, @@ -229,12 +235,12 @@ public void When_InvalidDateTypeFormat_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(DateTimeLexerException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DLEX01, exception.getCode()); + assertEquals(LEXRDT01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_JsonMismatchWithDateFormat_ExceptionThrown() { + public void When_JsonMismatchedWithDateFormat_ExceptionThrown() { var schema = """ %pragma DateDataTypeFormat: "DD-MM-YYYY" @@ -252,14 +258,14 @@ public void When_JsonMismatchWithDateFormat_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_JsonMismatchWithTimeFormat_ExceptionThrown() { + public void When_JsonMismatchedWithTimeFormat_ExceptionThrown() { var schema = """ %pragma TimeDataTypeFormat: "DD-MM-YYYY hh:mm:ss" @@ -273,13 +279,13 @@ public void When_JsonMismatchWithTimeFormat_ExceptionThrown() { """ { "key1": "05-11-2023 12:10:30", - "key2": "05-11-2023 23.59.59" + "key2": "05.11.2023 23.59.59" } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -301,9 +307,9 @@ public void When_JsonDateNotValidWithBefore_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(BFOR01, exception.getCode()); + assertEquals(BFORDT01, exception.getCode()); exception.printStackTrace(); } @@ -325,9 +331,9 @@ public void When_JsonDateNotValidWithAfter_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(AFTR01, exception.getCode()); + assertEquals(AFTRDT01, exception.getCode()); exception.printStackTrace(); } @@ -349,9 +355,9 @@ public void When_JsonTimeNotValidWithBefore_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(BFOR02, exception.getCode()); + assertEquals(BFORDT02, exception.getCode()); exception.printStackTrace(); } @@ -373,9 +379,9 @@ public void When_JsonTimeNotValidWithAfter_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(AFTR02, exception.getCode()); + assertEquals(AFTRDT02, exception.getCode()); exception.printStackTrace(); } @@ -398,9 +404,9 @@ public void When_JsonDateNotValidWithBothRange_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DRNG01, exception.getCode()); + assertEquals(RNDSTA01, exception.getCode()); exception.printStackTrace(); } @@ -423,9 +429,9 @@ public void When_JsonDateNotValidWithStartRange_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DRNG07, exception.getCode()); + assertEquals(RNDSTA03, exception.getCode()); exception.printStackTrace(); } @@ -448,9 +454,9 @@ public void When_JsonDateNotValidWithEndRange_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DRNG05, exception.getCode()); + assertEquals(RNDEND03, exception.getCode()); exception.printStackTrace(); } @@ -473,9 +479,9 @@ public void When_JsonTimeNotValidWithBothRange_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DRNG02, exception.getCode()); + assertEquals(RNDSTA02, exception.getCode()); exception.printStackTrace(); } @@ -497,9 +503,9 @@ public void When_JsonTimeNotValidWithStartRange_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DRNG08, exception.getCode()); + assertEquals(RNDSTA04, exception.getCode()); exception.printStackTrace(); } @@ -522,9 +528,9 @@ public void When_JsonTimeNotValidWithEndRange_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DRNG06, exception.getCode()); + assertEquals(RNDEND04, exception.getCode()); exception.printStackTrace(); } @@ -545,9 +551,9 @@ public void When_SchemaDateNotValidWithStartRange_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DSYM01, exception.getCode()); + assertEquals(SYMBDT01, exception.getCode()); exception.printStackTrace(); } @@ -557,7 +563,7 @@ public void When_SchemaTimeNotValidWithEndRange_ExceptionThrown() { """ %pragma TimeDataTypeFormat: "DD-MM-YYYY hh:mm:ss" %schema: - @range*(!, "31-12-2010 23.59.59") #time* #array + @range*(!, "31.12.2010 23.59.59") #time* #array """; var json = """ @@ -568,9 +574,115 @@ public void When_SchemaTimeNotValidWithEndRange_ExceptionThrown() { ] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DSYM01, exception.getCode()); + assertEquals(SYMBDT01, exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_EnableContextualExceptionWithError_ExceptionThrown() { + var schema = + """ + %pragma EnableContextualException: true + %schema: { + "user": { + "userName": @length(5, 20) #string, + "profile": { + "email": @email #string + } + } + } + """; + var json = + """ + { + "user": { + "userName": "example_user_name", + "profile": { + "email": "invalid email address" + } + } + } + """; + var jsonSchema = new JsonSchema(schema); + assertFalse(jsonSchema.isValid(json)); + assertEquals(4, jsonSchema.writeError()); + var exception = assertThrows(FunctionValidationException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals(EMALCF01, exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_DisableContextualExceptionWithError_ExceptionThrown() { + var schema = + """ + // By default contextual exception is disabled + // %pragma EnableContextualException: false + %schema: { + "user": { + "userName": @length(5, 20) #string, + "profile": { + "email": @email #string + } + } + } + """; + var json = + """ + { + "user": { + "userName": "example_user_name", + "profile": { + "email": "invalid email address" + } + } + } + """; + var jsonSchema = new JsonSchema(schema); + assertFalse(jsonSchema.isValid(json)); + assertEquals(1, jsonSchema.writeError()); + var exception = assertThrows(FunctionValidationException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals(EMALCF01, exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_CustomOutlineMaxLengthWithError_ExceptionThrown() { + var schema = + """ + %pragma EnableContextualException: true + %pragma OutlineMaximumLength: 30 + %schema: { + "user": { + "userName": @length(5, 20) #string, + "profile": { + "phoneNumber": @phone #string + } + } + } + """; + var json = + """ + { + "user": { + "userName": "example_user_name", + "profile": { + "phoneNumber": "invalid phone number" + } + } + } + """; + var jsonSchema = new JsonSchema(schema); + assertFalse(jsonSchema.isValid(json)); + assertEquals(4, jsonSchema.writeError()); + for(Exception e : jsonSchema.getExceptions()) + assertTrue(e.getMessage().length() < 190); + var exception = assertThrows(FunctionValidationException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals(PHONCF01, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/ReceiverTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/ReceiverTests.java index 8c82d67..67fd956 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/ReceiverTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/ReceiverTests.java @@ -2,17 +2,17 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.exception.InvalidReceiverStateException; -import com.relogiclabs.jschema.exception.JsonSchemaException; import com.relogiclabs.jschema.exception.ReceiverNotFoundException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.RECV01; -import static com.relogiclabs.jschema.message.ErrorCode.RECV02; -import static com.relogiclabs.jschema.test.external.ExternalFunctions.CONDFUNC01; -import static com.relogiclabs.jschema.test.external.ExternalFunctions.CONDFUNC02; -import static com.relogiclabs.jschema.test.external.ExternalFunctions.MINMAX01; -import static com.relogiclabs.jschema.test.external.ExternalFunctions.SUMEQUAL01; +import static com.relogiclabs.jschema.message.ErrorCode.RECVER01; +import static com.relogiclabs.jschema.message.ErrorCode.RECVER02; +import static com.relogiclabs.jschema.test.external.ExternalFunctions.EX_CONDFUNC01; +import static com.relogiclabs.jschema.test.external.ExternalFunctions.EX_CONDFUNC02; +import static com.relogiclabs.jschema.test.external.ExternalFunctions.EX_MINMAX01; +import static com.relogiclabs.jschema.test.external.ExternalFunctions.EX_SUMEQUAL01; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -38,7 +38,7 @@ public void When_WrongReceiverNameInObject_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(ReceiverNotFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(RECV01, exception.getCode()); + assertEquals(RECVER01, exception.getCode()); exception.printStackTrace(); } @@ -64,7 +64,7 @@ public void When_NoValueReceiveInObject_ExceptionThrown() { //This exception is avoidable inside @condition when needed var exception = assertThrows(InvalidReceiverStateException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(RECV02, exception.getCode()); + assertEquals(RECVER02, exception.getCode()); exception.printStackTrace(); } @@ -88,14 +88,14 @@ public void When_ConditionFailedInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(CONDFUNC01, exception.getCode()); + assertEquals(EX_CONDFUNC01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_ConditionAllFailedInObject_ExceptionThrown() { + public void When_ConditionManyFailedInObject_ExceptionThrown() { var schema = """ %import: com.relogiclabs.jschema.test.external.ExternalFunctions @@ -104,7 +104,7 @@ public void When_ConditionAllFailedInObject_ExceptionThrown() { %schema: { "key1": #integer*($numbers) #array, - "key2": @conditionAll(&relatedValues) #integer + "key2": @conditionMany(&relatedValues) #integer } """; var json = @@ -115,9 +115,9 @@ public void When_ConditionAllFailedInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(CONDFUNC02, exception.getCode()); + assertEquals(EX_CONDFUNC02, exception.getCode()); exception.printStackTrace(); } @@ -148,9 +148,9 @@ public void When_ReceiveWrongValuesInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(SUMEQUAL01, exception.getCode()); + assertEquals(EX_SUMEQUAL01, exception.getCode()); exception.printStackTrace(); } @@ -176,9 +176,9 @@ public void When_MultiReceiverFunctionWrongValuesInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MINMAX01, exception.getCode()); + assertEquals(EX_MINMAX01, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/ScriptBasicTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/ScriptBasicTests.java index 7b46f87..37decfa 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/ScriptBasicTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/ScriptBasicTests.java @@ -2,13 +2,16 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; -import com.relogiclabs.jschema.exception.ScriptRuntimeException; +import com.relogiclabs.jschema.exception.DuplicateVariableException; +import com.relogiclabs.jschema.exception.ScriptIteratorException; import com.relogiclabs.jschema.exception.SystemOperationException; +import com.relogiclabs.jschema.exception.VariableNotFoundException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.DIVD02; -import static com.relogiclabs.jschema.message.ErrorCode.ITER01; -import static com.relogiclabs.jschema.message.ErrorCode.VARD01; +import static com.relogiclabs.jschema.message.ErrorCode.ITERSE01; +import static com.relogiclabs.jschema.message.ErrorCode.OPDIVD02; +import static com.relogiclabs.jschema.message.ErrorCode.VARDUP01; +import static com.relogiclabs.jschema.message.ErrorCode.VARRES01; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -42,9 +45,37 @@ constraint variableTest() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(DuplicateVariableException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(VARD01, exception.getCode()); + assertEquals(VARDUP01, exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_VariableDeclarationNotFound_ExceptionThrown() { + var schema = + """ + %schema: + { + "variableTest": @variableTest #integer + } + %script: { + constraint variableTest() { + var test = 10; + print(test2); //test2 is not declared + } + } + """; + var json = + """ + { + "variableTest": 2 + } + """; + JsonSchema.isValid(schema, json); + var exception = assertThrows(VariableNotFoundException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals(VARRES01, exception.getCode()); exception.printStackTrace(); } @@ -69,9 +100,9 @@ constraint iteratorTest() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(ScriptIteratorException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ITER01, exception.getCode()); + assertEquals(ITERSE01, exception.getCode()); exception.printStackTrace(); } @@ -87,33 +118,33 @@ public void When_WrongIndexAndRangeOutOfBounds_ExceptionThrown() { %script: { constraint stringTest() { var result1 = tryof(target[100]); - if(!result1.error.find("[SIDX01]")) throw("Invalid: " + target); + if(!result1.error.find("[STRIDX01]")) throw("Invalid: " + target); var result2 = tryof(target[-1]); - if(!result2.error.find("[SIDX02]")) throw("Invalid: " + target); + if(!result2.error.find("[STRIDX02]")) throw("Invalid: " + target); var result3 = tryof(target[100..]); - if(!result3.error.find("[SRNG01]")) throw("Invalid: " + target); + if(!result3.error.find("[STRRNG01]")) throw("Invalid: " + target); var result4 = tryof(target[..100]); - if(!result4.error.find("[SRNG02]")) throw("Invalid: " + target); + if(!result4.error.find("[STRRNG02]")) throw("Invalid: " + target); var result5 = tryof(target[8..6]); - if(!result5.error.find("[SRNG03]")) throw("Invalid: " + target); + if(!result5.error.find("[STRRNG03]")) throw("Invalid: " + target); } constraint arrayTest() { var result1 = tryof(target[10]); - if(!result1.error.find("[AIDX01]")) throw("Invalid: " + target); + if(!result1.error.find("[ARRIDX01]")) throw("Invalid: " + target); var result2 = tryof(target[-1]); - if(!result2.error.find("[AIDX02]")) throw("Invalid: " + target); + if(!result2.error.find("[ARRIDX02]")) throw("Invalid: " + target); var result3 = tryof(target[10..]); - if(!result3.error.find("[ARNG01]")) throw("Invalid: " + target); + if(!result3.error.find("[ARRRNG01]")) throw("Invalid: " + target); var result4 = tryof(target[..10]); - if(!result4.error.find("[ARNG02]")) throw("Invalid: " + target); + if(!result4.error.find("[ARRRNG02]")) throw("Invalid: " + target); var result5 = tryof(target[-2..-4]); - if(!result5.error.find("[ARNG03]")) throw("Invalid: " + target); + if(!result5.error.find("[ARRRNG03]")) throw("Invalid: " + target); var array = [0, 1]; // only assign at the end of array to add // use fill method for array of specific size var result6 = tryof(array[10] = 10); - if(!result6.error.find("[AWRT01]")) throw("Invalid: " + array); + if(!result6.error.find("[ARNSRT02]")) throw("Invalid: " + array); } } """; @@ -141,33 +172,33 @@ public void When_WrongTypeForDifferentOperations_ExceptionThrown() { %script: { constraint unaryOperation() { var result1 = tryof(+target); - if(!result1.error.find("[PLUS01]")) throw("Invalid: " + target); + if(!result1.error.find("[OPPLUS01]")) throw("Invalid: " + target); var result2 = tryof(-target); - if(!result2.error.find("[MINS01]")) throw("Invalid: " + target); + if(!result2.error.find("[OPMINS01]")) throw("Invalid: " + target); } constraint binaryOperation() { var result1 = tryof(target + 10); - if(!result1.error.find("[ADDT01]")) throw("Invalid: " + target); + if(!result1.error.find("[OPADDT01]")) throw("Invalid: " + target); var result2 = tryof(target - 10); - if(!result2.error.find("[SUBT01]")) throw("Invalid: " + target); + if(!result2.error.find("[OPSUBT01]")) throw("Invalid: " + target); var result3 = tryof(target * 10); - if(!result3.error.find("[MULT01]")) throw("Invalid: " + target); + if(!result3.error.find("[OPMULT01]")) throw("Invalid: " + target); var result4 = tryof(target / 10); - if(!result4.error.find("[DIVD01]")) throw("Invalid: " + target); + if(!result4.error.find("[OPDIVD01]")) throw("Invalid: " + target); var result5 = tryof(target % 10); - if(!result5.error.find("[MODU01]")) throw("Invalid: " + target); + if(!result5.error.find("[OPMODU01]")) throw("Invalid: " + target); } constraint comparisonOperation() { var result1 = tryof(target > 10); - if(!result1.error.find("[RELA01]")) throw("Invalid: " + target); + if(!result1.error.find("[CMPSGT01]")) throw("Invalid: " + target); var result2 = tryof(target >= 10); - if(!result2.error.find("[RELA02]")) throw("Invalid: " + target); + if(!result2.error.find("[CMPSGE01]")) throw("Invalid: " + target); var result3 = tryof(target < 10); - if(!result3.error.find("[RELA03]")) throw("Invalid: " + target); + if(!result3.error.find("[CMPSLT01]")) throw("Invalid: " + target); var result4 = tryof(target <= 10); - if(!result4.error.find("[RELA04]")) throw("Invalid: " + target); + if(!result4.error.find("[CMPSLE01]")) throw("Invalid: " + target); } } """; @@ -196,13 +227,13 @@ public void When_WrongLeftValueForIncrementDecrement_ExceptionThrown() { constraint incDecTest() { var t = target; var result1 = tryof(t++); - if(!result1.error.find("[INCT02]")) throw("Invalid: " + target); + if(!result1.error.find("[INCPST02]")) throw("Invalid: " + target); var result2 = tryof(++t); - if(!result2.error.find("[INCE02]")) throw("Invalid: " + target); + if(!result2.error.find("[INCPRE02]")) throw("Invalid: " + target); var result3 = tryof(t--); - if(!result3.error.find("[DECT02]")) throw("Invalid: " + target); + if(!result3.error.find("[DECPST02]")) throw("Invalid: " + target); var result4 = tryof(--t); - if(!result4.error.find("[DECE02]")) throw("Invalid: " + target); + if(!result4.error.find("[DECPRE02]")) throw("Invalid: " + target); } } """; @@ -230,24 +261,24 @@ public void When_ReadonlyLeftValueForUpdateOperations_ExceptionThrown() { %script: { constraint lvalueTest1() { var result1 = tryof(target[2] = 10); - if(!result1.error.find("[AUPD01]")) throw("Invalid: " + target); + if(!result1.error.find("[ROARRY01]")) throw("Invalid: " + target); var result2 = tryof(target[0] = 10); - if(!result2.error.find("[AUPD01]")) throw("Invalid: " + target); + if(!result2.error.find("[ROARRY01]")) throw("Invalid: " + target); } constraint lvalueTest2() { var result1 = tryof(target.test = 10); - if(!result1.error.find("[OUPD01]")) throw("Invalid: " + target); + if(!result1.error.find("[ROOBJT01]")) throw("Invalid: " + target); var result2 = tryof(target.k1 = 10); - if(!result2.error.find("[OUPD01]")) throw("Invalid: " + target); + if(!result2.error.find("[ROOBJT01]")) throw("Invalid: " + target); } constraint lvalueTest3() { // String values are designed to be immutable var result1 = tryof(target[1] = "f"); - if(!result1.error.find("[SASN01]")) throw("Invalid: " + target); + if(!result1.error.find("[STRASN01]")) throw("Invalid: " + target); var result2 = tryof(target[11] = "f"); - if(!result2.error.find("[SASN01]")) throw("Invalid: " + target); + if(!result2.error.find("[STRASN01]")) throw("Invalid: " + target); } } """; @@ -277,41 +308,41 @@ public void When_WrongLeftValueForDifferentOperations_ExceptionThrown() { %script: { constraint lvalueTest1() { var result1 = tryof(target.test); - if(!result1.error.find("[PRPT01]")) throw("Invalid: " + target); + if(!result1.error.find("[OPPRTY01]")) throw("Invalid: " + target); var result2 = tryof(target["test"]); - if(!result2.error.find("[BKTR01]")) throw("Invalid: " + target); + if(!result2.error.find("[REDBKT01]")) throw("Invalid: " + target); } constraint lvalueTest2() { var result1 = tryof(target[0]); - if(!result1.error.find("[IDXR01]")) throw("Invalid: " + target); + if(!result1.error.find("[REDIDX01]")) throw("Invalid: " + target); var result2 = tryof(target[0..]); - if(!result2.error.find("[RNGR01]")) throw("Invalid: " + target); + if(!result2.error.find("[REDRNG01]")) throw("Invalid: " + target); } constraint lvalueTest3() { var result1 = tryof(target.test); - if(!result1.error.find("[PRPT01]")) throw("Invalid: " + target); + if(!result1.error.find("[OPPRTY01]")) throw("Invalid: " + target); var result2 = tryof(target[0]); - if(!result2.error.find("[IDXR01]")) throw("Invalid: " + target); + if(!result2.error.find("[REDIDX01]")) throw("Invalid: " + target); var result3 = tryof(target[0..]); - if(!result3.error.find("[RNGR01]")) throw("Invalid: " + target); + if(!result3.error.find("[REDRNG01]")) throw("Invalid: " + target); var result4 = tryof(target[0] = 0); - if(!result4.error.find("[IDXA01]")) throw("Invalid: " + target); + if(!result4.error.find("[IDXASN01]")) throw("Invalid: " + target); var result5 = tryof(target[0..] = 10); - if(!result5.error.find("[RNGA01]")) throw("Invalid: " + target); + if(!result5.error.find("[RNGASN01]")) throw("Invalid: " + target); } constraint lvalueTest4() { var string = "Strings are designed to be immutable"; var result1 = tryof(string[1] = "f"); - if(!result1.error.find("[SASN01]")) throw("Invalid: " + string); + if(!result1.error.find("[STRASN01]")) throw("Invalid: " + string); var array = [10, 20, 30, 40]; // an array range update is not supported var result2 = tryof(array[1..3]++); - if(!result2.error.find("[ARUD01]")) throw("Invalid: " + array); + if(!result2.error.find("[ARRUPD01]")) throw("Invalid: " + array); var result3 = tryof(array[1..3] = "test"); - if(!result3.error.find("[ARAS01]")) throw("Invalid: " + array); + if(!result3.error.find("[ARRASN01]")) throw("Invalid: " + array); } } """; @@ -329,7 +360,7 @@ constraint lvalueTest4() { } @Test - public void When_InvalidRangeSetupForWrongBoundaryValue_ExceptionThrown() { + public void When_InvalidRangeForWrongBoundaryValueType_ExceptionThrown() { var schema = """ %schema: @@ -339,11 +370,11 @@ public void When_InvalidRangeSetupForWrongBoundaryValue_ExceptionThrown() { %script: { constraint testRangeSetup() { var result1 = tryof(target..); - if(!result1.error.find("[RNGT01]")) throw("Invalid: " + target); + if(!result1.error.find("[OPRNGT01]")) throw("Invalid: " + target); var result2 = tryof(5..target); - if(!result2.error.find("[RNGT03]")) throw("Invalid: " + target); + if(!result2.error.find("[OPRNGT03]")) throw("Invalid: " + target); var result3 = tryof(..target); - if(!result3.error.find("[RNGT05]")) throw("Invalid: " + target); + if(!result3.error.find("[OPRNGT05]")) throw("Invalid: " + target); } } """; @@ -381,7 +412,7 @@ constraint exceptionTest() { JsonSchema.isValid(schema, json); var exception = assertThrows(SystemOperationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DIVD02, exception.getCode()); + assertEquals(OPDIVD02, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/ScriptFunctionTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/ScriptFunctionTests.java index ad12583..2561a28 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/ScriptFunctionTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/ScriptFunctionTests.java @@ -2,20 +2,29 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; +import com.relogiclabs.jschema.exception.DuplicateFunctionException; +import com.relogiclabs.jschema.exception.FunctionArgumentTypeException; +import com.relogiclabs.jschema.exception.FunctionArgumentValueException; import com.relogiclabs.jschema.exception.FunctionNotFoundException; -import com.relogiclabs.jschema.exception.ScriptRuntimeException; +import com.relogiclabs.jschema.exception.InvalidContextException; +import com.relogiclabs.jschema.exception.InvalidReturnTypeException; +import com.relogiclabs.jschema.exception.InvocationRuntimeException; +import com.relogiclabs.jschema.exception.MethodArgumentTypeException; +import com.relogiclabs.jschema.exception.MethodNotFoundException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.CALR01; -import static com.relogiclabs.jschema.message.ErrorCode.FAIL09; -import static com.relogiclabs.jschema.message.ErrorCode.FNVK01; -import static com.relogiclabs.jschema.message.ErrorCode.FNVK02; -import static com.relogiclabs.jschema.message.ErrorCode.FUNC05; -import static com.relogiclabs.jschema.message.ErrorCode.FUND01; -import static com.relogiclabs.jschema.message.ErrorCode.FUND02; -import static com.relogiclabs.jschema.message.ErrorCode.MNVK01; -import static com.relogiclabs.jschema.message.ErrorCode.RETN01; -import static com.relogiclabs.jschema.message.ErrorCode.TRGT01; +import static com.relogiclabs.jschema.message.ErrorCode.CALRSE01; +import static com.relogiclabs.jschema.message.ErrorCode.FAILBS01; +import static com.relogiclabs.jschema.message.ErrorCode.FAILMV01; +import static com.relogiclabs.jschema.message.ErrorCode.FNCDEF01; +import static com.relogiclabs.jschema.message.ErrorCode.FNDSTR01; +import static com.relogiclabs.jschema.message.ErrorCode.FNSDUP01; +import static com.relogiclabs.jschema.message.ErrorCode.FNSDUP02; +import static com.relogiclabs.jschema.message.ErrorCode.FNSNVK01; +import static com.relogiclabs.jschema.message.ErrorCode.FNSNVK02; +import static com.relogiclabs.jschema.message.ErrorCode.MTHNVK01; +import static com.relogiclabs.jschema.message.ErrorCode.RETNSE02; +import static com.relogiclabs.jschema.message.ErrorCode.TRGTSE01; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -26,7 +35,7 @@ public void When_ConstraintFunctionNotExists_ExceptionThrown() { """ %schema: { - "funcTest": @funcTest #integer + "funcTest": @notExists #integer } """; var json = @@ -38,7 +47,7 @@ public void When_ConstraintFunctionNotExists_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(FunctionNotFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUNC05, exception.getCode()); + assertEquals(FNCDEF01, exception.getCode()); exception.printStackTrace(); } @@ -52,8 +61,10 @@ public void When_SubroutineFunctionNotExists_ExceptionThrown() { } %script: { + // constraint functions are available to schema context + // subroutine functions are not available to schema context constraint funcTest() { - subroutineFunction(target); + subroutineNotExists(target); } } """; @@ -64,9 +75,9 @@ constraint funcTest() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(FunctionNotFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FNVK01, exception.getCode()); + assertEquals(FNSNVK01, exception.getCode()); exception.printStackTrace(); } @@ -96,9 +107,9 @@ subroutine function testFunction(p1, p2, p3...) { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(InvocationRuntimeException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FNVK02, exception.getCode()); + assertEquals(FNSNVK02, exception.getCode()); exception.printStackTrace(); } @@ -111,8 +122,6 @@ public void When_InvalidReturnTypeOfConstraint_ExceptionThrown() { "funcTest": @funcTest #integer } %script: { - // constraint functions are available to schema context - // subroutine functions are not available to schema context constraint function funcTest() { return 10; } @@ -125,9 +134,9 @@ constraint function funcTest() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(InvalidReturnTypeException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(RETN01, exception.getCode()); + assertEquals(RETNSE02, exception.getCode()); exception.printStackTrace(); } @@ -163,14 +172,14 @@ subroutine testFunction(p1, p2...) { } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(DuplicateFunctionException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUND02, exception.getCode()); + assertEquals(FNSDUP02, exception.getCode()); exception.printStackTrace(); } @Test - public void When_InvalidBuiltinMethodCallWithWrongType_ExceptionThrown() { + public void When_BuiltinMethodCallOnWrongType_ExceptionThrown() { var schema = """ %schema: @@ -192,9 +201,63 @@ constraint funcTest() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(MethodNotFoundException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(MNVK01, exception.getCode()); + assertEquals(MTHNVK01, exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_BuiltinSubroutineCallWithWrongType_ExceptionThrown() { + var schema = + """ + %schema: + { + "funcTest": @funcTest + } + %script: { + constraint funcTest() { + var result = fail(target); + } + } + """; + var json = + """ + { + "funcTest": 2 + } + """; + JsonSchema.isValid(schema, json); + var exception = assertThrows(FunctionArgumentTypeException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals(FAILBS01, exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_BuiltinMethodCallWithWrongType_ExceptionThrown() { + var schema = + """ + %schema: + { + "methodTest": @methodTest + } + %script: { + constraint methodTest() { + var result = target.find(-10); + } + } + """; + var json = + """ + { + "methodTest": "test string method" + } + """; + JsonSchema.isValid(schema, json); + var exception = assertThrows(MethodArgumentTypeException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals(FNDSTR01, exception.getCode()); exception.printStackTrace(); } @@ -208,7 +271,7 @@ public void When_InvalidArgumentValueWithBuiltinSubroutine_ExceptionThrown() { } %script: { constraint funcTest() { - return fail("ERR01", "Test Message", { node: null }, {}); + return fail("EX_ERR01", "Test Message", { node: null }, {}); } } """; @@ -219,9 +282,9 @@ constraint funcTest() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(FunctionArgumentValueException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FAIL09, exception.getCode()); + assertEquals(FAILMV01, exception.getCode()); exception.printStackTrace(); } @@ -237,8 +300,7 @@ public void When_ScriptInitiatedCallNotFoundTargetNode_ExceptionThrown() { // target is only available inside subroutine from call stack // when call initiated from schema but not from script subroutine targetTest() { - var test = target; - return 5; + var result = target; } var test = targetTest(); } @@ -250,9 +312,9 @@ subroutine targetTest() { } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(InvalidContextException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(TRGT01, exception.getCode()); + assertEquals(TRGTSE01, exception.getCode()); exception.printStackTrace(); } @@ -268,8 +330,7 @@ public void When_ScriptInitiatedCallNotFoundCallerNode_ExceptionThrown() { // caller is only available inside subroutine from call stack // when call initiated from schema but not from script subroutine callerTest() { - var test = caller; - return 5; + var result = caller; } var test = callerTest(); } @@ -281,9 +342,9 @@ subroutine callerTest() { } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(InvalidContextException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(CALR01, exception.getCode()); + assertEquals(CALRSE01, exception.getCode()); exception.printStackTrace(); } @@ -293,7 +354,7 @@ public void When_DuplicateConstraintDefinitionsConflict_ExceptionThrown() { """ %schema: { - "funcTest": @funcTest(5) + "funcTest": @funcTest(3) } %script: { constraint funcTest(param1) { @@ -308,13 +369,13 @@ constraint funcTest(param1) { var json = """ { - "funcTest": 10 + "funcTest": 2 } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(DuplicateFunctionException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUND01, exception.getCode()); + assertEquals(FNSDUP01, exception.getCode()); exception.printStackTrace(); } @@ -324,7 +385,7 @@ public void When_DuplicateConstraintDefinitionsWithFutureConflict_ExceptionThrow """ %schema: { - "funcTest": @funcTest(5) + "funcTest": @funcTest(3) } %script: { constraint funcTest(param1) { @@ -340,13 +401,13 @@ future constraint funcTest(param1) { var json = """ { - "funcTest": 10 + "funcTest": 2 } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(DuplicateFunctionException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUND01, exception.getCode()); + assertEquals(FNSDUP01, exception.getCode()); exception.printStackTrace(); } @@ -356,11 +417,11 @@ public void When_DuplicateSubroutineDefinitionsConflict_ExceptionThrown() { """ %schema: { - "funcTest": @funcTest(5) + "funcTest": @funcTest(3) } %script: { constraint funcTest(param1) { - return true; + return funcTest(param1); } // Constraint functions are special functions and are not callable @@ -377,13 +438,13 @@ subroutine funcTest(param1) { var json = """ { - "funcTest": 10 + "funcTest": 2 } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptRuntimeException.class, + var exception = assertThrows(DuplicateFunctionException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(FUND02, exception.getCode()); + assertEquals(FNSDUP02, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/ScriptGeneralTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/ScriptGeneralTests.java index 258abf3..820355f 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/ScriptGeneralTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/ScriptGeneralTests.java @@ -2,10 +2,12 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; -import com.relogiclabs.jschema.exception.JsonSchemaException; -import com.relogiclabs.jschema.exception.ScriptInitiatedException; +import com.relogiclabs.jschema.exception.FunctionValidationException; +import com.relogiclabs.jschema.exception.ScriptThrowInitiatedException; import org.junit.jupiter.api.Test; +import static com.relogiclabs.jschema.message.ErrorCode.FAILDF01; +import static com.relogiclabs.jschema.message.ErrorCode.THRODF01; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -22,11 +24,11 @@ public void When_WrongValueConditionUnsatisfiedV1_ExceptionThrown() { %script: { future constraint checkAccess(role) { if(role == "user" && target > 5) return fail( - "ERRACCESS01", "Data access incompatible with 'user' role", + "EX_ERRACCESS01", "Data access incompatible with 'user' role", // 'caller' is the default node added automatically expected("an access at most 5 for 'user' role"), // 'target' is the default node added automatically - actual("found access " + target + " which is greater than 5")); + actual("found access " + target + " that is greater than 5")); } } """; @@ -38,9 +40,9 @@ future constraint checkAccess(role) { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals("ERRACCESS01", exception.getCode()); + assertEquals("EX_ERRACCESS01", exception.getCode()); exception.printStackTrace(); } @@ -56,11 +58,11 @@ public void When_WrongValueConditionUnsatisfiedV2_ExceptionThrown() { %script: { future constraint checkAccess(role) { if(role == "user" && target > 5) return fail( - "ERRACCESS01", "Data access incompatible with 'user' role", + "EX_ERRACCESS01", "Data access incompatible with 'user' role", // Pass any node explicitly to the expected function expected(caller, "an access at most 5 for 'user' role"), // Pass any node explicitly to the actual function - actual(target, "found access " + target + " which is greater than 5")); + actual(target, "found access " + target + " that is greater than 5")); } } """; @@ -72,9 +74,9 @@ future constraint checkAccess(role) { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals("ERRACCESS01", exception.getCode()); + assertEquals("EX_ERRACCESS01", exception.getCode()); exception.printStackTrace(); } @@ -90,12 +92,12 @@ public void When_WrongValueConditionUnsatisfiedV3_ExceptionThrown() { %script: { constraint checkAccess(role) { if(role == "user" && target > 5) return fail( - "ERRACCESS01", "Data access incompatible with 'user' role", + "EX_ERRACCESS01", "Data access incompatible with 'user' role", // Create an expected object explicitly without any function { node: caller, message: "an access at most 5 for 'user' role" }, // Create an actual object explicitly without any function { node: target, message: "found access " + target - + " which is greater than 5" }); + + " that is greater than 5" }); } } """; @@ -107,9 +109,9 @@ constraint checkAccess(role) { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals("ERRACCESS01", exception.getCode()); + assertEquals("EX_ERRACCESS01", exception.getCode()); exception.printStackTrace(); } @@ -126,7 +128,7 @@ public void When_WrongValueConditionUnsatisfiedV4_ExceptionThrown() { future constraint checkAccess(role) { // Fail with simple message and a code if(role == "user" && target > 5) return fail( - "ERRACCESS01", "Data access incompatible with 'user' role"); + "EX_ERRACCESS01", "Data access incompatible with 'user' role"); } } """; @@ -138,9 +140,9 @@ future constraint checkAccess(role) { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptInitiatedException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals("ERRACCESS01", exception.getCode()); + assertEquals("EX_ERRACCESS01", exception.getCode()); exception.printStackTrace(); } @@ -169,10 +171,10 @@ future constraint checkAccess(role) { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptInitiatedException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - // FAIL01 is the default code if no code provided - assertEquals("FAIL01", exception.getCode()); + // FAILDF01 is the default code if no code provided + assertEquals(FAILDF01, exception.getCode()); exception.printStackTrace(); } @@ -188,8 +190,8 @@ public void When_ThrowFromScriptWithCode_ExceptionThrown() { future constraint throwTest() { if(target.type() != "#array") return fail("Invalid: " + target); if(!target.find(45.5)) return fail("Invalid: " + target); - if(target[1] == 20) throw("ERROR01", "Throw test with code"); - return fail("NOTTHRO01", "Exception not thrown"); + if(target[1] == 20) throw("EX_ERROR01", "Throw test with code"); + return fail("EX_NOTTHRO01", "Exception not thrown"); } } """; @@ -200,9 +202,9 @@ future constraint throwTest() { } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptInitiatedException.class, + var exception = assertThrows(ScriptThrowInitiatedException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals("ERROR01", exception.getCode()); + assertEquals("EX_ERROR01", exception.getCode()); exception.printStackTrace(); } @@ -221,7 +223,7 @@ future constraint throwTest() { if(c1 != 10) return fail("Invalid: " + target); if(c2 != 45.5) return fail("Invalid: " + target); if(target[2] == 30) throw("Throw test without code"); - return fail("NOTTHRO01", "Exception not thrown"); + return fail("EX_NOTTHRO01", "Exception not thrown"); } } """; @@ -232,10 +234,10 @@ future constraint throwTest() { } """; //JsonSchema.isValid(schema, json); - var exception = assertThrows(ScriptInitiatedException.class, + var exception = assertThrows(ScriptThrowInitiatedException.class, () -> JsonAssert.isValid(schema, json)); - // THRO01 is the default code if no code provided - assertEquals("THRO01", exception.getCode()); + // THRODF01 is the default code if no code provided + assertEquals(THRODF01, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/ScriptLiteralTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/ScriptLiteralTests.java new file mode 100644 index 0000000..809c362 --- /dev/null +++ b/src/test/java/com/relogiclabs/jschema/test/negative/ScriptLiteralTests.java @@ -0,0 +1,123 @@ +package com.relogiclabs.jschema.test.negative; + +import com.relogiclabs.jschema.JsonAssert; +import com.relogiclabs.jschema.JsonSchema; +import com.relogiclabs.jschema.exception.FunctionValidationException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ScriptLiteralTests { + @Test + public void When_CheckUndefinedFromSchemaWithWrongJson_ExceptionThrown() { + var schema = + """ + %schema: + { + "value1": @checkRange(10, !) #integer, + "value2": @checkRange(!, 1000) #integer + } + %script: { + future constraint checkRange(min, max) { + if(min != undefined && target < min) return fail( + "EX_RANGEERR01", "Target number is less than minimum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); + if(max != undefined && target > max) return fail( + "EX_RANGEERR02", "Target number is greater than maximum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); + } + } + """; + var json = + """ + { + "value1": 9, + "value2": 1100 + } + """; + JsonSchema.isValid(schema, json); + var exception = assertThrows(FunctionValidationException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals("EX_RANGEERR01", exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_CheckNullFromSchemaWithWrongJson_ExceptionThrown() { + var schema = + """ + %schema: + { + "value1": @checkRange(10, null) #integer, + "value2": @checkRange(null, 10000) #integer + } + %script: { + future constraint checkRange(min, max) { + if(min != null && target < min) return fail( + "EX_RANGEERR01", "Target number is less than minimum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); + if(max != null && target > max) return fail( + "EX_RANGEERR02", "Target number is greater than maximum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); + } + } + """; + var json = + """ + { + "value1": 5, + "value2": 12200 + } + """; + JsonSchema.isValid(schema, json); + var exception = assertThrows(FunctionValidationException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals("EX_RANGEERR01", exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_CheckNullOrUndefinedFromSchemaWithWrongJson_ExceptionThrown() { + var schema = + """ + %schema: + { + "value1": @checkRange(200, 1000) #integer, + "value2": @checkRange(200, 1000) #integer, + "value3": @checkRange(20, !) #integer, + "value4": @checkRange(null, 1000) #integer + } + %script: { + future constraint checkRange(min, max) { + if(min && target < min) return fail( + "EX_RANGEERR01", "Target number is less than minimum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); + if(max && target > max) return fail( + "EX_RANGEERR02", "Target number is greater than maximum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); + } + } + """; + var json = + """ + { + "value1": -100, + "value2": 1001, + "value3": 19, + "value4": 12202 + } + """; + JsonSchema.isValid(schema, json); + var exception = assertThrows(FunctionValidationException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals("EX_RANGEERR01", exception.getCode()); + exception.printStackTrace(); + } +} \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/negative/StringTests.java b/src/test/java/com/relogiclabs/jschema/test/negative/StringTests.java index 4982141..9f01351 100644 --- a/src/test/java/com/relogiclabs/jschema/test/negative/StringTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/negative/StringTests.java @@ -2,26 +2,29 @@ import com.relogiclabs.jschema.JsonAssert; import com.relogiclabs.jschema.JsonSchema; +import com.relogiclabs.jschema.exception.DataTypeValidationException; +import com.relogiclabs.jschema.exception.FunctionValidationException; import com.relogiclabs.jschema.exception.JsonLexerException; -import com.relogiclabs.jschema.exception.JsonSchemaException; import com.relogiclabs.jschema.exception.SchemaLexerException; import org.junit.jupiter.api.Test; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP04; -import static com.relogiclabs.jschema.message.ErrorCode.DTYP06; -import static com.relogiclabs.jschema.message.ErrorCode.EMAL01; -import static com.relogiclabs.jschema.message.ErrorCode.ENUM01; -import static com.relogiclabs.jschema.message.ErrorCode.JLEX01; -import static com.relogiclabs.jschema.message.ErrorCode.NEMT01; -import static com.relogiclabs.jschema.message.ErrorCode.PHON01; -import static com.relogiclabs.jschema.message.ErrorCode.REGX01; -import static com.relogiclabs.jschema.message.ErrorCode.SLEN01; -import static com.relogiclabs.jschema.message.ErrorCode.SLEN03; -import static com.relogiclabs.jschema.message.ErrorCode.SLEN04; -import static com.relogiclabs.jschema.message.ErrorCode.SLEN05; -import static com.relogiclabs.jschema.message.ErrorCode.SLEX01; -import static com.relogiclabs.jschema.message.ErrorCode.URLA01; -import static com.relogiclabs.jschema.message.ErrorCode.URLA04; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS01; +import static com.relogiclabs.jschema.message.ErrorCode.DTYPMS02; +import static com.relogiclabs.jschema.message.ErrorCode.EMALCF01; +import static com.relogiclabs.jschema.message.ErrorCode.EMPTCF01; +import static com.relogiclabs.jschema.message.ErrorCode.ENMSTR01; +import static com.relogiclabs.jschema.message.ErrorCode.IPV4CF01; +import static com.relogiclabs.jschema.message.ErrorCode.IPV6CF01; +import static com.relogiclabs.jschema.message.ErrorCode.JSNLEX01; +import static com.relogiclabs.jschema.message.ErrorCode.LENSTR01; +import static com.relogiclabs.jschema.message.ErrorCode.LENSTR03; +import static com.relogiclabs.jschema.message.ErrorCode.LENSTR04; +import static com.relogiclabs.jschema.message.ErrorCode.LENSTR05; +import static com.relogiclabs.jschema.message.ErrorCode.PHONCF01; +import static com.relogiclabs.jschema.message.ErrorCode.REGXCF01; +import static com.relogiclabs.jschema.message.ErrorCode.SCMLEX01; +import static com.relogiclabs.jschema.message.ErrorCode.URLADR01; +import static com.relogiclabs.jschema.message.ErrorCode.URLSCM02; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -32,9 +35,9 @@ public void When_JsonNotString_ExceptionThrown() { var json = "10"; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -51,7 +54,7 @@ public void When_InvalidUnicodeStringInSchema_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(SchemaLexerException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(SLEX01, exception.getCode()); + assertEquals(SCMLEX01, exception.getCode()); exception.printStackTrace(); } @@ -68,7 +71,7 @@ public void When_InvalidUnicodeStringInJson_ExceptionThrown() { //JsonSchema.isValid(schema, json); var exception = assertThrows(JsonLexerException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(JLEX01, exception.getCode()); + assertEquals(JSNLEX01, exception.getCode()); exception.printStackTrace(); } @@ -91,9 +94,9 @@ public void When_JsonNotStringInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @@ -109,14 +112,14 @@ public void When_JsonNotStringInArray_ExceptionThrown() { "Lorem ipsum dolor sit amet, consectetur adipiscing elit"] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP04, exception.getCode()); + assertEquals(DTYPMS01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_NestedJsonNotStringInArray_ExceptionThrown() { + public void When_JsonNotNestedStringInArray_ExceptionThrown() { var schema = """ #string* @@ -126,14 +129,14 @@ public void When_NestedJsonNotStringInArray_ExceptionThrown() { ["value1", 10, "value3"] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @Test - public void When_NestedJsonNotStringInObject_ExceptionThrown() { + public void When_JsonNotNestedStringInObject_ExceptionThrown() { var schema = """ #string* @@ -147,9 +150,9 @@ public void When_NestedJsonNotStringInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(DataTypeValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(DTYP06, exception.getCode()); + assertEquals(DTYPMS02, exception.getCode()); exception.printStackTrace(); } @@ -168,9 +171,9 @@ public void When_WrongLengthWithStringInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(SLEN01, exception.getCode()); + assertEquals(LENSTR01, exception.getCode()); exception.printStackTrace(); } @@ -189,9 +192,9 @@ public void When_NestedWrongLengthWithStringInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(SLEN03, exception.getCode()); + assertEquals(LENSTR03, exception.getCode()); exception.printStackTrace(); } @@ -210,9 +213,9 @@ public void When_NestedWrongLengthWithUndefinedStringInObject_ExceptionThrown() } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(SLEN04, exception.getCode()); + assertEquals(LENSTR04, exception.getCode()); exception.printStackTrace(); } @@ -227,9 +230,9 @@ public void When_NestedWrongLengthWithUndefinedStringInArray_ExceptionThrown() { ["Lorem ipsum dolor sit amet", "value11", "value111"] """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(SLEN05, exception.getCode()); + assertEquals(LENSTR05, exception.getCode()); exception.printStackTrace(); } @@ -246,9 +249,9 @@ public void When_RegexWithWrongStringInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(REGX01, exception.getCode()); + assertEquals(REGXCF01, exception.getCode()); exception.printStackTrace(); } @@ -269,9 +272,9 @@ public void When_EnumWithWrongStringInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(ENUM01, exception.getCode()); + assertEquals(ENMSTR01, exception.getCode()); exception.printStackTrace(); } @@ -292,20 +295,20 @@ public void When_EmailWithWrongStringInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(EMAL01, exception.getCode()); + assertEquals(EMALCF01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_UrlWithWrongStringAddressInObject_ExceptionThrown() { + public void When_UrlWithWrongFormatInObject_ExceptionThrown() { var schema = """ { "key1": @url #string, "key2": @url #string, - "key3": @url("ftps") #string + "key3": @url #string } """; var json = @@ -317,37 +320,39 @@ public void When_UrlWithWrongStringAddressInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(URLA01, exception.getCode()); + assertEquals(URLADR01, exception.getCode()); exception.printStackTrace(); } @Test - public void When_UrlWithSchemeAndWrongStringAddressInObject_ExceptionThrown() { + public void When_UrlWithSchemeMismatchedInObject_ExceptionThrown() { var schema = """ { "key1": @url("ftps") #string, - "key2": @url #string + "key2": @url("https") #string, + "key3": @url("irc") #string } """; var json = """ { "key1": "ssh://www.example.com/test/", - "key2": "ftp://www.example.com/" + "key2": "ftp://www.example.com/", + "key3": "http://www.example.com/test?q=str" } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(URLA04, exception.getCode()); + assertEquals(URLSCM02, exception.getCode()); exception.printStackTrace(); } @Test - public void When_PhoneWithWrongStringInObject_ExceptionThrown() { + public void When_MalformedPhoneNumberInObject_ExceptionThrown() { var schema = """ { @@ -359,15 +364,65 @@ public void When_PhoneWithWrongStringInObject_ExceptionThrown() { var json = """ { - "key1": "Phone 01737048177", + "key1": "Phone: 01737048177", "key2": "+880:1737048177", "key3": "0088/01737048177" } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(PHON01, exception.getCode()); + assertEquals(PHONCF01, exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_InvalidIPv4AddressInObject_ExceptionThrown() { + var schema = + """ + { + "ip1v4": @ipv4 #string, + "ip2v4": @ipv(4) #string, + "ip3vAny": @ipv(4, 6) #string + } + """; + var json = + """ + { + "ip1v4": "13.0.0", + "ip2v4": "13.0.0.256", + "ip3vAny": "13:0:0:0" + } + """; + JsonSchema.isValid(schema, json); + var exception = assertThrows(FunctionValidationException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals(IPV4CF01, exception.getCode()); + exception.printStackTrace(); + } + + @Test + public void When_InvalidIPv6AddressInObject_ExceptionThrown() { + var schema = + """ + { + "ip1v6": @ipv6 #string, + "ip2v6": @ipv(6) #string, + "ip3vAny": @ipv(4, 6) #string + } + """; + var json = + """ + { + "ip1v6": "130d::1310:500c:d01::22", + "ip2v6": "130c:1310:500c:d01:0:0:22", + "ip3vAny": "13cd:500c::192.0.2.256" + } + """; + JsonSchema.isValid(schema, json); + var exception = assertThrows(FunctionValidationException.class, + () -> JsonAssert.isValid(schema, json)); + assertEquals(IPV6CF01, exception.getCode()); exception.printStackTrace(); } @@ -388,9 +443,9 @@ public void When_EmptyStringInObject_ExceptionThrown() { } """; JsonSchema.isValid(schema, json); - var exception = assertThrows(JsonSchemaException.class, + var exception = assertThrows(FunctionValidationException.class, () -> JsonAssert.isValid(schema, json)); - assertEquals(NEMT01, exception.getCode()); + assertEquals(EMPTCF01, exception.getCode()); exception.printStackTrace(); } } \ No newline at end of file diff --git a/src/test/java/com/relogiclabs/jschema/test/positive/AggregatedTests.java b/src/test/java/com/relogiclabs/jschema/test/positive/AggregatedTests.java index a046f03..1bd0c72 100644 --- a/src/test/java/com/relogiclabs/jschema/test/positive/AggregatedTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/positive/AggregatedTests.java @@ -5,7 +5,7 @@ public class AggregatedTests { @Test - public void When_SchemaAggregatedTestExample_ValidTrue() { + public void When_SchemaAggregatedTest_ValidTrue() { var schema = """ %title: "Example Schema For Some Json HTTP Request or Response" %version: "February 11, 2023" @@ -133,7 +133,7 @@ public void When_JsonAggregatedFormatTest_ValidTrue() { } @Test - public void When_SimpleJSchemaAggregatedTest_ValidTrue() { + public void When_BasicJSchemaAggregatedTest_ValidTrue() { var schema = """ %title: "User Profile Response" %version: "1.0.0-basic" @@ -233,7 +233,8 @@ public void When_ExtendedJSchemaAggregatedTest_ValidTrue() { "role": @enum("user", "admin") #string &role, "isActive": #boolean, //user account current status "registeredAt": @after("01-01-2010 00:00:00") #time, - "dataAccess": @checkAccess(&role) #integer, + "dataAccess": @checkDataAccess(&role) #integer, + "ipAddress": @checkIPAddress #string, "profile": { "firstName": @regex("[A-Za-z]{3,50}") #string, "lastName": @regex("[A-Za-z]{3,50}") #string, @@ -271,6 +272,7 @@ public void When_ExtendedJSchemaAggregatedTest_ValidTrue() { "isActive": true, "registeredAt": "06-09-2023 15:10:30", "dataAccess": 10, + "ipAddress": "127.0.0.1", "profile": { "firstName": "John", "lastName": "Doe", @@ -300,7 +302,7 @@ public void When_ExtendedJSchemaAggregatedTest_ValidTrue() { "title": "Working with JSON in Java", "content": "Java provides great support for working with JSON...", "tags": [ - "CSharp", + "Java", "JSON", "tutorial" ] @@ -359,6 +361,7 @@ public void When_ExtendedJSchemaWithScriptAggregatedTest_ValidTrue() { var schema = """ %title: "Extended User Profile Dashboard API Response" %version: "2.0.0-extended" + %import: com.relogiclabs.jschema.test.external.ExternalFunctions %pragma DateDataTypeFormat: "DD-MM-YYYY" %pragma TimeDataTypeFormat: "DD-MM-YYYY hh:mm:ss" @@ -397,6 +400,7 @@ public void When_ExtendedJSchemaWithScriptAggregatedTest_ValidTrue() { "isActive": #boolean, //user account current status "registeredAt": @after("01-01-2010 00:00:00") #time, "dataAccess": @checkAccess(&role) #integer, + "ipAddress": @checkIPAddress #string, "profile": { "firstName": @regex("[A-Za-z]{3,50}") #string, "lastName": @regex("[A-Za-z]{3,50}") #string, @@ -430,9 +434,9 @@ future checkAccess(role) { // Auto-unpacking unwraps single-value '&role' array into its value // 'target' keyword refers to the target JSON value if(role == "user" && target > 5) return fail( - "ERRACCESS", "Data access incompatible with 'user' role", + "EX_ERRACCESS", "Data access incompatible with 'user' role", expected("an access at most 5 for 'user' role"), - actual("found access " + target + " which is greater than 5")); + actual("found access " + target + " that is greater than 5")); return true; // Skipping this line also works } } @@ -440,12 +444,13 @@ future checkAccess(role) { var json = """ { "user": { - "id": 1234, + "id": 1111, "username": "johndoe", "role": "admin", "isActive": true, "registeredAt": "06-09-2023 15:10:30", "dataAccess": 10, + "ipAddress": "127.0.0.1", "profile": { "firstName": "John", "lastName": "Doe", @@ -475,7 +480,7 @@ future checkAccess(role) { "title": "Working with JSON in Java", "content": "Java provides great support for working with JSON...", "tags": [ - "CSharp", + "Java", "JSON", "tutorial" ] diff --git a/src/test/java/com/relogiclabs/jschema/test/positive/ComponentTests.java b/src/test/java/com/relogiclabs/jschema/test/positive/ComponentTests.java index 6fcee53..ad816fb 100644 --- a/src/test/java/com/relogiclabs/jschema/test/positive/ComponentTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/positive/ComponentTests.java @@ -46,8 +46,7 @@ public void When_ComponentExampleInArray_ValidTrue() { public void When_ComponentAlternativeFormInArray_ValidTrue() { var schema1 = """ - %define $cmp: @range*(1, 10) #integer* - %schema: @length(5) #array($cmp) + %schema: @range*(1, 10) @length(5) #integer* #array """; var schema2 = """ @@ -56,7 +55,13 @@ public void When_ComponentAlternativeFormInArray_ValidTrue() { """; var schema3 = """ - @range*(1, 10) @length(5) #integer* #array + %define $cmp: @range*(1, 10) #integer* + %schema: @length(5) #array($cmp) + """; + var schema4 = + """ + %define $cmp: @range*(1, 10) @length(5) #integer* #array + %schema: $cmp """; var json = """ @@ -65,6 +70,7 @@ public void When_ComponentAlternativeFormInArray_ValidTrue() { JsonAssert.isValid(schema1, json); JsonAssert.isValid(schema2, json); JsonAssert.isValid(schema3, json); + JsonAssert.isValid(schema4, json); } @Test diff --git a/src/test/java/com/relogiclabs/jschema/test/positive/ReceiverTests.java b/src/test/java/com/relogiclabs/jschema/test/positive/ReceiverTests.java index a1cb98e..963f7a3 100644 --- a/src/test/java/com/relogiclabs/jschema/test/positive/ReceiverTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/positive/ReceiverTests.java @@ -60,12 +60,12 @@ public void When_ReceiveArrayValuesInObject_ValidTrue() { var schema = """ %import: com.relogiclabs.jschema.test.external.ExternalFunctions - + %define $numbers: @range(1, 10) #integer &relatedValues %schema: { "key1": #integer*($numbers) #array, - "key2": @conditionAll(&relatedValues) #integer + "key2": @conditionMany(&relatedValues) #integer } """; var json = diff --git a/src/test/java/com/relogiclabs/jschema/test/positive/ScriptBasicTests.java b/src/test/java/com/relogiclabs/jschema/test/positive/ScriptBasicTests.java index f1cd698..7c99bbb 100644 --- a/src/test/java/com/relogiclabs/jschema/test/positive/ScriptBasicTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/positive/ScriptBasicTests.java @@ -201,7 +201,7 @@ constraint tryofTest() { var result2 = tryof(target.k1[5].item); if(result2.error) print("Error: " + result2.error); else print("Value: " + result2.value); - if(!result2.error.find("[IDXR01]")) throw("Invalid: " + target); + if(!result2.error.find("[REDIDX01]")) throw("Invalid: " + target); } } """; @@ -228,11 +228,11 @@ constraint tryofTest() { var result = tryof(throwerFunc(-1)); if(result.error) print("Error: " + result.error); else print("Value : " + result.value); - if(!result.error.find("[ERR01]")) throw("Invalid: " + result); + if(!result.error.find("[EX_ERR01]")) throw("Invalid: " + result); } subroutine function throwerFunc(value) { - if(value < 0) throw("ERR01", "Invalid argument value"); + if(value < 0) throw("EX_ERR01", "Invalid argument value"); print("line not execute"); } } diff --git a/src/test/java/com/relogiclabs/jschema/test/positive/ScriptFunctionTests.java b/src/test/java/com/relogiclabs/jschema/test/positive/ScriptFunctionTests.java index 7a949e1..b11abb5 100644 --- a/src/test/java/com/relogiclabs/jschema/test/positive/ScriptFunctionTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/positive/ScriptFunctionTests.java @@ -26,12 +26,12 @@ constraint funcTest(params...) { foreach(var p in params) print("Received param: " + p); return true; } - + constraint funcTest(param1) { if(target != 20 || param1 != 1) return fail("Invalid: " + target); return true; } - + constraint funcTest(param1, param2) { if(target != 30 || param1 != 3 || param2 != 5) return fail("Invalid: " + target); @@ -63,7 +63,7 @@ public void When_SimilarConstraintAndSubroutineNoConflict_ValidTrue() { constraint funcTest() { return funcTest(); } - + // Constraint functions are special functions and are not callable // from script thereby preventing any conflicts with subroutines // target and caller are available from the call stack of schema @@ -98,12 +98,12 @@ constraint funcTest() { if(target != 10) return fail("Invalid: " + target); return true; } - + constraint funcTest(param1) { if(target != 20) return fail("Invalid: " + target); return true; } - + constraint funcTest(param1, param2, param3) { if(target != 30) return fail("Invalid: " + target); return true; @@ -129,7 +129,7 @@ public void When_SubroutineVariadicCallWithVaryingParameters_ValidTrue() { { "funcTest": @funcTest #integer } - + %script: { constraint funcTest() { var r1 = funcVar(); @@ -137,7 +137,7 @@ constraint funcTest() { var r2 = funcVar(1, 2, 3, 4); if(r2 != 4) return fail("Invalid: " + r2); } - + subroutine funcVar(params...) { return params.length(); } @@ -165,43 +165,43 @@ public void When_AlternativeSyntaxForDifferentFunctionTypes_ValidTrue() { "funcTest5": @funcTest5 #integer, "funcTest6": @funcTest6 #integer } - + %script: { constraint funcTest1() { if(target != 10) return fail("Invalid: " + target); return funcTest7(); } - + constraint function funcTest2() { if(target != 20) return fail("Invalid: " + target); return funcTest8(); } - + future funcTest3() { if(target != 30) return fail("Invalid: " + target); return true; } - + future constraint funcTest4() { if(target != 40) return fail("Invalid: " + target); return true; } - + future function funcTest5() { if(target != 50) return fail("Invalid: " + target); return true; } - + future constraint function funcTest6() { if(target != 60) return fail("Invalid: " + target); return true; } - + subroutine funcTest7() { if(target != 10) return fail("Invalid: " + target); return true; } - + subroutine function funcTest8() { if(target != 20) return fail("Invalid: " + target); return true; @@ -231,14 +231,14 @@ public void When_CheckScriptGlobalVariableState_ValidTrue() { "stateTest1": @stateTest1 #integer, "stateTest2": @stateTest2 #integer } - + %script: { var test = 10, test2 = 5; constraint stateTest1() { test = target; test2 = 10; } - + constraint stateTest2() { if(test != target - 10) return fail("Invalid: " + target); if(test2 != 10) return fail("Invalid: " + test2); diff --git a/src/test/java/com/relogiclabs/jschema/test/positive/ScriptGeneralTests.java b/src/test/java/com/relogiclabs/jschema/test/positive/ScriptGeneralTests.java index 1a361bb..ae02927 100644 --- a/src/test/java/com/relogiclabs/jschema/test/positive/ScriptGeneralTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/positive/ScriptGeneralTests.java @@ -16,11 +16,11 @@ public void When_ConditionSatisfiedWithValueV1_ValidTrue() { %script: { future constraint checkAccess(role) { if(role == "user" && target > 5) return fail( - "ERRACCESS01", "Data access incompatible with 'user' role", + "EX_ERRACCESS01", "Data access incompatible with 'user' role", // 'caller' is the default node added automatically expected("an access at most 5 for 'user' role"), // 'target' is the default node added automatically - actual("found access " + target + " which is greater than 5")); + actual("found access " + target + " that is greater than 5")); } } """; @@ -46,11 +46,11 @@ public void When_ConditionSatisfiedWithValueV2_ValidTrue() { %script: { future checkAccess(role) { if(role == "user" && target > 5) return fail( - "ERRACCESS01", "Data access incompatible with 'user' role", + "EX_ERRACCESS01", "Data access incompatible with 'user' role", // Pass any node explicitly to the expected function expected(caller, "an access at most 5 for 'user' role"), // Pass any node explicitly to the actual function - actual(target, "found access " + target + " which is greater than 5")); + actual(target, "found access " + target + " that is greater than 5")); } } """; @@ -76,12 +76,12 @@ public void When_ConditionSatisfiedWithValueV3_ValidTrue() { %script: { constraint checkAccess(role) { if(role == "user" && target > 5) return fail( - "ERRACCESS01", "Data access incompatible with 'user' role", + "EX_ERRACCESS01", "Data access incompatible with 'user' role", // Create an expected object explicitly without any function { node: caller, message: "an access at most 5 for 'user' role" }, // Create an actual object explicitly without any function { node: target, message: "found access " + target - + " which is greater than 5" }); + + " that is greater than 5" }); } } """; @@ -198,9 +198,9 @@ public void When_TargetRangeDependsOnOtherValues_ValidTrue() { %script: { future constraint checkRange(min, max) { if(target < min || target > max) return fail( - "RANGEERR01", "The target value is out of range", - expected("a value in range " + [min, max]), - actual("found " + target + " which is out of range")); + "EX_RANGEERR01", "Target number is out of range", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); } } """; diff --git a/src/test/java/com/relogiclabs/jschema/test/positive/ScriptLiteralTests.java b/src/test/java/com/relogiclabs/jschema/test/positive/ScriptLiteralTests.java index 61010f0..9713b4d 100644 --- a/src/test/java/com/relogiclabs/jschema/test/positive/ScriptLiteralTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/positive/ScriptLiteralTests.java @@ -118,13 +118,13 @@ public void When_CheckUndefinedFromSchema_ValidTrue() { %script: { future constraint checkRange(min, max) { if(min != undefined && target < min) return fail( - "RANGEERR01", "The value is less than minimum", - expected("a value in range " + [min, max]), - actual("found " + target + " which is out of range")); + "EX_RANGEERR01", "Target number is less than minimum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); if(max != undefined && target > max) return fail( - "RANGEERR02", "The value is greater than maximum", - expected("a value in range " + [min, max]), - actual("found " + target + " which is out of range")); + "EX_RANGEERR02", "Target number is greater than maximum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); } } """; @@ -150,13 +150,13 @@ public void When_CheckNullFromSchema_ValidTrue() { %script: { future constraint checkRange(min, max) { if(min != null && target < min) return fail( - "RANGEERR01", "The value is less than minimum", - expected("a value in range " + [min, max]), - actual("found " + target + " which is out of range")); + "EX_RANGEERR01", "Target number is less than minimum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); if(max != null && target > max) return fail( - "RANGEERR02", "The value is greater than maximum", - expected("a value in range " + [min, max]), - actual("found " + target + " which is out of range")); + "EX_RANGEERR02", "Target number is greater than maximum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); } } """; @@ -184,13 +184,13 @@ public void When_CheckNullOrUndefinedFromSchema_ValidTrue() { %script: { future constraint checkRange(min, max) { if(min && target < min) return fail( - "RANGEERR01", "The value is less than minimum", - expected("a value in range " + [min, max]), - actual("found " + target + " which is out of range")); + "EX_RANGEERR01", "Target number is less than minimum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); if(max && target > max) return fail( - "RANGEERR02", "The value is greater than maximum", - expected("a value in range " + [min, max]), - actual("found " + target + " which is out of range")); + "EX_RANGEERR02", "Target number is greater than maximum", + expected("a number in range " + [min, max]), + actual("found " + target + " that is out of range")); } } """; diff --git a/src/test/java/com/relogiclabs/jschema/test/positive/StringTests.java b/src/test/java/com/relogiclabs/jschema/test/positive/StringTests.java index c33409d..966ebaa 100644 --- a/src/test/java/com/relogiclabs/jschema/test/positive/StringTests.java +++ b/src/test/java/com/relogiclabs/jschema/test/positive/StringTests.java @@ -228,6 +228,33 @@ public void When_PhoneWithStringInObject_ValidTrue() { JsonAssert.isValid(schema, json); } + @Test + public void When_IPWithDifferentFunctionsAndVersions_ValidTrue() { + var schema = + """ + { + "ip1v4": @ipv4 #string, + "ip2v6": @ipv6 #string, + "ip3v4": @ipv(4) #string, + "ip4v6": @ipv(6) #string, + "ip5vAny": @ipv(4, 6) #string, + "ip6vAny": @ipv(4, 6) #string + } + """; + var json = + """ + { + "ip1v4": "18.195.85.27", + "ip2v6": "2a00:1450:400b:c01:0:0:0:71", + "ip3v4": "8.8.8.8", + "ip4v6": "2001:4860:4860::8888", + "ip5vAny": "108.177.16.0", + "ip6vAny": "::ffff:8.8.4.4" + } + """; + JsonAssert.isValid(schema, json); + } + @Test public void When_NonEmptyStringInObject_ValidTrue() { var schema = From 8dd016d3232eca733fa92affeac3cde4207bc127 Mon Sep 17 00:00:00 2001 From: Zahid Hossain <60911932+zhossain-info@users.noreply.github.com> Date: Fri, 30 Aug 2024 17:13:51 +0600 Subject: [PATCH 7/7] Update project documentations --- README.md | 9 +- doc/content/articles/components.md | 17 +- doc/content/articles/cscript.md | 28 +- doc/content/articles/directives.md | 16 +- doc/content/articles/functions.md | 55 +- doc/content/articles/quickstart.md | 28 +- doc/content/articles/sourcebuild.md | 28 +- doc/public/api/allclasses-index.html | 216 +- doc/public/api/allpackages-index.html | 8 +- .../com/relogiclabs/jschema/JsonAssert.html | 6 +- .../com/relogiclabs/jschema/JsonSchema.html | 14 +- .../jschema/class-use/JsonAssert.html | 6 +- .../jschema/class-use/JsonSchema.html | 6 +- .../jschema/collection/IndexMap.html | 6 +- .../jschema/collection/Keyable.html | 6 +- .../collection/class-use/IndexMap.html | 6 +- .../jschema/collection/class-use/Keyable.html | 6 +- .../jschema/collection/package-summary.html | 6 +- .../jschema/collection/package-tree.html | 6 +- .../jschema/collection/package-use.html | 6 +- ...ption.html => AliasNotFoundException.html} | 32 +- .../exception/ArgumentTypeException.html | 247 ++ .../exception/ArgumentValueException.html | 247 ++ ...ml => ArrayIndexOutOfBoundsException.html} | 40 +- ...ception.html => BaseRuntimeException.html} | 93 +- .../ClassInstantiationException.html | 14 +- .../DataTypeValidationException.html | 221 ++ .../exception/DateTimeLexerException.html | 14 +- .../exception/DuplicateAliasException.html | 158 + ...n.html => DuplicateFunctionException.html} | 74 +- .../exception/DuplicateImportException.html | 14 +- ....html => DuplicateParameterException.html} | 32 +- .../exception/DuplicatePragmaException.html | 14 +- .../DuplicatePropertyKeyException.html | 14 +- .../exception/DuplicateVariableException.html | 210 + .../FunctionArgumentTypeException.html | 215 ++ .../FunctionArgumentValueException.html | 223 ++ .../exception/FunctionNotFoundException.html | 14 +- ....html => FunctionValidationException.html} | 85 +- .../exception/IndexOutOfBoundsException.html | 164 + .../exception/InvalidArgumentException.html | 263 ++ .../exception/InvalidContextException.html | 235 ++ .../exception/InvalidDataTypeException.html | 14 +- .../exception/InvalidDateTimeException.html | 65 +- .../exception/InvalidFunctionException.html | 24 +- .../exception/InvalidImportException.html | 14 +- .../exception/InvalidLeftValueException.html | 158 + .../InvalidPragmaValueException.html | 14 +- .../InvalidReceiverStateException.html | 14 +- ...n.html => InvalidReturnTypeException.html} | 42 +- .../exception/InvocationRuntimeException.html | 252 ++ .../jschema/exception/JsonLexerException.html | 14 +- .../exception/JsonParserException.html | 14 +- .../MethodArgumentTypeException.html | 233 ++ .../MethodArgumentValueException.html | 215 ++ .../exception/MethodNotFoundException.html | 228 ++ .../exception/MisplacedOptionalException.html | 14 +- .../exception/MultilevelRuntimeException.html | 260 ++ .../exception/NoClassFoundException.html | 14 +- .../exception/PragmaNotFoundException.html | 14 +- .../exception/PropertyNotFoundException.html | 158 + ...ption.html => PropertyOrderException.html} | 56 +- .../exception/ReceiverNotFoundException.html | 14 +- .../exception/SchemaLexerException.html | 14 +- .../exception/SchemaParserException.html | 14 +- .../exception/ScriptArrayInsertException.html | 210 + ...tion.html => ScriptIteratorException.html} | 100 +- .../exception/ScriptOperationException.html | 18 +- .../ScriptThrowInitiatedException.html | 158 + ...l => StringIndexOutOfBoundsException.html} | 40 +- .../exception/SystemOperationException.html | 18 +- .../exception/TargetInvocationException.html | 14 +- .../UpdateNotSupportedException.html | 76 +- ...xception.html => ValidationException.html} | 58 +- ...ion.html => ValueValidationException.html} | 62 +- .../exception/VariableNotFoundException.html | 158 + .../class-use/AliasNotFoundException.html | 62 + ...eption.html => ArgumentTypeException.html} | 22 +- ...ption.html => ArgumentValueException.html} | 28 +- .../ArrayIndexOutOfBoundsException.html | 62 + .../class-use/BaseRuntimeException.html | 249 ++ .../ClassInstantiationException.html | 6 +- .../exception/class-use/CommonException.html | 189 - ....html => DataTypeValidationException.html} | 14 +- .../class-use/DateTimeLexerException.html | 6 +- ...tion.html => DuplicateAliasException.html} | 14 +- .../class-use/DuplicateFunctionException.html | 62 + .../class-use/DuplicateImportException.html | 6 +- .../DuplicateParameterException.html | 62 + .../class-use/DuplicatePragmaException.html | 6 +- .../DuplicatePropertyKeyException.html | 6 +- .../class-use/DuplicateVariableException.html | 62 + .../FunctionArgumentTypeException.html} | 27 +- .../FunctionArgumentValueException.html | 62 + .../class-use/FunctionNotFoundException.html | 6 +- .../FunctionValidationException.html | 62 + .../class-use/IndexOutOfBoundsException.html | 90 + .../class-use/InvalidArgumentException.html | 120 + ...tion.html => InvalidContextException.html} | 14 +- .../class-use/InvalidDataTypeException.html | 6 +- .../class-use/InvalidDateTimeException.html | 6 +- .../class-use/InvalidFunctionException.html | 6 +- .../class-use/InvalidImportException.html | 6 +- ...on.html => InvalidLeftValueException.html} | 14 +- .../InvalidPragmaValueException.html | 6 +- .../InvalidReceiverStateException.html | 6 +- .../class-use/InvalidReturnTypeException.html | 62 + ...n.html => InvocationRuntimeException.html} | 40 +- .../class-use/JsonLexerException.html | 6 +- .../class-use/JsonParserException.html | 6 +- .../MethodArgumentTypeException.html | 87 + .../MethodArgumentValueException.html | 62 + ...tion.html => MethodNotFoundException.html} | 14 +- .../class-use/MisplacedOptionalException.html | 6 +- .../class-use/MultilevelRuntimeException.html | 141 + .../class-use/NoClassFoundException.html | 6 +- .../class-use/PragmaNotFoundException.html | 6 +- .../class-use/PropertyNotFoundException.html | 62 + .../class-use/PropertyOrderException.html | 62 + .../class-use/ReceiverNotFoundException.html | 6 +- .../class-use/SchemaLexerException.html | 6 +- .../class-use/SchemaParserException.html | 6 +- .../class-use/ScriptArrayInsertException.html | 62 + ...tion.html => ScriptIteratorException.html} | 14 +- .../class-use/ScriptOperationException.html | 6 +- .../ScriptThrowInitiatedException.html | 62 + .../StringIndexOutOfBoundsException.html | 62 + .../class-use/SystemOperationException.html | 6 +- .../class-use/TargetInvocationException.html | 6 +- .../UpdateNotSupportedException.html | 6 +- .../class-use/ValidationException.html | 93 + ...ion.html => ValueValidationException.html} | 14 +- .../class-use/VariableNotFoundException.html | 62 + .../jschema/exception/package-summary.html | 102 +- .../jschema/exception/package-tree.html | 78 +- .../jschema/exception/package-use.html | 26 +- .../jschema/function/CoreFunctions1.html | 6 +- .../jschema/function/CoreFunctions2.html | 6 +- .../jschema/function/CoreFunctions3.html | 6 +- .../jschema/function/CoreFunctions4.html | 28 +- .../jschema/function/CoreFunctions5.html | 208 + .../jschema/function/FunctionProvider.html | 6 +- .../jschema/function/FutureFunction.html | 6 +- .../function/class-use/CoreFunctions1.html | 11 +- .../function/class-use/CoreFunctions2.html | 11 +- .../function/class-use/CoreFunctions3.html | 11 +- .../function/class-use/CoreFunctions4.html | 33 +- .../class-use/CoreFunctions5.html} | 14 +- .../function/class-use/FunctionProvider.html | 11 +- .../function/class-use/FutureFunction.html | 8 +- .../jschema/function/package-summary.html | 14 +- .../jschema/function/package-tree.html | 12 +- .../jschema/function/package-use.html | 10 +- .../jschema/message/ActualDetail.html | 8 +- .../jschema/message/ContextDetail.html | 27 +- .../jschema/message/ErrorCode.html | 3368 +++++++++-------- .../jschema/message/ErrorDetail.html | 6 +- .../jschema/message/ExpectedDetail.html | 8 +- .../jschema/message/MessageFormatter.html | 52 +- .../jschema/message/OutlineFormatter.html | 66 +- .../message/class-use/ActualDetail.html | 45 +- .../message/class-use/ContextDetail.html | 6 +- .../jschema/message/class-use/ErrorCode.html | 6 +- .../message/class-use/ErrorDetail.html | 210 +- .../message/class-use/ExpectedDetail.html | 45 +- .../message/class-use/MessageFormatter.html | 6 +- .../message/class-use/OutlineFormatter.html | 6 +- .../jschema/message/package-summary.html | 6 +- .../jschema/message/package-tree.html | 6 +- .../jschema/message/package-use.html | 6 +- .../relogiclabs/jschema/node/Derivable.html | 6 +- .../com/relogiclabs/jschema/node/JAlias.html | 6 +- .../com/relogiclabs/jschema/node/JArray.html | 6 +- .../relogiclabs/jschema/node/JBoolean.html | 6 +- .../com/relogiclabs/jschema/node/JBranch.html | 6 +- .../relogiclabs/jschema/node/JComposite.html | 6 +- .../relogiclabs/jschema/node/JDataType.html | 6 +- .../com/relogiclabs/jschema/node/JDate.html | 6 +- .../relogiclabs/jschema/node/JDateTime.html | 6 +- .../relogiclabs/jschema/node/JDefinition.html | 6 +- .../relogiclabs/jschema/node/JDirective.html | 6 +- .../com/relogiclabs/jschema/node/JDouble.html | 6 +- .../com/relogiclabs/jschema/node/JFloat.html | 6 +- .../relogiclabs/jschema/node/JFunction.html | 6 +- .../com/relogiclabs/jschema/node/JImport.html | 6 +- .../relogiclabs/jschema/node/JInteger.html | 6 +- .../com/relogiclabs/jschema/node/JLeaf.html | 6 +- .../com/relogiclabs/jschema/node/JNode.html | 6 +- .../com/relogiclabs/jschema/node/JNull.html | 6 +- .../com/relogiclabs/jschema/node/JNumber.html | 6 +- .../com/relogiclabs/jschema/node/JObject.html | 6 +- .../com/relogiclabs/jschema/node/JPragma.html | 6 +- .../relogiclabs/jschema/node/JPrimitive.html | 6 +- .../relogiclabs/jschema/node/JProperty.html | 6 +- .../relogiclabs/jschema/node/JReceiver.html | 6 +- .../com/relogiclabs/jschema/node/JRoot.html | 6 +- .../com/relogiclabs/jschema/node/JScript.html | 6 +- .../com/relogiclabs/jschema/node/JString.html | 6 +- .../com/relogiclabs/jschema/node/JTime.html | 6 +- .../com/relogiclabs/jschema/node/JTitle.html | 6 +- .../relogiclabs/jschema/node/JUndefined.html | 6 +- .../relogiclabs/jschema/node/JValidator.html | 6 +- .../relogiclabs/jschema/node/JVersion.html | 6 +- .../relogiclabs/jschema/node/JsonTypable.html | 6 +- .../relogiclabs/jschema/node/JsonType.html | 22 +- .../relogiclabs/jschema/node/NestedMode.html | 6 +- .../relogiclabs/jschema/node/PragmaValue.html | 6 +- .../jschema/node/class-use/Derivable.html | 6 +- .../jschema/node/class-use/JAlias.html | 6 +- .../jschema/node/class-use/JArray.html | 6 +- .../jschema/node/class-use/JBoolean.html | 6 +- .../jschema/node/class-use/JBranch.html | 6 +- .../jschema/node/class-use/JComposite.html | 6 +- .../jschema/node/class-use/JDataType.html | 6 +- .../jschema/node/class-use/JDate.html | 6 +- .../jschema/node/class-use/JDateTime.html | 14 +- .../jschema/node/class-use/JDefinition.html | 6 +- .../jschema/node/class-use/JDirective.html | 6 +- .../jschema/node/class-use/JDouble.html | 6 +- .../jschema/node/class-use/JFloat.html | 6 +- .../jschema/node/class-use/JFunction.html | 6 +- .../jschema/node/class-use/JImport.html | 6 +- .../jschema/node/class-use/JInteger.html | 68 +- .../jschema/node/class-use/JLeaf.html | 6 +- .../jschema/node/class-use/JNode.html | 54 +- .../jschema/node/class-use/JNull.html | 6 +- .../jschema/node/class-use/JNumber.html | 6 +- .../jschema/node/class-use/JObject.html | 6 +- .../jschema/node/class-use/JPragma.html | 6 +- .../jschema/node/class-use/JPrimitive.html | 6 +- .../jschema/node/class-use/JProperty.html | 35 +- .../jschema/node/class-use/JReceiver.html | 6 +- .../jschema/node/class-use/JRoot.html | 6 +- .../jschema/node/class-use/JScript.html | 6 +- .../jschema/node/class-use/JString.html | 84 +- .../jschema/node/class-use/JTime.html | 6 +- .../jschema/node/class-use/JTitle.html | 6 +- .../jschema/node/class-use/JUndefined.html | 6 +- .../jschema/node/class-use/JValidator.html | 6 +- .../jschema/node/class-use/JVersion.html | 6 +- .../jschema/node/class-use/JsonTypable.html | 6 +- .../jschema/node/class-use/JsonType.html | 8 +- .../jschema/node/class-use/NestedMode.html | 6 +- .../jschema/node/class-use/PragmaValue.html | 6 +- .../jschema/node/package-summary.html | 6 +- .../jschema/node/package-tree.html | 6 +- .../relogiclabs/jschema/node/package-use.html | 31 +- .../relogiclabs/jschema/package-summary.html | 8 +- .../com/relogiclabs/jschema/package-tree.html | 6 +- .../com/relogiclabs/jschema/package-use.html | 6 +- .../jschema/time/DateTimeType.html | 6 +- .../jschema/time/JsonDateTime.html | 6 +- .../jschema/time/JsonUtcOffset.html | 6 +- .../jschema/time/class-use/DateTimeType.html | 6 +- .../jschema/time/class-use/JsonDateTime.html | 6 +- .../jschema/time/class-use/JsonUtcOffset.html | 6 +- .../jschema/time/package-summary.html | 6 +- .../jschema/time/package-tree.html | 6 +- .../relogiclabs/jschema/time/package-use.html | 6 +- .../com/relogiclabs/jschema/tree/Context.html | 30 +- .../relogiclabs/jschema/tree/DataTree.html | 6 +- .../jschema/tree/ExceptionRegistry.html | 6 +- .../jschema/tree/FunctionRegistry.html | 18 +- .../relogiclabs/jschema/tree/JsonTree.html | 6 +- .../relogiclabs/jschema/tree/Location.html | 263 -- .../jschema/tree/PragmaRegistry.html | 172 +- .../jschema/tree/ReceiverRegistry.html | 6 +- .../jschema/tree/RuntimeContext.html | 10 +- .../relogiclabs/jschema/tree/SchemaTree.html | 6 +- .../relogiclabs/jschema/tree/TreeType.html | 6 +- .../jschema/tree/class-use/Context.html | 6 +- .../jschema/tree/class-use/DataTree.html | 6 +- .../tree/class-use/ExceptionRegistry.html | 6 +- .../tree/class-use/FunctionRegistry.html | 6 +- .../jschema/tree/class-use/JsonTree.html | 6 +- .../jschema/tree/class-use/Location.html | 122 - .../tree/class-use/PragmaRegistry.html | 6 +- .../tree/class-use/ReceiverRegistry.html | 6 +- .../tree/class-use/RuntimeContext.html | 6 +- .../jschema/tree/class-use/SchemaTree.html | 6 +- .../jschema/tree/class-use/TreeType.html | 6 +- .../jschema/tree/package-summary.html | 24 +- .../jschema/tree/package-tree.html | 11 +- .../relogiclabs/jschema/tree/package-use.html | 18 +- .../com/relogiclabs/jschema/type/EArray.html | 6 +- .../relogiclabs/jschema/type/EBoolean.html | 6 +- .../com/relogiclabs/jschema/type/EDouble.html | 6 +- .../relogiclabs/jschema/type/EInteger.html | 6 +- .../com/relogiclabs/jschema/type/ENull.html | 6 +- .../com/relogiclabs/jschema/type/ENumber.html | 6 +- .../com/relogiclabs/jschema/type/EObject.html | 6 +- .../com/relogiclabs/jschema/type/EString.html | 6 +- .../com/relogiclabs/jschema/type/EType.html | 6 +- .../relogiclabs/jschema/type/EUndefined.html | 6 +- .../com/relogiclabs/jschema/type/EValue.html | 6 +- .../jschema/type/class-use/EArray.html | 6 +- .../jschema/type/class-use/EBoolean.html | 6 +- .../jschema/type/class-use/EDouble.html | 6 +- .../jschema/type/class-use/EInteger.html | 6 +- .../jschema/type/class-use/ENull.html | 6 +- .../jschema/type/class-use/ENumber.html | 6 +- .../jschema/type/class-use/EObject.html | 6 +- .../jschema/type/class-use/EString.html | 6 +- .../jschema/type/class-use/EType.html | 37 +- .../jschema/type/class-use/EUndefined.html | 6 +- .../jschema/type/class-use/EValue.html | 44 +- .../jschema/type/package-summary.html | 6 +- .../jschema/type/package-tree.html | 6 +- .../relogiclabs/jschema/type/package-use.html | 27 +- .../relogiclabs/jschema/util/Reference.html | 219 -- .../jschema/util/package-summary.html | 98 - .../jschema/util/package-tree.html | 76 - .../relogiclabs/jschema/util/package-use.html | 84 - doc/public/api/constant-values.html | 2022 +++++----- doc/public/api/element-list | 1 - doc/public/api/help-doc.html | 6 +- doc/public/api/index-all.html | 1295 ++++--- doc/public/api/index.html | 10 +- doc/public/api/member-search-index.js | 2 +- doc/public/api/overview-summary.html | 6 +- doc/public/api/overview-tree.html | 93 +- doc/public/api/package-search-index.js | 2 +- doc/public/api/serialized-form.html | 424 ++- doc/public/api/type-search-index.js | 2 +- doc/public/articles/components/index.html | 17 +- doc/public/articles/cscript/index.html | 6 +- doc/public/articles/directives/index.html | 10 +- doc/public/articles/functions/index.html | 70 +- doc/public/articles/quickstart/index.html | 28 +- doc/public/articles/sourcebuild/index.html | 28 +- doc/public/images/favicon.ico | Bin 32038 -> 15406 bytes doc/public/images/json-schema.webp | Bin 28778 -> 25504 bytes doc/public/images/logo.svg | 2 +- doc/static/images/favicon.ico | Bin 32038 -> 15406 bytes doc/static/images/json-schema.webp | Bin 28778 -> 25504 bytes doc/static/images/logo.svg | 2 +- pom.xml | 8 +- 337 files changed, 12737 insertions(+), 6197 deletions(-) rename doc/public/api/com/relogiclabs/jschema/exception/{DuplicateDefinitionException.html => AliasNotFoundException.html} (84%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/ArgumentTypeException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/ArgumentValueException.html rename doc/public/api/com/relogiclabs/jschema/exception/{ScriptVariableException.html => ArrayIndexOutOfBoundsException.html} (80%) rename doc/public/api/com/relogiclabs/jschema/exception/{CommonException.html => BaseRuntimeException.html} (68%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/DataTypeValidationException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/DuplicateAliasException.html rename doc/public/api/com/relogiclabs/jschema/exception/{ScriptInvocationException.html => DuplicateFunctionException.html} (74%) rename doc/public/api/com/relogiclabs/jschema/exception/{DefinitionNotFoundException.html => DuplicateParameterException.html} (86%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/DuplicateVariableException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/FunctionArgumentTypeException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/FunctionArgumentValueException.html rename doc/public/api/com/relogiclabs/jschema/exception/{ScriptCommonException.html => FunctionValidationException.html} (65%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/IndexOutOfBoundsException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/InvalidArgumentException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/InvalidContextException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/InvalidLeftValueException.html rename doc/public/api/com/relogiclabs/jschema/exception/{ScriptInitiatedException.html => InvalidReturnTypeException.html} (80%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/InvocationRuntimeException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/MethodArgumentTypeException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/MethodArgumentValueException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/MethodNotFoundException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/MultilevelRuntimeException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/PropertyNotFoundException.html rename doc/public/api/com/relogiclabs/jschema/exception/{ScriptTemplateException.html => PropertyOrderException.html} (77%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/ScriptArrayInsertException.html rename doc/public/api/com/relogiclabs/jschema/exception/{ScriptRuntimeException.html => ScriptIteratorException.html} (65%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/ScriptThrowInitiatedException.html rename doc/public/api/com/relogiclabs/jschema/exception/{ScriptFunctionException.html => StringIndexOutOfBoundsException.html} (80%) rename doc/public/api/com/relogiclabs/jschema/exception/{JsonSchemaException.html => ValidationException.html} (83%) rename doc/public/api/com/relogiclabs/jschema/exception/{ScriptArgumentException.html => ValueValidationException.html} (67%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/VariableNotFoundException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/AliasNotFoundException.html rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{ScriptTemplateException.html => ArgumentTypeException.html} (67%) rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{ScriptRuntimeException.html => ArgumentValueException.html} (63%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/ArrayIndexOutOfBoundsException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/BaseRuntimeException.html delete mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/CommonException.html rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{DefinitionNotFoundException.html => DataTypeValidationException.html} (80%) rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{DuplicateDefinitionException.html => DuplicateAliasException.html} (82%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/DuplicateFunctionException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/DuplicateParameterException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/DuplicateVariableException.html rename doc/public/api/com/relogiclabs/jschema/{util/class-use/Reference.html => exception/class-use/FunctionArgumentTypeException.html} (63%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/FunctionArgumentValueException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/FunctionValidationException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/IndexOutOfBoundsException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/InvalidArgumentException.html rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{ScriptVariableException.html => InvalidContextException.html} (81%) rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{ScriptInvocationException.html => InvalidLeftValueException.html} (80%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/InvalidReturnTypeException.html rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{ScriptCommonException.html => InvocationRuntimeException.html} (54%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/MethodArgumentTypeException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/MethodArgumentValueException.html rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{ScriptArgumentException.html => MethodNotFoundException.html} (81%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/MultilevelRuntimeException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/PropertyNotFoundException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/PropertyOrderException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/ScriptArrayInsertException.html rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{ScriptFunctionException.html => ScriptIteratorException.html} (83%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/ScriptThrowInitiatedException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/StringIndexOutOfBoundsException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/ValidationException.html rename doc/public/api/com/relogiclabs/jschema/exception/class-use/{ScriptInitiatedException.html => ValueValidationException.html} (81%) create mode 100644 doc/public/api/com/relogiclabs/jschema/exception/class-use/VariableNotFoundException.html create mode 100644 doc/public/api/com/relogiclabs/jschema/function/CoreFunctions5.html rename doc/public/api/com/relogiclabs/jschema/{exception/class-use/JsonSchemaException.html => function/class-use/CoreFunctions5.html} (79%) delete mode 100644 doc/public/api/com/relogiclabs/jschema/tree/Location.html delete mode 100644 doc/public/api/com/relogiclabs/jschema/tree/class-use/Location.html delete mode 100644 doc/public/api/com/relogiclabs/jschema/util/Reference.html delete mode 100644 doc/public/api/com/relogiclabs/jschema/util/package-summary.html delete mode 100644 doc/public/api/com/relogiclabs/jschema/util/package-tree.html delete mode 100644 doc/public/api/com/relogiclabs/jschema/util/package-use.html diff --git a/README.md b/README.md index 9694175..c36d08d 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ The next example represents an expanded version of the previous one, which bring ```cpp %title: "Extended User Profile Dashboard API Response" %version: "2.0.0-extended" +%import: com.relogiclabs.jschema.test.external.ExternalFunctions %pragma DateDataTypeFormat: "DD-MM-YYYY" %pragma TimeDataTypeFormat: "DD-MM-YYYY hh:mm:ss" @@ -105,6 +106,7 @@ The next example represents an expanded version of the previous one, which bring "isActive": #boolean, //user account current status "registeredAt": @after("01-01-2010 00:00:00") #time, "dataAccess": @checkAccess(&role) #integer, + "ipAddress": @checkIPAddress #string, "profile": { "firstName": @regex("[A-Za-z]{3,50}") #string, "lastName": @regex("[A-Za-z]{3,50}") #string, @@ -140,7 +142,7 @@ The next example represents an expanded version of the previous one, which bring if(role == "user" && target > 5) return fail( "ERRACCESS01", "Data access incompatible with 'user' role", expected("an access at most 5 for 'user' role"), - actual("found access " + target + " which is greater than 5")); + actual("found access " + target + " that is greater than 5")); } } ``` @@ -148,12 +150,13 @@ The subsequent JSON sample is an illustrative example that successfully validate ```json { "user": { - "id": 1234, + "id": 1111, "username": "johndoe", "role": "admin", "isActive": true, "registeredAt": "06-09-2023 15:10:30", "dataAccess": 10, + "ipAddress": "127.0.0.1", "profile": { "firstName": "John", "lastName": "Doe", @@ -183,7 +186,7 @@ The subsequent JSON sample is an illustrative example that successfully validate "title": "Working with JSON in Java", "content": "Java provides great support for working with JSON...", "tags": [ - "CSharp", + "Java", "JSON", "tutorial" ] diff --git a/doc/content/articles/components.md b/doc/content/articles/components.md index 578f2b2..bfca1cb 100644 --- a/doc/content/articles/components.md +++ b/doc/content/articles/components.md @@ -7,24 +7,25 @@ weight = 10 # Schema Components A schema component, also known as a reusable schema fragment or sub-schema, plays a vital role in improving readability, reducing redundancy, and organizing the structure of a Schema document. In JSON validation, a schema component or fragment defines a validation rule that can be recursively composed of multiple nested validation rules, collectively specifying the expected and valid format of a JSON construct. -These schema components are used as an extension of data type validation, as core data types have limited features to validate the internal structure of a composite JSON value or construct. Therefore, a data type is parameterized with a schema component to validate the internal structure of such composite JSON constructs. +These schema components are used as an extension of data type validation, as core data types have limited features to validate the internal structure of a composite JSON value or construct. Therefore, a data type is parameterized with a schema component to validate the internal structure of such composite JSON constructs. -The name or alias of a schema component always starts with `$` which also refers to the fact that they are named schema components or fragments defined elsewhere in the schema. Schema components can be referenced from any other part of the schema document, effectively reducing redundancy and enhancing reusability and readability. The following example defines a simple schema component named `$component` where the validation rule describes an object structure with two key-value pairs: +The name or alias of a schema component always starts with `$` which unambiguously indicates that they are named schema components or fragments defined elsewhere in the schema. Schema components can be referenced from any parts of the schema document, effectively reducing redundancy and enhancing reusability and readability. The following example defines a simple schema component named `$component` where the validation rule describes an object structure with two key-value pairs: ```js %define $component: { "key1": #integer, "key2": #string } ``` A composite JSON construct is created by combining multiple values as defined by the JSON specification. These nested values can range from simple, like numbers or strings, to more complex, such as arrays or objects. While simple nested values of a composite construct can be validated using only nested data types and functions, handling hierarchical composite constructs with multiple layers of nested structures requires defining schema components. -The second and third rows of the following table illustrate how the component validates the value associated with the data type for which it is used as a parameter. If the associated data type is direct, the component validates the target value itself. Conversely, if the associated data type is nested, the component validates each of the nested values comprising the composite target construct. +The second and third rows of the following table illustrate how the component validates the value associated with the data type for which it is used as a parameter. If the associated data type is direct, the component validates the target value itself. Conversely, if the associated data type is nested, the component validates each of the nested values. | SN | Component Example | Valid Json | |----|----------------------------------------------------------------------------------|--------------------| -| 1 | `@range*(1, 10) @length(5) #integer* #array` | `[1, 3, 5, 8, 10]` | -| 2 | `%define $cmp: @range*(1, 10) #integer*`
`%schema: @length(5) #array($cmp)` | `[1, 3, 5, 8, 10]` | -| 3 | `%define $cmp: @range(1, 10)`
`%schema: @length(5) #integer*($cmp) #array` | `[1, 3, 5, 8, 10]` | +| 1 | `%schema: @range*(1, 10) @length(5) #integer* #array` | `[1, 3, 5, 8, 10]` | +| 2 | `%define $cmp: @range(1, 10)`
`%schema: @length(5) #integer*($cmp) #array` | `[1, 3, 5, 8, 10]` | +| 3 | `%define $cmp: @range*(1, 10) #integer*`
`%schema: @length(5) #array($cmp)` | `[1, 3, 5, 8, 10]` | +| 4 | `%define $cmp: @range*(1, 10) @length(5) #integer* #array`
`%schema: $cmp` | `[1, 3, 5, 8, 10]` | -In the above table, all three rows have identical validation constraints for the input JSON array. This demonstrates that when dealing with simple and primitive nested values in a composite JSON construct, preferring the nested data types and functions is more convenient due to their simplicity and conciseness. However, in cases where the nested values are complex and composite, the schema component syntax becomes more suitable. The following example illustrates how the component syntax can be used to validate elements of a JSON array that are not as straightforward as the previous examples: +In the above table, all four rows have identical validation constraints for the input JSON array. This demonstrates that when dealing with simple and primitive nested values in a composite JSON construct, preferring the nested data types and functions is more convenient due to their simplicity and conciseness. However, in cases where the nested values are complex and composite, the schema component syntax becomes more suitable. The following example illustrates how the component syntax can be used to validate elements of a JSON array that are not as straightforward as the previous examples: ```js %define $article: { "id": @range(1, 100) #integer, @@ -36,7 +37,7 @@ In the above table, all three rows have identical validation constraints for the %schema: @length(1, 10) #object*($article) #array ``` -In practical scenarios, JSON arrays often hold multiple composite JSON constructs as elements, typically sharing a recurring pattern and structure similar to the example above. To facilitate the validation of such elements, using schema components is highly effective. +In practical scenarios, JSON arrays often hold multiple composite JSON constructs as elements, typically sharing a recurring pattern and structure similar to the example above. To facilitate the validation of such elements, using schema components is highly effective. By defining a reusable schema component with a clear and descriptive name, one can improve the overall clarity and readability of the Schema document with recurring structures. This clarity not only makes it easier to understand the structure and intent of the schema but also contributes to keeping your complex schema well-organized, concise, and more manageable. For instance, consider the following example of a JSON document which is valid against the schema example provided above, demonstrating the usage of a schema component: ```js diff --git a/doc/content/articles/cscript.md b/doc/content/articles/cscript.md index c2be8a6..45f3a98 100644 --- a/doc/content/articles/cscript.md +++ b/doc/content/articles/cscript.md @@ -49,20 +49,20 @@ The `#void` type is reserved for internal operations, including initializing una ## Operators & Precedences CScript operators are symbols that are used to perform operations on variables and values. The direct operation of any operator that requires a modifiable l-value including `++`, `--` or `=` will raise an exception for the readonly schema nodes. The following table lists the operators according to their precedences from the highest to the lowest: -| SN | Category | Operator | -|----|-------------------------------|-----------------------------------| -| 1 | Property Access & Parentheses | `.`; `[]`; `()` | -| 2 | Unary Minus & Logical Not | `-`; `!` | -| 3 | Postfix Increment/Decrement | `i++`; `i--` | -| 4 | Prefix Increment/Decrement | `++i`; `--i` | -| 5 | Arithmetic Multiplicative | `*`; `/`; `%` | -| 6 | Arithmetic Additive | `+`; `-` | -| 7 | Sequence Range | `..` | -| 8 | Relational Comparison | `>`; `<`; `>=`; `<=` | -| 9 | Equality Comparison | `==`; `!=` | -| 10 | Logical And (Short-Circuit) | `&&` | -| 11 | Logical Or (Short-Circuit) | `\|\|` | -| 12 | Assignment | `=`; `+=`; `-=`; `*=`; `/=`; `%=` | +| SN | Category | Operator | +|----|--------------------------------|-----------------------------------| +| 1 | Property Access & Parentheses | `.`; `[]`; `()` | +| 2 | Unary Plus/Minus & Logical Not | `+`; `-`; `!` | +| 3 | Postfix Increment/Decrement | `i++`; `i--` | +| 4 | Prefix Increment/Decrement | `++i`; `--i` | +| 5 | Arithmetic Multiplicative | `*`; `/`; `%` | +| 6 | Arithmetic Additive | `+`; `-` | +| 7 | Sequence Range | `..` | +| 8 | Relational Comparison | `>`; `<`; `>=`; `<=` | +| 9 | Equality Comparison | `==`; `!=` | +| 10 | Logical And (Short-Circuit) | `&&` | +| 11 | Logical Or (Short-Circuit) | `\|\|` | +| 12 | Assignment (Augmented) | `=`; `+=`; `-=`; `*=`; `/=`; `%=` | ## Function Types Function types are essential for specifying the executable units that serve as the building-blocks of validation process within CScript. All function types can also accept variable number of arguments, specified by an ellipsis `...` after the last parameter name which is then bound to an array containing the remaining arguments. diff --git a/doc/content/articles/directives.md b/doc/content/articles/directives.md index 47d9ffc..6d90a77 100644 --- a/doc/content/articles/directives.md +++ b/doc/content/articles/directives.md @@ -55,7 +55,7 @@ To customize the default format of the `#time` data type, utilize the `TimeDataT ``` ### Floating Point Tolerance -The `FloatingPointTolerance` pragma directive allows you to define the tolerance level for relative errors in floating-point numbers during calculations and computations carried out by the validation process. By default, this directive is set to `1E-10`, indicating a small tolerance. However, you have the flexibility to adjust this value to any desired number. To specify a custom tolerance value of `1E-07`, you can use the following notation as an example: +The pragma directive `FloatingPointTolerance` allows you to define the tolerance level for relative errors in floating-point numbers during calculations and computations carried out by the validation process. By default, this directive is set to `1E-10`, indicating a small tolerance. However, you have the flexibility to adjust this value to any desired number. To specify a custom tolerance value of `1E-07`, you can use the following notation as an example: ```js %pragma FloatingPointTolerance: 1E-07 ``` @@ -66,6 +66,18 @@ The `IgnoreObjectPropertyOrder` pragma directive provides a means to enforce a s %pragma IgnoreObjectPropertyOrder: false ``` +### Enable Contextual Exception +The `EnableContextualException` pragma directive enables an additional type of exception. These exceptions provide supplementary contextual information about the primary exceptions, showing the errors that occurred during the validation process. The default value of this directive is `false`, meaning no contextual exceptions are generated. +```js +%pragma EnableContextualException: true +``` + +### Outline Maximum Length +The pragma directive `OutlineMaximumLength` specifies the maximum length of tree outlines generated when string representations of large Schema and JSON trees or subtrees are needed. The default maximum length is `200` characters. If the tree or subtree string exceeds this limit, an outline is shown instead of the full string representation. Currently, a basic abbreviation rule is used to create the tree outlines. +```js +%pragma OutlineMaximumLength: 500 +``` + ## Definition / Define Directive This feature in JSchema allows you to define a name for a schema component or fragment, which can be referenced from various parts of your schema. This means that if you encounter similar validation requirements in different sections of your schema, you can conveniently refer to the named schema component instead of duplicating the same validation rules. For more information about the schema component syntax and format, please refer to the documentation [here](/JSchema-Java/articles/components). Here is a simple example of how to use this directive: ```js @@ -102,7 +114,7 @@ The script directive enables the inclusion of CScript code into a JSchema docume %script: { constraint function checkAccess(role) { if(role[0] == "user" && target > 5) return fail( - "ERRACCESS01", "Data access incompatible with 'user' role", + "EX_ERRACCESS01", "Data access incompatible with 'user' role", expected("an access at most 5 for 'user' role"), actual("found access " + target + " which is greater than 5")); } diff --git a/doc/content/articles/functions.md b/doc/content/articles/functions.md index b63cc39..a63e8fe 100644 --- a/doc/content/articles/functions.md +++ b/doc/content/articles/functions.md @@ -85,7 +85,7 @@ If either the parameter values for `minimum` or `maximum` are unspecified or und ```stylus #number target - @range(#number minimum, #number maximum) ``` -Validates that the `target` number satisfies the range requirement specified by the parameters. It checks that the `target` number is greater than or equal to the `minimum` number specified and simultaneously less than or equal to the `maximum` number specified. If not, a validation error will generate. +Validates that the `target` number satisfies the range requirement specified by the parameters. It checks that the `target` number is greater than or equal to the `minimum` number specified and simultaneously less than or equal to the `maximum` number specified. If not, a validation error will generate. If either the parameter values for `minimum` or `maximum` are unspecified or undefined, the `undefined` symbol `!` can be used in place of either of these parameters. The following examples illustrate the various use cases of the `@range` function of the two variations described above, for the target data type number: @@ -121,7 +121,7 @@ Validates that the `target` number is less than or equal to the `maximum` number | `@maximum(10.5)` | `10.50`; `10.49`; `-1000.1` | `10.51`; `11.0`; `1000.1` | | `@maximum(0, true)` | `-0.001`; `-1.01`; `-1000.1` | `0`; `0.01`; `100.1` | -### Enum String and Number +### Enum String and Number ```stylus #string target - @enum(#string... items) #number target - @enum(#number... items) @@ -175,13 +175,28 @@ Validates whether the `target` string is a valid URL or URI with `HTTP` and `HTT ``` Validates whether the `target` string is a valid phone number. It follows the ITU-T E.163 and E.164 telephone number notation to determine the validity of the phone number. In addition to conforming to this standard, it recognizes all widely used national and international phone number formats, ensuring compatibility with a wide range of systems and user requirements. +### IP Address +```stylus +#string target - @ipv4 +#string target - @ipv6 +#string target - @ipv(#integer... versions) +``` +Validates whether the `target` string is a valid IPv4 or IPv6 address for the first and second variants respectively. For the third variant, target must match one of the IP versions specified by the `versions` parameter. It follows RFC 790, RFC 1918, and RFC 2373, as well as recognizing common IP formats widely used. + +| Ues Cases | Valid Values | Invalid Values | +|--------------|----------------------------|---------------------------| +| `@ipv4` | `18.195.85.27` | `13.0.1` | +| `@ipv6` | `2a00:1450:400b:c01::71` | `130d::1310:600c:d01::22` | +| `@ipv(4)` | `8.8.8.8` | `192.0.2.256` | +| `@ipv(4, 6)` | `2001:db8:3:4::192.0.2.33` | `13cd:0:0:13` | + ### Date and Time ```stylus #string target - @date(pattern) ``` Validates that the `target` string matches the date and time pattern specified by the `pattern` parameter. It fully supports the ISO 8601 date and time format. Beyond this standard, it also allows custom date and time formats, ensuring compatibility with various systems and meeting diverse users and businesses requirements. This [document](/JSchema-Java/articles/datetime) provides a comprehensive overview of the date-time custom patterns. -``` +```stylus #string target - @time(pattern) ``` Both the `@date` and `@time` functions support a complete range of date-time patterns, enabling the precise definition of any date and time scenario. Therefore, these functions can be used interchangeably. When the sole consideration is the date or day of the month in a year, employing the `@date` function is the more convenient choice. In contrast, when it becomes necessary to specify a particular time on a date, the `@time` function is the more appropriate option. To learn more about date-time patterns, please refer to [this page](/JSchema-Java/articles/datetime). @@ -203,40 +218,40 @@ If either the parameter values for `start` or `end` are unspecified or undefined ### Date and Time Start ```stylus -#datetime target - @start(#string reference) +#datetime target - @start(#string datetime) ``` -Validates that the `target` date-time starts from or finds after the specified `reference` date-time parameter. If the `target` date-time finds before the `reference` date-time, a validation error is triggered. The `reference` parameter must be the string representation of the `target` data type, which can either be a `#date` or `#time` type. +Validates that the `target` date-time starts from or finds after the specified `datetime` parameter. If the `target` date-time finds before the `datetime` specified, a validation error is triggered. The `datetime` parameter must be the string representation of the `target` data type, which can either be a `#date` or `#time` type. ### Date and Time End ```stylus -#datetime target - @end(#string reference) +#datetime target - @end(#string datetime) ``` -Validates that the `target` date-time finds before or ends at the specified `reference` date-time parameter. If the `target` date-time finds after the `reference` date-time, a validation error is triggered. The `reference` parameter must be the string representation of the `target` data type, which can either be a `#date` or `#time` type. +Validates that the `target` date-time finds before or ends at the specified `datetime` parameter. If the `target` date-time finds after the `datetime` specified, a validation error is triggered. The `datetime` parameter must be the string representation of the `target` data type, which can either be a `#date` or `#time` type. ### Date and Time Before ```stylus -#datetime target - @before(#string reference) +#datetime target - @before(#string datetime) ``` -Validates that the `target` date-time is exclusively before the `reference` date-time. If the `target` date-time finds on or after the `reference` date-time, a validation error is triggered. The `reference` parameter must be the string representation of the `target` data type, which can either be a `#date` or `#time` type. +Validates that the `target` date-time is exclusively before the `datetime` parameter. If the `target` date-time finds on or after the `datetime` specified, a validation error is triggered. The `datetime` parameter must be the string representation of the `target` data type, which can either be a `#date` or `#time` type. ### Date and Time After ```stylus -#datetime target - @after(#string reference) +#datetime target - @after(#string datetime) ``` -Validates that the `target` date-time is exclusively after the `reference` date-time. If the `target` date-time finds on or before the `reference` date-time, a validation error is triggered. The `reference` parameter must be the string representation of the `target` data type, which can either be a `#date` or `#time` type. +Validates that the `target` date-time is exclusively after the `datetime` parameter. If the `target` date-time finds on or before the `datetime` specified, a validation error is triggered. The `datetime` parameter must be the string representation of the `target` data type, which can either be a `#date` or `#time` type. ### Number Positive and Negative ```stylus #number target - @positive #number target - @positive(#number reference) ``` -Validates that the `target` number is positive or positive from (or relative to) the specified `reference`. If the `target` number is zero or negative for the first definition or less than the `reference` for the second definition, it generates a validation error. +Validates that the `target` number is positive or positive from (or relative to) the specified `reference`. If the `target` number is zero or negative for the first variant or less than the `reference` for the second variant, it generates a validation error. ```stylus #number target - @negative #number target - @negative(#number reference) ``` -Validates that the `target` number is negative or negative from (or relative to) the specified `reference`. If the `target` number is zero or positive for the first definition or greater than the `reference` for the second definition, it generates a validation error. +Validates that the `target` number is negative or negative from (or relative to) the specified `reference`. If the `target` number is zero or positive for the first variant or greater than the `reference` for the second variant, it generates a validation error. | Ues Cases | Valid Values | Invalid Values | |------------------|--------------------------|------------------------| @@ -249,20 +264,10 @@ Validates that the `target` number is negative or negative from (or relative to) | `@positive(-1)` | `-1`; `0`; `10000` | `-10`; `-100`; `-1000` | | `@negative(1)` | `1`; `0`; `-10000` | `10`; `100`; `1000` | -### String Not Empty +### Not Empty ```stylus #string target - @nonempty -``` -Validates that the `target` string is not empty. If the `target` string is empty, it generates a validation error. - -### Array Not Empty -```stylus #array target - @nonempty -``` -Validates that the `target` array is not empty. If the `target` array is empty, it generates a validation error. - -### Object Not Empty -```stylus #object target - @nonempty ``` -Validates that the `target` object is not empty. If the `target` object is empty, it generates a validation error. \ No newline at end of file +Validates that the `target` string or array or object is not empty. If the `target` string or array or object is empty, it generates a validation error. \ No newline at end of file diff --git a/doc/content/articles/quickstart.md b/doc/content/articles/quickstart.md index 2dfb7f1..80a7dee 100644 --- a/doc/content/articles/quickstart.md +++ b/doc/content/articles/quickstart.md @@ -87,7 +87,7 @@ public class SampleSchema { ``` ## Create Validation Errors -Let's intentionally introduce a few errors by modifying the previous JSON document and then examine the validation results. To begin, we'll alter the `id` within the `user` object to a string type and observe the outcome. Additionally, we'll modify the `username` by inserting a space into its value, thus creating an invalid `username`. Below is the revised JSON representation, now containing these purposeful errors. +Let's intentionally introduce a few errors by modifying the previous JSON document and examine the validation results. The revised JSON, shown below, contains two purposeful errors. The `id` within the `user` object is now a string type value, and the `username` is now invalid due to a space inserted in its value. ```json { "user": { @@ -119,17 +119,15 @@ if(!jsonSchema.isValid(json)) jsonSchema.writeError(); ... ``` -Here is the error as displayed in the console. More specific errors will be listed first, followed by more general errors. Consequently, the specific errors will precisely pinpoint the issues within the JSON document, while the generic errors will provide contextual information about where the errors occurred. +The schema validation using `JsonSchema` attempts to continue processing JSON data even when encountering validation errors, allowing it to identify and report multiple issues in the data all at once. However, if a critical schema error is detected that could lead to unpredictable behavior, the validator will halt processing to prevent unintended consequences. ```json -Schema (Line: 6:31) Json (Line: 3:14) [DTYP04]: Data type mismatch. Data type #integer is expected but found #string inferred by "not number". -Schema (Line: 6:14) Json (Line: 3:14) [FUNC03]: Function @range(1, 10000) is incompatible with the target data type. Applying to a supported data type such as #number is expected but applied to an unsupported data type #string of "not number". -Schema (Line: 8:20) Json (Line: 4:20) [REGX01]: Regex pattern does not match. String of pattern "[a-z_]{3,30}" is expected but found "john doe" that mismatches with pattern. -Schema (Line: 5:12) Json (Line: 2:12) [VALD01]: Validation failed. A valid value of {"id": @range(1, 10000) #integer, "username": @regex("[a-z_]{3,30}") #string, "role": "user" #string, "isActive": #boolean, "register...ing, "country": @regex("[A-Za-z ]{3,50}") #string} #object #null}} is expected but found {"id": "not number", "username": "john doe", "role": "user", "isActive": true, "registeredAt": "2023-09-06T15:10:30.639Z", "profile":...: "123 Some St", "city": "Some town", "country": "Some Country"}}}. -Schema (Line: 4:0) Json (Line: 1:0) [VALD01]: Validation failed. A valid value of {"user": {"id": @range(1, 10000) #integer, "username": @regex("[a-z_]{3,30}") #string, "role": "user" #string, "isActive": #boolean, ...ng, "country": @regex("[A-Za-z ]{3,50}") #string} #object #null}}} is expected but found {"user": {"id": "not number", "username": "john doe", "role": "user", "isActive": true, "registeredAt": "2023-09-06T15:10:30.639Z", "... "123 Some St", "city": "Some town", "country": "Some Country"}}}}. +DataTypeValidationException: Schema (Line: 6:31) Json (Line: 3:14) [DTYPMS01]: Data type mismatched. Data type #integer is expected but found #string inferred by "not number". +FunctionValidationException: Schema (Line: 6:14) Json (Line: 3:14) [FNTRGT01]: Function @range(1, 10000) is incompatible with target data type. A supported data type such as #number is expected but found unsupported target #string of "not number". +FunctionValidationException: Schema (Line: 8:20) Json (Line: 4:20) [REGXCF01]: Target mismatched with regex pattern. A string following pattern "[a-z_]{3,30}" is expected but mismatched for target "john doe". ``` ## Assertion for Validation -To utilize this library for test automation and API testing, you can use the following alternative code snippet to perform assertions on input JSON against a specified schema. For instance, let's examine how to assert the JSON, which has been intentionally altered to introduce some errors, against the aforementioned schema. The following demonstrates the adjusted code for asserting the JSON with errors: +To utilize this library for test automation and API testing, you can use the following alternative code snippet to perform assertions on input JSON against a specified schema. For instance, let's examine how to assert the JSON, which has been intentionally altered to introduce few errors, against the aforementioned schema. The following demonstrates the adjusted code for asserting the JSON with errors: ```java ... @@ -141,23 +139,23 @@ try { ... ``` -The following presents the printed stack trace for the preceding example. It's important to note that when using `JsonAssert`, it throws an exception upon encountering the first error, thus preventing the continuation of processing the rest of the schema: +In contrast to continuing processing after encountering validation errors, schema assertion using `JsonAssert` throws an exception immediately upon discovering the first error, reducing processing time when a detailed report of all validation issues is not required. The following presents the printed stack trace for the preceding example: ```json -com.relogiclabs.jschema.exception.JsonSchemaException: DTYP04: Data type mismatch +com.relogiclabs.jschema.exception.DataTypeValidationException: DTYPMS01: Data type mismatched Expected (Schema Line: 6:31): data type #integer Actual (Json Line: 3:14): found #string inferred by "not number" - at com.relogiclabs.jschema.node.JValidator.matchDataType(JValidator.java:87) - at com.relogiclabs.jschema.node.JValidator.match(JValidator.java:76) - at com.relogiclabs.jschema.node.JObject.match(JObject.java:56) + at com.relogiclabs.jschema.node.JValidator.matchDataType(JValidator.java:88) + at com.relogiclabs.jschema.node.JValidator.match(JValidator.java:77) + at com.relogiclabs.jschema.node.JObject.match(JObject.java:60) at com.relogiclabs.jschema.node.JValidator.match(JValidator.java:71) - at com.relogiclabs.jschema.node.JObject.match(JObject.java:56) + at com.relogiclabs.jschema.node.JObject.match(JObject.java:60) at com.relogiclabs.jschema.node.JValidator.match(JValidator.java:71) at com.relogiclabs.jschema.node.JRoot.match(JRoot.java:52) at com.relogiclabs.jschema.tree.SchemaTree.match(SchemaTree.java:38) at com.relogiclabs.jschema.JsonAssert.isValid(JsonAssert.java:61) at com.relogiclabs.jschema.JsonAssert.isValid(JsonAssert.java:72) - at com.example.SampleSchema.checkIsValid(SampleSchema.java:68) + at com.example.SampleSchema.checkIsValid(SampleSchema.java:69) at com.example.Main.main(Main.java:5) ``` Similar to the above illustration, there are a lot of test cases with various types of examples available in the source code repository [here](https://github.com/relogiclabs/JSchema-Java/tree/master/src/test/java/com/relogiclabs/jschema/test). \ No newline at end of file diff --git a/doc/content/articles/sourcebuild.md b/doc/content/articles/sourcebuild.md index 31908bc..d7c71e9 100644 --- a/doc/content/articles/sourcebuild.md +++ b/doc/content/articles/sourcebuild.md @@ -98,7 +98,7 @@ public class SampleSchema { ``` ## Create Validation Errors -Let's intentionally introduce a few errors by modifying the previous JSON document and then examine the validation results. To begin, we'll alter the `id` within the `user` object to a string type and observe the outcome. Additionally, we'll modify the `username` by inserting a space into its value, thus creating an invalid `username`. Below is the revised JSON representation, now containing these purposeful errors. +Let's intentionally introduce a few errors by modifying the previous JSON document and examine the validation results. The revised JSON, shown below, contains two purposeful errors. The `id` within the `user` object is now a string type value, and the `username` is now invalid due to a space inserted in its value. ```json { "user": { @@ -130,17 +130,15 @@ if(!jsonSchema.isValid(json)) jsonSchema.writeError(); ... ``` -Here is the error as displayed in the console. More specific errors will be listed first, followed by more general errors. Consequently, the specific errors will precisely pinpoint the issues within the JSON document, while the generic errors will provide contextual information about where the errors occurred. +The schema validation using `JsonSchema` attempts to continue processing JSON data even when encountering validation errors, allowing it to identify and report multiple issues in the data all at once. However, if a critical schema error is detected that could lead to unpredictable behavior, the validator will halt processing to prevent unintended consequences. ```json -Schema (Line: 6:31) Json (Line: 3:14) [DTYP04]: Data type mismatch. Data type #integer is expected but found #string inferred by "not number". -Schema (Line: 6:14) Json (Line: 3:14) [FUNC03]: Function @range(1, 10000) is incompatible with the target data type. Applying to a supported data type such as #number is expected but applied to an unsupported data type #string of "not number". -Schema (Line: 8:20) Json (Line: 4:20) [REGX01]: Regex pattern does not match. String of pattern "[a-z_]{3,30}" is expected but found "john doe" that mismatches with pattern. -Schema (Line: 5:12) Json (Line: 2:12) [VALD01]: Validation failed. A valid value of {"id": @range(1, 10000) #integer, "username": @regex("[a-z_]{3,30}") #string, "role": "user" #string, "isActive": #boolean, "register...ing, "country": @regex("[A-Za-z ]{3,50}") #string} #object #null}} is expected but found {"id": "not number", "username": "john doe", "role": "user", "isActive": true, "registeredAt": "2023-09-06T15:10:30.639Z", "profile":...: "123 Some St", "city": "Some town", "country": "Some Country"}}}. -Schema (Line: 4:0) Json (Line: 1:0) [VALD01]: Validation failed. A valid value of {"user": {"id": @range(1, 10000) #integer, "username": @regex("[a-z_]{3,30}") #string, "role": "user" #string, "isActive": #boolean, ...ng, "country": @regex("[A-Za-z ]{3,50}") #string} #object #null}}} is expected but found {"user": {"id": "not number", "username": "john doe", "role": "user", "isActive": true, "registeredAt": "2023-09-06T15:10:30.639Z", "... "123 Some St", "city": "Some town", "country": "Some Country"}}}}. +DataTypeValidationException: Schema (Line: 6:31) Json (Line: 3:14) [DTYPMS01]: Data type mismatched. Data type #integer is expected but found #string inferred by "not number". +FunctionValidationException: Schema (Line: 6:14) Json (Line: 3:14) [FNTRGT01]: Function @range(1, 10000) is incompatible with target data type. A supported data type such as #number is expected but found unsupported target #string of "not number". +FunctionValidationException: Schema (Line: 8:20) Json (Line: 4:20) [REGXCF01]: Target mismatched with regex pattern. A string following pattern "[a-z_]{3,30}" is expected but mismatched for target "john doe". ``` ## Assertion for Validation -To utilize this library for test automation and API testing, you can use the following alternative code snippet to perform assertions on input JSON against a specified schema. For instance, let's examine how to assert the JSON, which has been intentionally altered to introduce some errors, against the aforementioned schema. The following demonstrates the adjusted code for asserting the JSON with errors: +To utilize this library for test automation and API testing, you can use the following alternative code snippet to perform assertions on input JSON against a specified schema. For instance, let's examine how to assert the JSON, which has been intentionally altered to introduce few errors, against the aforementioned schema. The following demonstrates the adjusted code for asserting the JSON with errors: ```java ... @@ -152,23 +150,23 @@ try { ... ``` -The following presents the printed stack trace for the preceding example. It's important to note that when using `JsonAssert`, it throws an exception upon encountering the first error, thus preventing the continuation of processing the rest of the schema: +In contrast to continuing processing after encountering validation errors, schema assertion using `JsonAssert` throws an exception immediately upon discovering the first error, reducing processing time when a detailed report of all validation issues is not required. The following presents the printed stack trace for the preceding example: ```json -com.relogiclabs.jschema.exception.JsonSchemaException: DTYP04: Data type mismatch +com.relogiclabs.jschema.exception.DataTypeValidationException: DTYPMS01: Data type mismatched Expected (Schema Line: 6:31): data type #integer Actual (Json Line: 3:14): found #string inferred by "not number" - at com.relogiclabs.jschema.node.JValidator.matchDataType(JValidator.java:87) - at com.relogiclabs.jschema.node.JValidator.match(JValidator.java:76) - at com.relogiclabs.jschema.node.JObject.match(JObject.java:56) + at com.relogiclabs.jschema.node.JValidator.matchDataType(JValidator.java:88) + at com.relogiclabs.jschema.node.JValidator.match(JValidator.java:77) + at com.relogiclabs.jschema.node.JObject.match(JObject.java:60) at com.relogiclabs.jschema.node.JValidator.match(JValidator.java:71) - at com.relogiclabs.jschema.node.JObject.match(JObject.java:56) + at com.relogiclabs.jschema.node.JObject.match(JObject.java:60) at com.relogiclabs.jschema.node.JValidator.match(JValidator.java:71) at com.relogiclabs.jschema.node.JRoot.match(JRoot.java:52) at com.relogiclabs.jschema.tree.SchemaTree.match(SchemaTree.java:38) at com.relogiclabs.jschema.JsonAssert.isValid(JsonAssert.java:61) at com.relogiclabs.jschema.JsonAssert.isValid(JsonAssert.java:72) - at com.example.SampleSchema.checkIsValid(SampleSchema.java:68) + at com.example.SampleSchema.checkIsValid(SampleSchema.java:69) at com.example.Main.main(Main.java:5) ``` Similar to the above illustration, there are a lot of test cases with various types of examples available in the source code repository [here](https://github.com/relogiclabs/JSchema-Java/tree/master/src/test/java/com/relogiclabs/jschema/test). \ No newline at end of file diff --git a/doc/public/api/allclasses-index.html b/doc/public/api/allclasses-index.html index b6694b3..9c258a8 100644 --- a/doc/public/api/allclasses-index.html +++ b/doc/public/api/allclasses-index.html @@ -1,11 +1,11 @@ - -All Classes and Interfaces (JSchema 2.1.0 API) + +All Classes and Interfaces (JSchema 2.2.0 API) - + @@ -56,16 +56,24 @@

All Classes and Interfaces

-
+
Class
Description
 
- +
 
- + +
 
+ +
 
+ +
 
+ +
 
+
 
 
@@ -79,23 +87,31 @@

All Classes and Interfaces<
 
 
- -
 
+ +
 
+ +
 
+ +
 
 
 
- -
 
- -
 
- + +
 
+ +
 
+
 
 
- + +
 
+ +
 
+
 
- +
 
 
@@ -127,16 +143,28 @@

All Classes and Interfaces<
 
 
+ +
 
+ +
 
 
 
 
- -
 
-
IndexMap<TK,TV extends Keyable<TK>>
+ +
 
+
 
+
IndexMap<TK,TV extends Keyable<TK>>
+
 
+ +
 
+ +
 
+ +
 
 
@@ -145,79 +173,83 @@

All Classes and Interfaces<
 
 
- +
 
- +
 
- -
 
- + +
 
+ +
 
+ +
 
+
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- +
 
- -
+ +
 
+ +
The class provides assertion functionalities to validate JSON documents against a JSchema or JSON document.
- -
 
- -
 
- + +
 
+
 
- -
+ +
 
+ +
JsonSchema provides JSchema validation functionalities for JSON document.
- -
 
 
@@ -240,29 +272,37 @@

All Classes and Interfaces<
 
 
- -
 
- -
 
- -
 
- -
 
- + +
 
+ +
 
+
 
- -
 
- + +
 
+
 
- -
 
- + +
 
+
 
- + +
 
+ +
 
+
 
- +
 
- + +
 
+ +
 
+ +
 
+ +
 
+
 
 
@@ -272,23 +312,15 @@

All Classes and Interfaces<
 
 
- +
 
- +
 
- +
 
- +
 
- -
 
- -
 
- -
 
- -
 
- +
 
 
@@ -298,6 +330,12 @@

All Classes and Interfaces<
 
 
+ +
 
+ +
 
+ +
 

diff --git a/doc/public/api/allpackages-index.html b/doc/public/api/allpackages-index.html index a2bc2f6..257b812 100644 --- a/doc/public/api/allpackages-index.html +++ b/doc/public/api/allpackages-index.html @@ -1,11 +1,11 @@ - -All Packages (JSchema 2.1.0 API) + +All Packages (JSchema 2.2.0 API) - + @@ -73,8 +73,6 @@

All Packages

 
 
- -
 
Description
 
+
+
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
 
 
- +
 
 
-
CommonException(ErrorDetail detail, +
 
 
- +
 
 
- +
 
 
 
 
- +
 
 
- +
 
 
- +
 
 
- +
 
 
- +
 
 
- +
 
 
- +
 
 
- +
 
 
-
JsonSchemaException(ErrorDetail error, +
 
 
-
JsonSchemaException(ErrorDetail error, +
 
 
- +
FunctionValidationException(ErrorDetail error, + JNode caller, + JNode target)
+
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+
 
 
- +
 
 
- + +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+
 
 
 
 
- +
 
 
-
ScriptCommonException(ErrorDetail detail, +
 
 
- +
 
 
-
ScriptOperationException(ErrorDetail detail, + +
 
+
 
+ +
 
+
 
+
 
 
- +
 
 
-
ScriptRuntimeException(ErrorDetail detail, +
 
 
-
SystemOperationException(ErrorDetail detail, + +
 
+
 
+
ValidationException(ErrorDetail error, + ExpectedDetail expected, + ActualDetail actual, + Throwable cause)
+
 
+
 
+
ValidationException(ErrorDetail error, + JNode schemaNode, + JNode jsonNode)
+
 
+
 
+ +
 
+
 
+
 
+
 
+ +
 
@@ -183,34 +312,35 @@

Uses of Method

Description
-
MessageFormatter.formatForJson(String code, +
MessageFormatter.formatForBoth(String code, String message, - JNode node)
+ JNode schemaNode, + JNode jsonNode)
 
-
MessageFormatter.formatForJson(String code, +
MessageFormatter.formatForJson(String code, String message, - Context context)
+ JNode node)
 
-
MessageFormatter.formatForJson(String code, +
MessageFormatter.formatForJson(String code, String message, - Location location)
+ Context context)
 
-
MessageFormatter.formatForSchema(String code, +
MessageFormatter.formatForJson(String code, String message, - JNode node)
+ Token token)
 
-
MessageFormatter.formatForSchema(String code, +
MessageFormatter.formatForSchema(String code, String message, - Context context)
+ JNode node)
 
-
MessageFormatter.formatForSchema(String code, +
MessageFormatter.formatForSchema(String code, String message, - Location location)
+ Context context)
 
MessageFormatter.formatForSchema(String code, diff --git a/doc/public/api/com/relogiclabs/jschema/message/class-use/ExpectedDetail.html b/doc/public/api/com/relogiclabs/jschema/message/class-use/ExpectedDetail.html index cb7930b..c1e8a7c 100644 --- a/doc/public/api/com/relogiclabs/jschema/message/class-use/ExpectedDetail.html +++ b/doc/public/api/com/relogiclabs/jschema/message/class-use/ExpectedDetail.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.message.ExpectedDetail (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.message.ExpectedDetail (JSchema 2.2.0 API) - + @@ -71,7 +71,7 @@

Uses of Method

Description
-
JsonSchemaException.getExpected()
+
ValidationException.getExpected()
 
Constructors in com.relogiclabs.jschema.exception with parameters of type ExpectedDetail
@@ -80,12 +80,45 @@

Uses of Constructor

Description
 
-
JsonSchemaException(ErrorDetail error, +
 
 
-
JsonSchemaException(ErrorDetail error, + +
 
+
 
+ +
 
+
 
+ +
 
+
 
+ +
 
+
 
+
ValidationException(ErrorDetail error, + ExpectedDetail expected, + ActualDetail actual, + Throwable cause)
+
 
+
 
+ +
 
+
 
+ diff --git a/doc/public/api/com/relogiclabs/jschema/message/class-use/MessageFormatter.html b/doc/public/api/com/relogiclabs/jschema/message/class-use/MessageFormatter.html index 19a33c1..e5e2bae 100644 --- a/doc/public/api/com/relogiclabs/jschema/message/class-use/MessageFormatter.html +++ b/doc/public/api/com/relogiclabs/jschema/message/class-use/MessageFormatter.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.message.MessageFormatter (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.message.MessageFormatter (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/message/class-use/OutlineFormatter.html b/doc/public/api/com/relogiclabs/jschema/message/class-use/OutlineFormatter.html index e24b76d..f9be3d3 100644 --- a/doc/public/api/com/relogiclabs/jschema/message/class-use/OutlineFormatter.html +++ b/doc/public/api/com/relogiclabs/jschema/message/class-use/OutlineFormatter.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.message.OutlineFormatter (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.message.OutlineFormatter (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/message/package-summary.html b/doc/public/api/com/relogiclabs/jschema/message/package-summary.html index dae0f15..2c52163 100644 --- a/doc/public/api/com/relogiclabs/jschema/message/package-summary.html +++ b/doc/public/api/com/relogiclabs/jschema/message/package-summary.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema.message (JSchema 2.1.0 API) + +com.relogiclabs.jschema.message (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/message/package-tree.html b/doc/public/api/com/relogiclabs/jschema/message/package-tree.html index 6357e20..304c38b 100644 --- a/doc/public/api/com/relogiclabs/jschema/message/package-tree.html +++ b/doc/public/api/com/relogiclabs/jschema/message/package-tree.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema.message Class Hierarchy (JSchema 2.1.0 API) + +com.relogiclabs.jschema.message Class Hierarchy (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/message/package-use.html b/doc/public/api/com/relogiclabs/jschema/message/package-use.html index 6c1622f..146f470 100644 --- a/doc/public/api/com/relogiclabs/jschema/message/package-use.html +++ b/doc/public/api/com/relogiclabs/jschema/message/package-use.html @@ -1,11 +1,11 @@ - -Uses of Package com.relogiclabs.jschema.message (JSchema 2.1.0 API) + +Uses of Package com.relogiclabs.jschema.message (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/Derivable.html b/doc/public/api/com/relogiclabs/jschema/node/Derivable.html index 5631cfd..b41759b 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/Derivable.html +++ b/doc/public/api/com/relogiclabs/jschema/node/Derivable.html @@ -1,11 +1,11 @@ - -Derivable (JSchema 2.1.0 API) + +Derivable (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JAlias.html b/doc/public/api/com/relogiclabs/jschema/node/JAlias.html index df5d5ed..d45db37 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JAlias.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JAlias.html @@ -1,11 +1,11 @@ - -JAlias (JSchema 2.1.0 API) + +JAlias (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JArray.html b/doc/public/api/com/relogiclabs/jschema/node/JArray.html index cc79a6e..18e8b5d 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JArray.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JArray.html @@ -1,11 +1,11 @@ - -JArray (JSchema 2.1.0 API) + +JArray (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JBoolean.html b/doc/public/api/com/relogiclabs/jschema/node/JBoolean.html index 5dd598a..4e13b7d 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JBoolean.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JBoolean.html @@ -1,11 +1,11 @@ - -JBoolean (JSchema 2.1.0 API) + +JBoolean (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JBranch.html b/doc/public/api/com/relogiclabs/jschema/node/JBranch.html index 893048e..341c1c9 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JBranch.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JBranch.html @@ -1,11 +1,11 @@ - -JBranch (JSchema 2.1.0 API) + +JBranch (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JComposite.html b/doc/public/api/com/relogiclabs/jschema/node/JComposite.html index dc7ae5b..596469e 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JComposite.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JComposite.html @@ -1,11 +1,11 @@ - -JComposite (JSchema 2.1.0 API) + +JComposite (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JDataType.html b/doc/public/api/com/relogiclabs/jschema/node/JDataType.html index 4b82465..7349bae 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JDataType.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JDataType.html @@ -1,11 +1,11 @@ - -JDataType (JSchema 2.1.0 API) + +JDataType (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JDate.html b/doc/public/api/com/relogiclabs/jschema/node/JDate.html index e89a14c..a436162 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JDate.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JDate.html @@ -1,11 +1,11 @@ - -JDate (JSchema 2.1.0 API) + +JDate (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JDateTime.html b/doc/public/api/com/relogiclabs/jschema/node/JDateTime.html index 558e500..e65cecd 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JDateTime.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JDateTime.html @@ -1,11 +1,11 @@ - -JDateTime (JSchema 2.1.0 API) + +JDateTime (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JDefinition.html b/doc/public/api/com/relogiclabs/jschema/node/JDefinition.html index 72bface..87a2a58 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JDefinition.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JDefinition.html @@ -1,11 +1,11 @@ - -JDefinition (JSchema 2.1.0 API) + +JDefinition (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JDirective.html b/doc/public/api/com/relogiclabs/jschema/node/JDirective.html index 8abf0f4..92e1674 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JDirective.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JDirective.html @@ -1,11 +1,11 @@ - -JDirective (JSchema 2.1.0 API) + +JDirective (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JDouble.html b/doc/public/api/com/relogiclabs/jschema/node/JDouble.html index 2040707..cfcb302 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JDouble.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JDouble.html @@ -1,11 +1,11 @@ - -JDouble (JSchema 2.1.0 API) + +JDouble (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JFloat.html b/doc/public/api/com/relogiclabs/jschema/node/JFloat.html index 353762f..74efea0 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JFloat.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JFloat.html @@ -1,11 +1,11 @@ - -JFloat (JSchema 2.1.0 API) + +JFloat (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JFunction.html b/doc/public/api/com/relogiclabs/jschema/node/JFunction.html index 7f29749..ef21638 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JFunction.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JFunction.html @@ -1,11 +1,11 @@ - -JFunction (JSchema 2.1.0 API) + +JFunction (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JImport.html b/doc/public/api/com/relogiclabs/jschema/node/JImport.html index b8ddb5a..c5b83ca 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JImport.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JImport.html @@ -1,11 +1,11 @@ - -JImport (JSchema 2.1.0 API) + +JImport (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JInteger.html b/doc/public/api/com/relogiclabs/jschema/node/JInteger.html index ca8b21f..8889ccf 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JInteger.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JInteger.html @@ -1,11 +1,11 @@ - -JInteger (JSchema 2.1.0 API) + +JInteger (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JLeaf.html b/doc/public/api/com/relogiclabs/jschema/node/JLeaf.html index 608960a..19ce25e 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JLeaf.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JLeaf.html @@ -1,11 +1,11 @@ - -JLeaf (JSchema 2.1.0 API) + +JLeaf (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JNode.html b/doc/public/api/com/relogiclabs/jschema/node/JNode.html index 65aefd0..f4741d8 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JNode.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JNode.html @@ -1,11 +1,11 @@ - -JNode (JSchema 2.1.0 API) + +JNode (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JNull.html b/doc/public/api/com/relogiclabs/jschema/node/JNull.html index 02b2ee7..a3a99f5 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JNull.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JNull.html @@ -1,11 +1,11 @@ - -JNull (JSchema 2.1.0 API) + +JNull (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JNumber.html b/doc/public/api/com/relogiclabs/jschema/node/JNumber.html index 0bf1bc6..85bd36f 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JNumber.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JNumber.html @@ -1,11 +1,11 @@ - -JNumber (JSchema 2.1.0 API) + +JNumber (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JObject.html b/doc/public/api/com/relogiclabs/jschema/node/JObject.html index a73994b..2b8f9c8 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JObject.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JObject.html @@ -1,11 +1,11 @@ - -JObject (JSchema 2.1.0 API) + +JObject (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JPragma.html b/doc/public/api/com/relogiclabs/jschema/node/JPragma.html index d0d3b84..c982c74 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JPragma.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JPragma.html @@ -1,11 +1,11 @@ - -JPragma (JSchema 2.1.0 API) + +JPragma (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JPrimitive.html b/doc/public/api/com/relogiclabs/jschema/node/JPrimitive.html index fa6609e..0441117 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JPrimitive.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JPrimitive.html @@ -1,11 +1,11 @@ - -JPrimitive (JSchema 2.1.0 API) + +JPrimitive (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JProperty.html b/doc/public/api/com/relogiclabs/jschema/node/JProperty.html index dd4098d..a084afc 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JProperty.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JProperty.html @@ -1,11 +1,11 @@ - -JProperty (JSchema 2.1.0 API) + +JProperty (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JReceiver.html b/doc/public/api/com/relogiclabs/jschema/node/JReceiver.html index a920da5..739ae38 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JReceiver.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JReceiver.html @@ -1,11 +1,11 @@ - -JReceiver (JSchema 2.1.0 API) + +JReceiver (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JRoot.html b/doc/public/api/com/relogiclabs/jschema/node/JRoot.html index 7cb63c8..8869f3e 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JRoot.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JRoot.html @@ -1,11 +1,11 @@ - -JRoot (JSchema 2.1.0 API) + +JRoot (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JScript.html b/doc/public/api/com/relogiclabs/jschema/node/JScript.html index ea49150..6dbb519 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JScript.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JScript.html @@ -1,11 +1,11 @@ - -JScript (JSchema 2.1.0 API) + +JScript (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JString.html b/doc/public/api/com/relogiclabs/jschema/node/JString.html index 849a658..3e73a7f 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JString.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JString.html @@ -1,11 +1,11 @@ - -JString (JSchema 2.1.0 API) + +JString (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JTime.html b/doc/public/api/com/relogiclabs/jschema/node/JTime.html index d1eeb40..0d3f5e7 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JTime.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JTime.html @@ -1,11 +1,11 @@ - -JTime (JSchema 2.1.0 API) + +JTime (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JTitle.html b/doc/public/api/com/relogiclabs/jschema/node/JTitle.html index 577def0..4214ad7 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JTitle.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JTitle.html @@ -1,11 +1,11 @@ - -JTitle (JSchema 2.1.0 API) + +JTitle (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JUndefined.html b/doc/public/api/com/relogiclabs/jschema/node/JUndefined.html index 30ed450..80a51f8 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JUndefined.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JUndefined.html @@ -1,11 +1,11 @@ - -JUndefined (JSchema 2.1.0 API) + +JUndefined (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JValidator.html b/doc/public/api/com/relogiclabs/jschema/node/JValidator.html index 1abcd64..8bdb0ba 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JValidator.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JValidator.html @@ -1,11 +1,11 @@ - -JValidator (JSchema 2.1.0 API) + +JValidator (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JVersion.html b/doc/public/api/com/relogiclabs/jschema/node/JVersion.html index 8efdb8d..4376b39 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JVersion.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JVersion.html @@ -1,11 +1,11 @@ - -JVersion (JSchema 2.1.0 API) + +JVersion (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JsonTypable.html b/doc/public/api/com/relogiclabs/jschema/node/JsonTypable.html index fee6844..e0bb7c7 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JsonTypable.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JsonTypable.html @@ -1,11 +1,11 @@ - -JsonTypable (JSchema 2.1.0 API) + +JsonTypable (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/JsonType.html b/doc/public/api/com/relogiclabs/jschema/node/JsonType.html index 3de80dc..fea1581 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/JsonType.html +++ b/doc/public/api/com/relogiclabs/jschema/node/JsonType.html @@ -1,11 +1,11 @@ - -JsonType (JSchema 2.1.0 API) + +JsonType (JSchema 2.2.0 API) - + @@ -107,14 +107,13 @@

Method Summary

Method
Description
static JsonType
-
from(TerminalNode node)
+
from(Token token)
 
static EType
getType(Class<?> type)
 
-
boolean
-
match(JNode node, - Reference<String> error)
+
MatchResult
+
match(JNode node)
 
@@ -151,16 +150,15 @@

JsonType

Method Details

  • -
    +

    from

    -
    public static JsonType from(TerminalNode node)
    +
    public static JsonType from(Token token)
  • -
    +

    match

    -
    public boolean match(JNode node, - Reference<String> error)
    +
    public MatchResult match(JNode node)
  • diff --git a/doc/public/api/com/relogiclabs/jschema/node/NestedMode.html b/doc/public/api/com/relogiclabs/jschema/node/NestedMode.html index 116337d..c9193e6 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/NestedMode.html +++ b/doc/public/api/com/relogiclabs/jschema/node/NestedMode.html @@ -1,11 +1,11 @@ - -NestedMode (JSchema 2.1.0 API) + +NestedMode (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/PragmaValue.html b/doc/public/api/com/relogiclabs/jschema/node/PragmaValue.html index 6f4df0d..df871e1 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/PragmaValue.html +++ b/doc/public/api/com/relogiclabs/jschema/node/PragmaValue.html @@ -1,11 +1,11 @@ - -PragmaValue (JSchema 2.1.0 API) + +PragmaValue (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/Derivable.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/Derivable.html index b340dc2..6d2ac60 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/Derivable.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/Derivable.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.node.Derivable (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.node.Derivable (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JAlias.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JAlias.html index 1a0d5e8..73fe5ed 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JAlias.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JAlias.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JAlias (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JAlias (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JArray.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JArray.html index f40cba5..99a7441 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JArray.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JArray.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JArray (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JArray (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JBoolean.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JBoolean.html index 8674e98..a36832e 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JBoolean.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JBoolean.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JBoolean (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JBoolean (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JBranch.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JBranch.html index d6a80c4..47eb9c4 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JBranch.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JBranch.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JBranch (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JBranch (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JComposite.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JComposite.html index e899ebe..1bc3236 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JComposite.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JComposite.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JComposite (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JComposite (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDataType.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDataType.html index 7539c13..045f49a 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDataType.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDataType.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JDataType (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JDataType (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDate.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDate.html index d43b21f..98df21d 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDate.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDate.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JDate (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JDate (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDateTime.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDateTime.html index 82b29a8..d65e6a4 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDateTime.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDateTime.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JDateTime (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JDateTime (JSchema 2.2.0 API) - + @@ -74,15 +74,15 @@

    Uses of Description

boolean
CoreFunctions4.after(JDateTime target, - JString reference)
+ JString dateTime)
 
boolean
CoreFunctions4.before(JDateTime target, - JString reference)
+ JString dateTime)
 
boolean
CoreFunctions4.end(JDateTime target, - JString reference)
+ JString dateTime)
 
boolean
CoreFunctions4.range(JDateTime target, @@ -101,7 +101,7 @@

Uses of  

boolean
CoreFunctions4.start(JDateTime target, - JString reference)
+ JString dateTime)
 
diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDefinition.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDefinition.html index df1ea8a..53cc2ed 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDefinition.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDefinition.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JDefinition (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JDefinition (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDirective.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDirective.html index e81f430..a3cc5f7 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDirective.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDirective.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JDirective (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JDirective (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDouble.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDouble.html index f32d29c..e3424e3 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JDouble.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JDouble.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JDouble (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JDouble (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JFloat.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JFloat.html index 3c0bfb9..dfd5e3f 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JFloat.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JFloat.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JFloat (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JFloat (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JFunction.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JFunction.html index 88605ca..7372671 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JFunction.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JFunction.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JFunction (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JFunction (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JImport.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JImport.html index 13e31b3..e5c1c04 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JImport.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JImport.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JImport (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JImport (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JInteger.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JInteger.html index 9999145..d77f387 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JInteger.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JInteger.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JInteger (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JInteger (JSchema 2.2.0 API) - + @@ -71,62 +71,66 @@

Uses of Method
Description
boolean
-
+
CoreFunctions5.ipv(JString target, + JInteger... versions)
 
boolean
-
CoreFunctions1.length(JArray target, - JInteger minimum, - JInteger maximum)
+
CoreFunctions1.length(JArray target, + JInteger length)
 
boolean
-
CoreFunctions1.length(JArray target, +
CoreFunctions1.length(JArray target, JInteger minimum, - JUndefined undefined)
+ JInteger maximum)
 
boolean
-
CoreFunctions1.length(JArray target, - JUndefined undefined, - JInteger maximum)
+
CoreFunctions1.length(JArray target, + JInteger minimum, + JUndefined undefined)
 
boolean
-
CoreFunctions1.length(JObject target, - JInteger length)
+
CoreFunctions1.length(JArray target, + JUndefined undefined, + JInteger maximum)
 
boolean
-
CoreFunctions1.length(JObject target, - JInteger minimum, - JInteger maximum)
+
CoreFunctions1.length(JObject target, + JInteger length)
 
boolean
-
CoreFunctions1.length(JObject target, +
CoreFunctions1.length(JObject target, JInteger minimum, - JUndefined undefined)
+ JInteger maximum)
 
boolean
-
CoreFunctions1.length(JObject target, - JUndefined undefined, - JInteger maximum)
+
CoreFunctions1.length(JObject target, + JInteger minimum, + JUndefined undefined)
 
boolean
-
CoreFunctions1.length(JString target, - JInteger length)
+
CoreFunctions1.length(JObject target, + JUndefined undefined, + JInteger maximum)
 
boolean
-
CoreFunctions1.length(JString target, - JInteger minimum, - JInteger maximum)
+
CoreFunctions1.length(JString target, + JInteger length)
 
boolean
-
CoreFunctions1.length(JString target, +
CoreFunctions1.length(JString target, JInteger minimum, - JUndefined undefined)
+ JInteger maximum)
 
boolean
-
CoreFunctions1.length(JString target, +
CoreFunctions1.length(JString target, + JInteger minimum, + JUndefined undefined)
+
 
+
boolean
+
CoreFunctions1.length(JString target, JUndefined undefined, JInteger maximum)
-
 
+
 
diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JLeaf.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JLeaf.html index fe20319..a9cb1fc 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JLeaf.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JLeaf.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JLeaf (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JLeaf (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JNode.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JNode.html index c0c7a28..7e761cc 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JNode.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JNode.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JNode (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JNode (JSchema 2.2.0 API) - + @@ -55,18 +55,41 @@

Uses
  • +
    +

    Uses of JNode in com.relogiclabs.jschema.exception

    +
    Constructors in com.relogiclabs.jschema.exception with parameters of type JNode
    +
    +
    Modifier
    +
    Constructor
    +
    Description
    +
     
    +
    FunctionValidationException(ErrorDetail error, + JNode caller, + JNode target)
    +
     
    +
     
    +
    ValidationException(ErrorDetail error, + JNode schemaNode, + JNode jsonNode)
    +
     
    +
    +
    +
  • +
  • Uses of JNode in com.relogiclabs.jschema.function

    Methods in com.relogiclabs.jschema.function with parameters of type JNode
    @@ -94,15 +117,21 @@

    Uses of Method
    Description
    -
    MessageFormatter.formatForJson(String code, +
    MessageFormatter.formatForBoth(String code, String message, - JNode node)
    + JNode schemaNode, + JNode jsonNode)
     
    -
    MessageFormatter.formatForSchema(String code, +
    MessageFormatter.formatForJson(String code, String message, JNode node)
     
    + +
    MessageFormatter.formatForSchema(String code, + String message, + JNode node)
    +
     
    Constructors in com.relogiclabs.jschema.message with parameters of type JNode
    @@ -351,9 +380,8 @@

    Uses of boolean

     
    -
    boolean
    -
    JsonType.match(JNode node, - Reference<String> error)
    +
    MatchResult
    +
    JsonType.match(JNode node)
     
    boolean
    JString.match(JNode node)
    diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JNull.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JNull.html index 54aa524..da17324 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JNull.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JNull.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JNull (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JNull (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JNumber.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JNumber.html index 3b9a468..9f150dc 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JNumber.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JNumber.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JNumber (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JNumber (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JObject.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JObject.html index 116aaea..08426ef 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JObject.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JObject.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JObject (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JObject (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JPragma.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JPragma.html index 0af7025..d362761 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JPragma.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JPragma.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JPragma (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JPragma (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JPrimitive.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JPrimitive.html index dd05374..46a44aa 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JPrimitive.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JPrimitive.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JPrimitive (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JPrimitive (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JProperty.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JProperty.html index 17c2d79..4d96998 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JProperty.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JProperty.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JProperty (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JProperty (JSchema 2.2.0 API) - + @@ -55,12 +55,39 @@

    U
    • +
      +

      Uses of JProperty in com.relogiclabs.jschema.exception

      + +
      +
      Modifier and Type
      +
      Method
      +
      Description
      + +
      PropertyOrderException.getFailOn()
      +
       
      +
      +
      Constructors in com.relogiclabs.jschema.exception with parameters of type JProperty
      +
      +
      Modifier
      +
      Constructor
      +
      Description
      +
       
      +
      PropertyOrderException(String code, + String message, + JProperty failOn)
      +
       
      +
      +
      +
    • +
    • Uses of JProperty in com.relogiclabs.jschema.node

      diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JReceiver.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JReceiver.html index e819369..efefe47 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JReceiver.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JReceiver.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JReceiver (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JReceiver (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JRoot.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JRoot.html index 41b2907..59dc136 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JRoot.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JRoot.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JRoot (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JRoot (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JScript.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JScript.html index bf818c7..8a84f53 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JScript.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JScript.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JScript (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JScript (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JString.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JString.html index f9aa0c9..517b5e7 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JString.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JString.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JString (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JString (JSchema 2.2.0 API) - + @@ -78,11 +78,11 @@

      Uses of  
      boolean
      CoreFunctions4.after(JDateTime target, - JString reference)
      + JString dateTime)
       
      boolean
      CoreFunctions4.before(JDateTime target, - JString reference)
      + JString dateTime)
       
      boolean
      CoreFunctions4.date(JString target, @@ -93,71 +93,81 @@

      Uses of  

      boolean
      CoreFunctions4.end(JDateTime target, - JString reference)
      + JString dateTime)
       
      boolean
      -
      CoreFunctions3.keys(JObject target, - JString... items)
      +
      CoreFunctions5.ipv(JString target, + JInteger... versions)
       
      boolean
      -
      CoreFunctions1.length(JString target, - JInteger length)
      +
      CoreFunctions5.ipv4(JString target)
       
      boolean
      -
      CoreFunctions1.length(JString target, - JInteger minimum, - JInteger maximum)
      +
      CoreFunctions5.ipv6(JString target)
       
      boolean
      -
      CoreFunctions1.length(JString target, - JInteger minimum, - JUndefined undefined)
      +
      CoreFunctions3.keys(JObject target, + JString... items)
       
      boolean
      -
      CoreFunctions1.length(JString target, - JUndefined undefined, +
      CoreFunctions1.length(JString target, + JInteger length)
      +
       
      +
      boolean
      +
      CoreFunctions1.length(JString target, + JInteger minimum, JInteger maximum)
      +
       
      +
      boolean
      +
      CoreFunctions1.length(JString target, + JInteger minimum, + JUndefined undefined)
       
      boolean
      -
      CoreFunctions2.nonempty(JString target)
      +
      CoreFunctions1.length(JString target, + JUndefined undefined, + JInteger maximum)
       
      boolean
      -
      CoreFunctions3.phone(JString target)
      +
      CoreFunctions2.nonempty(JString target)
       
      boolean
      -
      CoreFunctions4.range(JDateTime target, - JString start, - JString end)
      +
      CoreFunctions3.phone(JString target)
       
      boolean
      -
      CoreFunctions4.range(JDateTime target, +
      CoreFunctions4.range(JDateTime target, JString start, - JUndefined end)
      + JString end)
       
      boolean
      -
      CoreFunctions4.range(JDateTime target, - JUndefined start, - JString end)
      +
      CoreFunctions4.range(JDateTime target, + JString start, + JUndefined end)
       
      boolean
      -
      CoreFunctions3.regex(JString target, - JString pattern)
      +
      CoreFunctions4.range(JDateTime target, + JUndefined start, + JString end)
       
      boolean
      -
      CoreFunctions4.start(JDateTime target, - JString reference)
      +
      CoreFunctions3.regex(JString target, + JString pattern)
       
      boolean
      -
      CoreFunctions4.time(JString target, - JString pattern)
      +
      CoreFunctions4.start(JDateTime target, + JString dateTime)
       
      boolean
      -
      CoreFunctions3.url(JString target)
      +
      CoreFunctions4.time(JString target, + JString pattern)
       
      boolean
      -
      CoreFunctions3.url(JString target, - JString scheme)
      +
      CoreFunctions3.url(JString target)
       
      +
      boolean
      +
      CoreFunctions3.url(JString target, + JString scheme)
      +
       

    • diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JTime.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JTime.html index d9d32b1..7ee2236 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JTime.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JTime.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JTime (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JTime (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JTitle.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JTitle.html index ebeb3f8..c81e086 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JTitle.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JTitle.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JTitle (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JTitle (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JUndefined.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JUndefined.html index b5ac8d5..e1166ac 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JUndefined.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JUndefined.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JUndefined (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JUndefined (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JValidator.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JValidator.html index 08f0bb7..3a69320 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JValidator.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JValidator.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JValidator (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JValidator (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JVersion.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JVersion.html index 2bf01c9..c0aa23e 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JVersion.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JVersion.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JVersion (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JVersion (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JsonTypable.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JsonTypable.html index 98741d0..5bfa631 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JsonTypable.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JsonTypable.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.node.JsonTypable (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.node.JsonTypable (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/JsonType.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/JsonType.html index 084a7fa..9165796 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/JsonType.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/JsonType.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.node.JsonType (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.node.JsonType (JSchema 2.2.0 API) - + @@ -69,7 +69,7 @@

      Uses of Method
      Description
      -
      JsonType.from(TerminalNode node)
      +
      JsonType.from(Token token)
       
      JDataType.getJsonType()
      diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/NestedMode.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/NestedMode.html index cf5b30c..6abbd7c 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/NestedMode.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/NestedMode.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.node.NestedMode (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.node.NestedMode (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/class-use/PragmaValue.html b/doc/public/api/com/relogiclabs/jschema/node/class-use/PragmaValue.html index 3ca0a60..a87adcb 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/class-use/PragmaValue.html +++ b/doc/public/api/com/relogiclabs/jschema/node/class-use/PragmaValue.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.node.PragmaValue (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.node.PragmaValue (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/package-summary.html b/doc/public/api/com/relogiclabs/jschema/node/package-summary.html index 019da3a..4a8b780 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/package-summary.html +++ b/doc/public/api/com/relogiclabs/jschema/node/package-summary.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema.node (JSchema 2.1.0 API) + +com.relogiclabs.jschema.node (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/package-tree.html b/doc/public/api/com/relogiclabs/jschema/node/package-tree.html index 737ceaf..e98c9cb 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/package-tree.html +++ b/doc/public/api/com/relogiclabs/jschema/node/package-tree.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema.node Class Hierarchy (JSchema 2.1.0 API) + +com.relogiclabs.jschema.node Class Hierarchy (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/node/package-use.html b/doc/public/api/com/relogiclabs/jschema/node/package-use.html index 849fe48..a5c6569 100644 --- a/doc/public/api/com/relogiclabs/jschema/node/package-use.html +++ b/doc/public/api/com/relogiclabs/jschema/node/package-use.html @@ -1,11 +1,11 @@ - -Uses of Package com.relogiclabs.jschema.node (JSchema 2.1.0 API) + +Uses of Package com.relogiclabs.jschema.node (JSchema 2.2.0 API) - + @@ -55,20 +55,35 @@

      Uses of P
      • +
        + +
        +
        Class
        +
        Description
        + +
         
        + +
         
        +
        +
        +
      • +
      • diff --git a/doc/public/api/com/relogiclabs/jschema/package-summary.html b/doc/public/api/com/relogiclabs/jschema/package-summary.html index 4a0342f..ad23204 100644 --- a/doc/public/api/com/relogiclabs/jschema/package-summary.html +++ b/doc/public/api/com/relogiclabs/jschema/package-summary.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema (JSchema 2.1.0 API) + +com.relogiclabs.jschema (JSchema 2.2.0 API) - + @@ -85,8 +85,6 @@

        Package com.relogiclab
         
         
        - -
         

      • diff --git a/doc/public/api/com/relogiclabs/jschema/package-tree.html b/doc/public/api/com/relogiclabs/jschema/package-tree.html index 50e38a8..cc39a72 100644 --- a/doc/public/api/com/relogiclabs/jschema/package-tree.html +++ b/doc/public/api/com/relogiclabs/jschema/package-tree.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema Class Hierarchy (JSchema 2.1.0 API) + +com.relogiclabs.jschema Class Hierarchy (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/package-use.html b/doc/public/api/com/relogiclabs/jschema/package-use.html index 960e20f..d655fff 100644 --- a/doc/public/api/com/relogiclabs/jschema/package-use.html +++ b/doc/public/api/com/relogiclabs/jschema/package-use.html @@ -1,11 +1,11 @@ - -Uses of Package com.relogiclabs.jschema (JSchema 2.1.0 API) + +Uses of Package com.relogiclabs.jschema (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/time/DateTimeType.html b/doc/public/api/com/relogiclabs/jschema/time/DateTimeType.html index 6e34a56..5445685 100644 --- a/doc/public/api/com/relogiclabs/jschema/time/DateTimeType.html +++ b/doc/public/api/com/relogiclabs/jschema/time/DateTimeType.html @@ -1,11 +1,11 @@ - -DateTimeType (JSchema 2.1.0 API) + +DateTimeType (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/time/JsonDateTime.html b/doc/public/api/com/relogiclabs/jschema/time/JsonDateTime.html index 008f074..ab3c258 100644 --- a/doc/public/api/com/relogiclabs/jschema/time/JsonDateTime.html +++ b/doc/public/api/com/relogiclabs/jschema/time/JsonDateTime.html @@ -1,11 +1,11 @@ - -JsonDateTime (JSchema 2.1.0 API) + +JsonDateTime (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/time/JsonUtcOffset.html b/doc/public/api/com/relogiclabs/jschema/time/JsonUtcOffset.html index ac0fa33..2ff09a6 100644 --- a/doc/public/api/com/relogiclabs/jschema/time/JsonUtcOffset.html +++ b/doc/public/api/com/relogiclabs/jschema/time/JsonUtcOffset.html @@ -1,11 +1,11 @@ - -JsonUtcOffset (JSchema 2.1.0 API) + +JsonUtcOffset (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/time/class-use/DateTimeType.html b/doc/public/api/com/relogiclabs/jschema/time/class-use/DateTimeType.html index 01066e2..b0bf458 100644 --- a/doc/public/api/com/relogiclabs/jschema/time/class-use/DateTimeType.html +++ b/doc/public/api/com/relogiclabs/jschema/time/class-use/DateTimeType.html @@ -1,11 +1,11 @@ - -Uses of Enum Class com.relogiclabs.jschema.time.DateTimeType (JSchema 2.1.0 API) + +Uses of Enum Class com.relogiclabs.jschema.time.DateTimeType (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/time/class-use/JsonDateTime.html b/doc/public/api/com/relogiclabs/jschema/time/class-use/JsonDateTime.html index 3b63643..f1853fd 100644 --- a/doc/public/api/com/relogiclabs/jschema/time/class-use/JsonDateTime.html +++ b/doc/public/api/com/relogiclabs/jschema/time/class-use/JsonDateTime.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.time.JsonDateTime (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.time.JsonDateTime (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/time/class-use/JsonUtcOffset.html b/doc/public/api/com/relogiclabs/jschema/time/class-use/JsonUtcOffset.html index 2a346af..b124ec0 100644 --- a/doc/public/api/com/relogiclabs/jschema/time/class-use/JsonUtcOffset.html +++ b/doc/public/api/com/relogiclabs/jschema/time/class-use/JsonUtcOffset.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.time.JsonUtcOffset (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.time.JsonUtcOffset (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/time/package-summary.html b/doc/public/api/com/relogiclabs/jschema/time/package-summary.html index 306e598..829851b 100644 --- a/doc/public/api/com/relogiclabs/jschema/time/package-summary.html +++ b/doc/public/api/com/relogiclabs/jschema/time/package-summary.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema.time (JSchema 2.1.0 API) + +com.relogiclabs.jschema.time (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/time/package-tree.html b/doc/public/api/com/relogiclabs/jschema/time/package-tree.html index 9cdd2ac..e1e3b6d 100644 --- a/doc/public/api/com/relogiclabs/jschema/time/package-tree.html +++ b/doc/public/api/com/relogiclabs/jschema/time/package-tree.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema.time Class Hierarchy (JSchema 2.1.0 API) + +com.relogiclabs.jschema.time Class Hierarchy (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/time/package-use.html b/doc/public/api/com/relogiclabs/jschema/time/package-use.html index 4a5fb3b..0c8b799 100644 --- a/doc/public/api/com/relogiclabs/jschema/time/package-use.html +++ b/doc/public/api/com/relogiclabs/jschema/time/package-use.html @@ -1,11 +1,11 @@ - -Uses of Package com.relogiclabs.jschema.time (JSchema 2.1.0 API) + +Uses of Package com.relogiclabs.jschema.time (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/Context.html b/doc/public/api/com/relogiclabs/jschema/tree/Context.html index 7eb9670..6000b09 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/Context.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/Context.html @@ -1,11 +1,11 @@ - -Context (JSchema 2.1.0 API) + +Context (JSchema 2.2.0 API) - + @@ -107,14 +107,14 @@

        Method Summary

        Modifier and Type
        Method
        Description
        - - +
        ParserRuleContext
        +
         
        -
        ParserRuleContext
        - + +
         
        - - +
        Token
        +
         
        @@ -149,6 +149,12 @@

        Context

        Method Details

        • +
          +

          getToken

          +
          public Token getToken()
          +
          +
        • +
        • getParser

          public ParserRuleContext getParser()
          @@ -160,12 +166,6 @@

          getRuntime

          public RuntimeContext getRuntime()
        • -
        • -
          -

          getLocation

          -
          public Location getLocation()
          -
          -
      diff --git a/doc/public/api/com/relogiclabs/jschema/tree/DataTree.html b/doc/public/api/com/relogiclabs/jschema/tree/DataTree.html index d3dde33..7be9766 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/DataTree.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/DataTree.html @@ -1,11 +1,11 @@ - -DataTree (JSchema 2.1.0 API) + +DataTree (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/ExceptionRegistry.html b/doc/public/api/com/relogiclabs/jschema/tree/ExceptionRegistry.html index 296967a..32b5932 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/ExceptionRegistry.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/ExceptionRegistry.html @@ -1,11 +1,11 @@ - -ExceptionRegistry (JSchema 2.1.0 API) + +ExceptionRegistry (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/FunctionRegistry.html b/doc/public/api/com/relogiclabs/jschema/tree/FunctionRegistry.html index a1d8ed8..9d5463f 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/FunctionRegistry.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/FunctionRegistry.html @@ -1,11 +1,11 @@ - -FunctionRegistry (JSchema 2.1.0 API) + +FunctionRegistry (JSchema 2.2.0 API) - + @@ -165,18 +165,18 @@

      addClass

  • +
    +

    addFunction

    +
    public void addFunction(ScriptFunction function)
    +
    +
  • +
  • invokeFunction

    public boolean invokeFunction(JFunction caller, JNode target)
  • -
  • -
    -

    addFunction

    -
    public void addFunction(ScriptFunction function)
    -
    -
diff --git a/doc/public/api/com/relogiclabs/jschema/tree/JsonTree.html b/doc/public/api/com/relogiclabs/jschema/tree/JsonTree.html index ff244e0..e3387c5 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/JsonTree.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/JsonTree.html @@ -1,11 +1,11 @@ - -JsonTree (JSchema 2.1.0 API) + +JsonTree (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/Location.html b/doc/public/api/com/relogiclabs/jschema/tree/Location.html deleted file mode 100644 index 34d8260..0000000 --- a/doc/public/api/com/relogiclabs/jschema/tree/Location.html +++ /dev/null @@ -1,263 +0,0 @@ - - - - -Location (JSchema 2.1.0 API) - - - - - - - - - - - - - - - -
- -
-
- -
- -

Record Class Location

-
-
Object -
Record -
Location
-
-
-
-
-
public record Location(int line, int column) -extends Record
-
-
-
    - -
  • -
    -

    Constructor Summary

    -
    Constructors
    -
    -
    Constructor
    -
    Description
    -
    Location(int line, - int column)
    -
    -
    Creates an instance of a Location record class.
    -
    -
    -
    -
  • - -
  • -
    -

    Method Summary

    -
    -
    -
    -
    -
    Modifier and Type
    -
    Method
    -
    Description
    -
    int
    - -
    -
    Returns the value of the column record component.
    -
    -
    final boolean
    - -
    -
    Indicates whether some other object is "equal to" this one.
    -
    -
    static Location
    -
    from(Token token)
    -
     
    -
    final int
    - -
    -
    Returns a hash code value for this object.
    -
    -
    int
    - -
    -
    Returns the value of the line record component.
    -
    - - -
    -
    Returns a string representation of this record class.
    -
    -
    -
    -
    -
    -

    Methods inherited from class Object

    -getClass, notify, notifyAll, wait, wait, wait
    -
    -
  • -
-
-
-
    - -
  • -
    -

    Constructor Details

    -
      -
    • -
      -

      Location

      -
      public Location(int line, - int column)
      -
      Creates an instance of a Location record class.
      -
      -
      Parameters:
      -
      line - the value for the line record component
      -
      column - the value for the column record component
      -
      -
      -
    • -
    -
    -
  • - -
  • -
    -

    Method Details

    -
      -
    • -
      -

      from

      -
      public static Location from(Token token)
      -
      -
    • -
    • -
      -

      toString

      -
      public String toString()
      -
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      -
      -
      Specified by:
      -
      toString in class Record
      -
      Returns:
      -
      a string representation of this object
      -
      -
      -
    • -
    • -
      -

      hashCode

      -
      public final int hashCode()
      -
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      -
      -
      Specified by:
      -
      hashCode in class Record
      -
      Returns:
      -
      a hash code value for this object
      -
      -
      -
    • -
    • -
      -

      equals

      -
      public final boolean equals(Object o)
      -
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with '=='.
      -
      -
      Specified by:
      -
      equals in class Record
      -
      Parameters:
      -
      o - the object with which to compare
      -
      Returns:
      -
      true if this object is the same as the o argument; false otherwise.
      -
      -
      -
    • -
    • -
      -

      line

      -
      public int line()
      -
      Returns the value of the line record component.
      -
      -
      Returns:
      -
      the value of the line record component
      -
      -
      -
    • -
    • -
      -

      column

      -
      public int column()
      -
      Returns the value of the column record component.
      -
      -
      Returns:
      -
      the value of the column record component
      -
      -
      -
    • -
    -
    -
  • -
-
- -
- -
-
- - diff --git a/doc/public/api/com/relogiclabs/jschema/tree/PragmaRegistry.html b/doc/public/api/com/relogiclabs/jschema/tree/PragmaRegistry.html index e1d13f3..ca37af8 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/PragmaRegistry.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/PragmaRegistry.html @@ -1,11 +1,11 @@ - -PragmaRegistry (JSchema 2.1.0 API) + +PragmaRegistry (JSchema 2.2.0 API) - + @@ -46,13 +46,13 @@ @@ -87,6 +87,39 @@

Class PragmaRegistry

    + +
  • +
    +

    Field Summary

    +
    Fields
    +
    +
    Modifier and Type
    +
    Field
    +
    Description
    +
    static final String
    + +
     
    +
    static final String
    + +
     
    +
    static final String
    + +
     
    +
    static final String
    + +
     
    +
    static final String
    + +
     
    +
    static final String
    + +
     
    +
    static final String
    + +
     
    +
    +
    +
  • @@ -139,18 +172,21 @@

    Method Summary

     
    boolean
    - +
     
    boolean
    - +
     
    - - +
    boolean
    +
     
    -
    <T> void
    -
    setPragmaValue(String name, - T value)
    + +
     
    +
    <T> void
    +
    setPragmaValue(String name, + T value)
    +
     
    @@ -166,6 +202,112 @@

    Methods inherited from interface&

      + +
    • +
      +

      Field Details

      +
        +
      • +
        +

        NAME_IGNORE_UNDEFINED_PROPERTIES

        +
        public static final String NAME_IGNORE_UNDEFINED_PROPERTIES
        +
        +
        See Also:
        +
        + +
        +
        +
        +
      • +
      • +
        +

        NAME_FLOATING_POINT_TOLERANCE

        +
        public static final String NAME_FLOATING_POINT_TOLERANCE
        +
        +
        See Also:
        +
        + +
        +
        +
        +
      • +
      • +
        +

        NAME_IGNORE_OBJECT_PROPERTY_ORDER

        +
        public static final String NAME_IGNORE_OBJECT_PROPERTY_ORDER
        +
        +
        See Also:
        +
        + +
        +
        +
        +
      • +
      • +
        +

        NAME_DATE_DATA_TYPE_FORMAT

        +
        public static final String NAME_DATE_DATA_TYPE_FORMAT
        +
        +
        See Also:
        +
        + +
        +
        +
        +
      • +
      • +
        +

        NAME_TIME_DATA_TYPE_FORMAT

        +
        public static final String NAME_TIME_DATA_TYPE_FORMAT
        +
        +
        See Also:
        +
        + +
        +
        +
        +
      • +
      • +
        +

        NAME_ENABLE_CONTEXTUAL_EXCEPTION

        +
        public static final String NAME_ENABLE_CONTEXTUAL_EXCEPTION
        +
        +
        See Also:
        +
        + +
        +
        +
        +
      • +
      • +
        +

        NAME_OUTLINE_MAXIMUM_LENGTH

        +
        public static final String NAME_OUTLINE_MAXIMUM_LENGTH
        +
        +
        See Also:
        +
        + +
        +
        +
        +
      • +
      +
      +
    • @@ -257,6 +399,12 @@

      getTimeDataTypeFormat

    • +
      +

      isEnableContextualException

      +
      public boolean isEnableContextualException()
      +
      +
    • +
    • getDateTypeParser

      public DateTimeParser getDateTypeParser()
      diff --git a/doc/public/api/com/relogiclabs/jschema/tree/ReceiverRegistry.html b/doc/public/api/com/relogiclabs/jschema/tree/ReceiverRegistry.html index e1515bf..d974ab1 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/ReceiverRegistry.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/ReceiverRegistry.html @@ -1,11 +1,11 @@ - -ReceiverRegistry (JSchema 2.1.0 API) + +ReceiverRegistry (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/RuntimeContext.html b/doc/public/api/com/relogiclabs/jschema/tree/RuntimeContext.html index d6eb9b3..858d188 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/RuntimeContext.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/RuntimeContext.html @@ -1,11 +1,11 @@ - -RuntimeContext (JSchema 2.1.0 API) + +RuntimeContext (JSchema 2.2.0 API) - + @@ -129,7 +129,7 @@

      Method Summary

       
      - +
       
      @@ -240,7 +240,7 @@

      getExceptions

    • getFutures

      -
      public Map<String,FutureFunction> getFutures()
      +
      public List<FutureFunction> getFutures()
    • diff --git a/doc/public/api/com/relogiclabs/jschema/tree/SchemaTree.html b/doc/public/api/com/relogiclabs/jschema/tree/SchemaTree.html index 1fbba05..47c99ef 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/SchemaTree.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/SchemaTree.html @@ -1,11 +1,11 @@ - -SchemaTree (JSchema 2.1.0 API) + +SchemaTree (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/TreeType.html b/doc/public/api/com/relogiclabs/jschema/tree/TreeType.html index dc6e8ed..0cfc476 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/TreeType.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/TreeType.html @@ -1,11 +1,11 @@ - -TreeType (JSchema 2.1.0 API) + +TreeType (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/Context.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/Context.html index b35d4bc..545fa4c 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/Context.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/Context.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.tree.Context (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.tree.Context (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/DataTree.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/DataTree.html index 6fe6d5a..eb68a15 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/DataTree.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/DataTree.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.tree.DataTree (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.tree.DataTree (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/ExceptionRegistry.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/ExceptionRegistry.html index 690a078..df12e08 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/ExceptionRegistry.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/ExceptionRegistry.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.tree.ExceptionRegistry (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.tree.ExceptionRegistry (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/FunctionRegistry.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/FunctionRegistry.html index a8c91e6..067a17e 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/FunctionRegistry.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/FunctionRegistry.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.tree.FunctionRegistry (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.tree.FunctionRegistry (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/JsonTree.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/JsonTree.html index 5fba845..baad132 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/JsonTree.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/JsonTree.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.tree.JsonTree (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.tree.JsonTree (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/Location.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/Location.html deleted file mode 100644 index a2eb238..0000000 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/Location.html +++ /dev/null @@ -1,122 +0,0 @@ - - - - -Uses of Record Class com.relogiclabs.jschema.tree.Location (JSchema 2.1.0 API) - - - - - - - - - - - - - - - -
      - -
      -
      -
      -

      Uses of Record Class
      com.relogiclabs.jschema.tree.Location

      -
      -
      Packages that use Location
      - -
      - -
      -
      - -
      -
      - - diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/PragmaRegistry.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/PragmaRegistry.html index b380b87..5c0d4bd 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/PragmaRegistry.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/PragmaRegistry.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.tree.PragmaRegistry (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.tree.PragmaRegistry (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/ReceiverRegistry.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/ReceiverRegistry.html index 9dfbe4e..a7807f7 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/ReceiverRegistry.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/ReceiverRegistry.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.tree.ReceiverRegistry (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.tree.ReceiverRegistry (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/RuntimeContext.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/RuntimeContext.html index 83c9ee2..e0a0dc3 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/RuntimeContext.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/RuntimeContext.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.tree.RuntimeContext (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.tree.RuntimeContext (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/SchemaTree.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/SchemaTree.html index d3fe2bf..a46a1e4 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/SchemaTree.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/SchemaTree.html @@ -1,11 +1,11 @@ - -Uses of Class com.relogiclabs.jschema.tree.SchemaTree (JSchema 2.1.0 API) + +Uses of Class com.relogiclabs.jschema.tree.SchemaTree (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/class-use/TreeType.html b/doc/public/api/com/relogiclabs/jschema/tree/class-use/TreeType.html index 95ee693..ef8f933 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/class-use/TreeType.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/class-use/TreeType.html @@ -1,11 +1,11 @@ - -Uses of Enum Class com.relogiclabs.jschema.tree.TreeType (JSchema 2.1.0 API) + +Uses of Enum Class com.relogiclabs.jschema.tree.TreeType (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/tree/package-summary.html b/doc/public/api/com/relogiclabs/jschema/tree/package-summary.html index 167b05d..00f6033 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/package-summary.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/package-summary.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema.tree (JSchema 2.1.0 API) + +com.relogiclabs.jschema.tree (JSchema 2.2.0 API) - + @@ -80,7 +80,7 @@

      Package com.relog

    • -
      +
      Class
      @@ -95,18 +95,16 @@

      Package com.relog
       
       
      - -
       
      - -
       
      - +
       
      - +
       
      - +
       
      - -
       
      + +
       
      + +
       

      diff --git a/doc/public/api/com/relogiclabs/jschema/tree/package-tree.html b/doc/public/api/com/relogiclabs/jschema/tree/package-tree.html index 37a3a93..c82fd5d 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/package-tree.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/package-tree.html @@ -1,11 +1,11 @@ - -com.relogiclabs.jschema.tree Class Hierarchy (JSchema 2.1.0 API) + +com.relogiclabs.jschema.tree Class Hierarchy (JSchema 2.2.0 API) - + @@ -66,11 +66,6 @@

      Class Hierarchy

    • JsonTree (implements DataTree)
    • PragmaRegistry (implements Iterable<T>)
    • ReceiverRegistry (implements Iterable<T>)
    • -
    • Record - -
    • RuntimeContext
    • SchemaTree (implements DataTree)
    diff --git a/doc/public/api/com/relogiclabs/jschema/tree/package-use.html b/doc/public/api/com/relogiclabs/jschema/tree/package-use.html index 996aeaf..554a809 100644 --- a/doc/public/api/com/relogiclabs/jschema/tree/package-use.html +++ b/doc/public/api/com/relogiclabs/jschema/tree/package-use.html @@ -1,11 +1,11 @@ - -Uses of Package com.relogiclabs.jschema.tree (JSchema 2.1.0 API) + +Uses of Package com.relogiclabs.jschema.tree (JSchema 2.2.0 API) - + @@ -106,8 +106,6 @@

    Uses of P
    Description
     
    - -
     

  • @@ -138,16 +136,14 @@

    Uses of P
     
     
    - +
     
    - +
     
    - +
     
    - +
     
    - -
     

diff --git a/doc/public/api/com/relogiclabs/jschema/type/EArray.html b/doc/public/api/com/relogiclabs/jschema/type/EArray.html index d672faa..653dd6f 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/EArray.html +++ b/doc/public/api/com/relogiclabs/jschema/type/EArray.html @@ -1,11 +1,11 @@ - -EArray (JSchema 2.1.0 API) + +EArray (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/EBoolean.html b/doc/public/api/com/relogiclabs/jschema/type/EBoolean.html index 7ad04cf..55c1f5f 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/EBoolean.html +++ b/doc/public/api/com/relogiclabs/jschema/type/EBoolean.html @@ -1,11 +1,11 @@ - -EBoolean (JSchema 2.1.0 API) + +EBoolean (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/EDouble.html b/doc/public/api/com/relogiclabs/jschema/type/EDouble.html index b6e7990..09cfa80 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/EDouble.html +++ b/doc/public/api/com/relogiclabs/jschema/type/EDouble.html @@ -1,11 +1,11 @@ - -EDouble (JSchema 2.1.0 API) + +EDouble (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/EInteger.html b/doc/public/api/com/relogiclabs/jschema/type/EInteger.html index 472d4db..2e8abf0 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/EInteger.html +++ b/doc/public/api/com/relogiclabs/jschema/type/EInteger.html @@ -1,11 +1,11 @@ - -EInteger (JSchema 2.1.0 API) + +EInteger (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/ENull.html b/doc/public/api/com/relogiclabs/jschema/type/ENull.html index b7b6c00..4026ecb 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/ENull.html +++ b/doc/public/api/com/relogiclabs/jschema/type/ENull.html @@ -1,11 +1,11 @@ - -ENull (JSchema 2.1.0 API) + +ENull (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/ENumber.html b/doc/public/api/com/relogiclabs/jschema/type/ENumber.html index 5275740..387a255 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/ENumber.html +++ b/doc/public/api/com/relogiclabs/jschema/type/ENumber.html @@ -1,11 +1,11 @@ - -ENumber (JSchema 2.1.0 API) + +ENumber (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/EObject.html b/doc/public/api/com/relogiclabs/jschema/type/EObject.html index 7909818..168c388 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/EObject.html +++ b/doc/public/api/com/relogiclabs/jschema/type/EObject.html @@ -1,11 +1,11 @@ - -EObject (JSchema 2.1.0 API) + +EObject (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/EString.html b/doc/public/api/com/relogiclabs/jschema/type/EString.html index 40350bb..6115d29 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/EString.html +++ b/doc/public/api/com/relogiclabs/jschema/type/EString.html @@ -1,11 +1,11 @@ - -EString (JSchema 2.1.0 API) + +EString (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/EType.html b/doc/public/api/com/relogiclabs/jschema/type/EType.html index 85e43f6..fa24a15 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/EType.html +++ b/doc/public/api/com/relogiclabs/jschema/type/EType.html @@ -1,11 +1,11 @@ - -EType (JSchema 2.1.0 API) + +EType (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/EUndefined.html b/doc/public/api/com/relogiclabs/jschema/type/EUndefined.html index 871963e..ac66571 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/EUndefined.html +++ b/doc/public/api/com/relogiclabs/jschema/type/EUndefined.html @@ -1,11 +1,11 @@ - -EUndefined (JSchema 2.1.0 API) + +EUndefined (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/EValue.html b/doc/public/api/com/relogiclabs/jschema/type/EValue.html index b0836f6..dee57ce 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/EValue.html +++ b/doc/public/api/com/relogiclabs/jschema/type/EValue.html @@ -1,11 +1,11 @@ - -EValue (JSchema 2.1.0 API) + +EValue (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/class-use/EArray.html b/doc/public/api/com/relogiclabs/jschema/type/class-use/EArray.html index 7ca41e4..34663bb 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/class-use/EArray.html +++ b/doc/public/api/com/relogiclabs/jschema/type/class-use/EArray.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.type.EArray (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.type.EArray (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/class-use/EBoolean.html b/doc/public/api/com/relogiclabs/jschema/type/class-use/EBoolean.html index 223531b..06dc8d0 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/class-use/EBoolean.html +++ b/doc/public/api/com/relogiclabs/jschema/type/class-use/EBoolean.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.type.EBoolean (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.type.EBoolean (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/class-use/EDouble.html b/doc/public/api/com/relogiclabs/jschema/type/class-use/EDouble.html index cc17d24..3511fc8 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/class-use/EDouble.html +++ b/doc/public/api/com/relogiclabs/jschema/type/class-use/EDouble.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.type.EDouble (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.type.EDouble (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/class-use/EInteger.html b/doc/public/api/com/relogiclabs/jschema/type/class-use/EInteger.html index 1eca74a..d91f398 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/class-use/EInteger.html +++ b/doc/public/api/com/relogiclabs/jschema/type/class-use/EInteger.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.type.EInteger (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.type.EInteger (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/class-use/ENull.html b/doc/public/api/com/relogiclabs/jschema/type/class-use/ENull.html index 526604c..2f1d285 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/class-use/ENull.html +++ b/doc/public/api/com/relogiclabs/jschema/type/class-use/ENull.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.type.ENull (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.type.ENull (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/class-use/ENumber.html b/doc/public/api/com/relogiclabs/jschema/type/class-use/ENumber.html index c43df3b..f3d4224 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/class-use/ENumber.html +++ b/doc/public/api/com/relogiclabs/jschema/type/class-use/ENumber.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.type.ENumber (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.type.ENumber (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/class-use/EObject.html b/doc/public/api/com/relogiclabs/jschema/type/class-use/EObject.html index 06a07c8..a0a0d39 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/class-use/EObject.html +++ b/doc/public/api/com/relogiclabs/jschema/type/class-use/EObject.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.type.EObject (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.type.EObject (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/class-use/EString.html b/doc/public/api/com/relogiclabs/jschema/type/class-use/EString.html index bbf7fb8..3541148 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/class-use/EString.html +++ b/doc/public/api/com/relogiclabs/jschema/type/class-use/EString.html @@ -1,11 +1,11 @@ - -Uses of Interface com.relogiclabs.jschema.type.EString (JSchema 2.1.0 API) + +Uses of Interface com.relogiclabs.jschema.type.EString (JSchema 2.2.0 API) - + diff --git a/doc/public/api/com/relogiclabs/jschema/type/class-use/EType.html b/doc/public/api/com/relogiclabs/jschema/type/class-use/EType.html index 0dd23df..06d76a5 100644 --- a/doc/public/api/com/relogiclabs/jschema/type/class-use/EType.html +++ b/doc/public/api/com/relogiclabs/jschema/type/class-use/EType.html @@ -1,11 +1,11 @@ - -Uses of Enum Class com.relogiclabs.jschema.type.EType (JSchema 2.1.0 API) + +Uses of Enum Class com.relogiclabs.jschema.type.EType (JSchema 2.2.0 API) - + @@ -55,16 +55,41 @@