diff --git a/content/io/cloudslang/base/json/validate_json.sl b/content/io/cloudslang/base/json/validate_json.sl index e44e200821..15f9d14fb7 100644 --- a/content/io/cloudslang/base/json/validate_json.sl +++ b/content/io/cloudslang/base/json/validate_json.sl @@ -1,28 +1,26 @@ -# (c) Copyright 2017 EntIT Software LLC, a Micro Focus company, L.P. -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License v2.0 which accompany this distribution. -# -# The Apache License is available at -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ######################################################################################################################## #!! -#! @description: Checks if a JSON is valid. +#! @description: Checks if a JSON is valid and, optionally, if it is valid against a schema. #! -#! @input json_input: JSON to validate +#! @input json_object: JSON to validate. +#! @input json_schema: JSON schema to validate against. Can also be a JSON schema, an URL or a file path. +#! Optional +#! @input proxy_host: The proxy host for getting the Get request. +#! Optional +#! @input proxy_port: The proxy port for getting the Get request. +#! Optional +#! @input proxy_username: The username for connecting via proxy. +#! Optional +#! @input proxy_password: The password for connecting via proxy. +#! Optional #! -#! @output return_result: Message of validity or exception -#! @output return_code: "0" if is a valid json, "-1" otherwise -#! @output error_message: Error message if there was an error when executing, empty otherwise +#! @output return_result: JSON was valid or not (optionally, against a JSON schema). +#! Same as exception if an exception was thrown during execution. +#! @output return_code: "0" if validation was successful, "-1" otherwise. +#! @output exception: Exception message if an exception was thrown during execution, empty otherwise. #! -#! @result SUCCESS: JSON is valid (return_code == '0') -#! @result FAILURE: Otherwise +#! @result SUCCESS: JSON is valid (return_code == '0'). +#! @result FAILURE: An error has occurred while trying to validate the given JSON object. #!!# ######################################################################################################################## @@ -32,31 +30,54 @@ operation: name: validate_json inputs: - - json_input + - json_object + - jsonObject: + default: ${get('json_object', '')} + required: false + private: true + - json_schema: + required: false + - jsonSchema: + default: ${get('json_schema', '')} + required: false + private: true + - proxy_host: + required: false + - proxyHost: + default: ${get('proxy_host', '')} + required: false + private: true + - proxy_port: + required: false + - proxyPort: + default: ${get('proxy_port', '')} + required: false + private: true + - proxy_username: + required: false + - proxyUsername: + default: ${get('proxy_username', '')} + required: false + private: true + - proxy_password: + required: false + sensitive: true + - proxyPassword: + default: ${get('proxy_password', '')} + required: false + private: true + sensitive: true - python_action: - script: | - try: - import json,re - for c in json_input: - if c in ['\'', '\"']: - quote = c - break - if quote == '\'': - json_input = str(re.sub(r"(?-": "v2!#%@&*`?|>{[-"}' description: Tests that validate_json.sl ends with SUCCESS when there are many levels of nesting testFlowPath: io.cloudslang.base.json.validate_json + testSuites: [validate] outputs: - return_result: Valid JSON - return_code: "0" @@ -134,10 +141,11 @@ testValidateSpecialCharactersJSON: testValidateJsonDataSuccess: inputs: - - json_input: + - json_object: value: '[15, "string", 1.54e-1]' description: Tests that validate_json.sl ends with SUCCESS when JSON contains standalone numbers or strings testFlowPath: io.cloudslang.base.json.validate_json + testSuites: [validate] outputs: - return_result: Valid JSON - return_code: "0" @@ -145,10 +153,11 @@ testValidateJsonDataSuccess: testValidateJsonSingleQuote: inputs: - - json_input: + - json_object: value: "[{'firstName':'Darth', 'lastName':'Vader'}, {'firstName':'Luke', 'lastName':'Skywalker'}]" description: Tests that validate_json.sl ends with SUCCESS for a single quotes JSON testFlowPath: io.cloudslang.base.json.validate_json + testSuites: [validate] outputs: - return_result: "Valid JSON" - return_code: "0" @@ -156,7 +165,7 @@ testValidateJsonSingleQuote: testValidateJsonSingleQuoteSuccess: inputs: - - json_input: + - json_object: value: "{ 'server' : { 'name' : 'server_3', @@ -191,6 +200,7 @@ testValidateJsonSingleQuoteSuccess: }" description: Tests that validate_json.sl ends with SUCCESS for a complex single quotes JSON testFlowPath: io.cloudslang.base.json.validate_json + testSuites: [validate] outputs: - return_result: "Valid JSON" - return_code: "0" @@ -198,7 +208,7 @@ testValidateJsonSingleQuoteSuccess: testValidateJsonSingleQuoteFailure: inputs: - - json_input: + - json_object: value: "{ 'server' : { 'name' : 'server_3', @@ -233,6 +243,99 @@ testValidateJsonSingleQuoteFailure: }" description: Tests that validate_json.sl ends with FAILURE for a invalid complex single quotes JSON testFlowPath: io.cloudslang.base.json.validate_json + testSuites: [validate] + outputs: + - return_code: "-1" + result: FAILURE + +testEmptySchemaSuccess: + inputs: + - json_object: + value: '{"first_name": "George", "last_name": "Daniel"}' + - json_schema: + value: '' + description: Tests that validate_json.sl ends with SUCCESS for an empty JSON schema + testFlowPath: io.cloudslang.base.json.validate_json + testSuites: [validate] + outputs: + - return_code: "0" + result: SUCCESS + +testValidateJsonAgainstSchemaSuccess: + inputs: + - json_object: + value: '{ + "first_name": "George", + "last_name": "Washington", + "birthday": "22-02-1732", + "address": { + "street_address": "3200 Mount Vernon Memorial Highway", + "city": "Mount Vernon", + "state": "Virginia", + "country": "United States" + } + }' + - json_schema: + value: '{ + "type": "object", + "properties": { + "first_name": { "type": "string" }, + "last_name": { "type": "string" }, + "birthday": { "type": "string", "format": "string" }, + "address": { + "type": "object", + "properties": { + "street_address": { "type": "string" }, + "city": { "type": "string" }, + "state": { "type": "string" }, + "country": { "type" : "string" } + } + } + } + }' + description: Tests that validate_json.sl ends with SUCCESS for a JSON string that validates against a JSON schema + testFlowPath: io.cloudslang.base.json.validate_json + testSuites: [validate] + outputs: + - return_code: "0" + result: SUCCESS + +testValidateJsonAgainstSchemaFailure: + inputs: + - json_object: + value: '{ + "first_name": "George", + "last_name": "Washington", + "birthday": "22-02-1732", + "address": { + "street_address": "3200 Mount Vernon Memorial Highway", + "city": "Mount Vernon", + "state": "Virginia", + "country": "United States" + } + }' + - json_schema: + value: '{ + "type": "object", + "properties": { + "first_name": { "type": "number" }, + "last_name": { "type": "string" }, + "birthday": { "type": "string", "format": "string" }, + "address": { + "type": "object", + "properties": { + "street_address": { "type": "string" }, + "city": { "type": "string" }, + "state": { "type": "string" }, + "country": { "type" : "string" } + } + } + } + }' + description: Tests that validate_json.sl ends with FAILURE for a JSON string that does not validate against a JSON schema + testFlowPath: io.cloudslang.base.json.validate_json + testSuites: [validate] outputs: - return_code: "-1" - result: FAILURE \ No newline at end of file + result: FAILURE +