src/main/java/application/rest/v1/actions/ResolutionContext.java
// This method validates the value of a user supplied input against a JavaScript snippet.
This method validates the value of a user supplied input against a JavaScript snippet.
This method validates the value of a user supplied input against a JavaScript snippet .
private void validateField(String name, JsonObject field, String value, String snippet) throws ValidationException {
        if (Logger.isDebugEnabled()) {
            Logger.log(className, "validateField", Logger.LogType.DEBUG, "For name="+name + ", value=" + value + ", snippet=" + snippet + ", field="+field.toString());
        }
        final JsonObject input = new JsonObject();
        input.addProperty(VALUE_PROPERTY_NAME, value);
        final String inputAsString = input.toString();
        final String outputAsString = invokeSnippet(snippet, Collections.singletonList(inputAsString));
        if (outputAsString == null) {
            throw new ValidationException("Validation of the field failed due to an internal error.", name);
        }
        try {
            final JsonParser parser = new JsonParser();
            final JsonElement element = parser.parse(outputAsString);
            if (element.isJsonObject()) {
                final JsonObject output = element.getAsJsonObject();
                if (Boolean.parseBoolean(getPrimitivePropertyValue(output, VALID_PROPERTY_NAME))) {
                    return;
                }
                final String message = getPrimitivePropertyValue(output, MESSAGE_PROPERTY_NAME);
                if (message != null) {
                    if (Logger.isErrorEnabled()) {
                        Logger.log(className, "validateField", Logger.LogType.ERROR, "The field is invalid for " + name + ". Reason: " + message);
                    }
                    throw new ValidationException("The field is invalid. Reason: " + message, name);
                }
            }
            if (Logger.isErrorEnabled()) {
                Logger.log(className, "validateField", Logger.LogType.ERROR, "The validation result for the field " + name + " could not be processed because it was in the wrong format.");
            }
            throw new ValidationException("The validation result for the field could not be processed because it was in the wrong format.", name);
        }
        catch (JsonSyntaxException e) {
            if (Logger.isErrorEnabled()) {
                Logger.log(className, "validateField", Logger.LogType.ERROR, "Caught JsonSyntaxException " + e.toString());
            }
            throw new ValidationException("The validation result for the field could not be processed. Reason: " + e.getMessage(), name);
        }
    }
private void validateField ( String name , JsonObject field , String value , String snippet ) throws ValidationException { if ( Logger . isDebugEnabled ( ) ) { Logger . log ( className , "validateField" , Logger . LogType . DEBUG , "For name=" + name + ", value=" + value + ", snippet=" + snippet + ", field=" + field . toString ( ) ) ; } final JsonObject input = new JsonObject ( ) ; input . addProperty ( VALUE_PROPERTY_NAME , value ) ; final String inputAsString = input . toString ( ) ; final String outputAsString = invokeSnippet ( snippet , Collections . singletonList ( inputAsString ) ) ; if ( outputAsString == null ) { throw new ValidationException ( "Validation of the field failed due to an internal error." , name ) ; } try { final JsonParser parser = new JsonParser ( ) ; final JsonElement element = parser . parse ( outputAsString ) ; if ( element . isJsonObject ( ) ) { final JsonObject output = element . getAsJsonObject ( ) ; if ( Boolean . parseBoolean ( getPrimitivePropertyValue ( output , VALID_PROPERTY_NAME ) ) ) { return ; } final String message = getPrimitivePropertyValue ( output , MESSAGE_PROPERTY_NAME ) ; if ( message != null ) { if ( Logger . isErrorEnabled ( ) ) { Logger . log ( className , "validateField" , Logger . LogType . ERROR , "The field is invalid for " + name + ". Reason: " + message ) ; } throw new ValidationException ( "The field is invalid. Reason: " + message , name ) ; } } if ( Logger . isErrorEnabled ( ) ) { Logger . log ( className , "validateField" , Logger . LogType . ERROR , "The validation result for the field " + name + " could not be processed because it was in the wrong format." ) ; } throw new ValidationException ( "The validation result for the field could not be processed because it was in the wrong format." , name ) ; } catch ( JsonSyntaxException e ) { if ( Logger . isErrorEnabled ( ) ) { Logger . log ( className , "validateField" , Logger . LogType . ERROR , "Caught JsonSyntaxException " + e . toString ( ) ) ; } throw new ValidationException ( "The validation result for the field could not be processed. Reason: " + e . getMessage ( ) , name ) ; } }
This method validates the value of a user supplied input against a JavaScript snippet.
This method validates the value of a user supplied input against a JavaScript snippet .
// snippet input: json containing input field value in form: { "value": "<field-value>" } // Invoke the validator. // REVISIT: Message translation required. // snippet output: { "valid": true | false, "message": "<error-message>"} // If the field is valid return normally. // REVISIT: Message translation required. // REVISIT: Message translation required. // REVISIT: Message translation required.