VCF Automation Blog

from Stefan Schnell

JSON Schema Validator


A JSON validator checks if the content of a JSON matches the rules defined in the JSON schema. In Aria Automation this kind of validator can be implemented very easily via a PowerShell action. The CmdLet Test-Json is used for this.

<#
@module de.stschnell

@version 0.1.0

@runtime powershell:7.4

@memoryLimit 256000000

@timeout 360

@param {string} in_jsonContent 
@param {string} in_jsonSchema 

@outputType Properties

@name jsonSchemaValidator
#>

function Handler($context, $inputs) {

  $result = Test-Json -Json $inputs.in_jsonContent `
    -Schema $inputs.in_jsonSchema

  $output = @{status = 'done'; result = $result}

  return $output

}

The following example is from the PowerShell Test-Json cmdlet description, but in JavaScript.

/**
 * @module de.stschnell
 *
 * @version 0.1.0
 *
 * @outputType boolean
 *
 * @name jsonSchemaValidatorCheck
 */

var jsonContent = '{"name": "Ashley", "age": 25}'

var jsonSchema = '{ \
  "definitions": {}, \
  "$schema": "http://json-schema.org/draft-07/schema#", \
  "$id": "http://example.com/root.json", \
  "type": "object", \
  "title": "The Root Schema", \
  "required": [ \
    "name", \
    "age" \
  ], \
  "properties": { \
    "name": { \
      "$id": "#/properties/name", \
      "type": "string", \
      "title": "The Name Schema", \
      "default": "", \
      "examples": [ \
        "Ashley" \
      ], \
      "pattern": "^(.*)$" \
    }, \
    "age": { \
      "$id": "#/properties/age", \
      "type": "integer", \
      "title": "The Age Schema", \
      "default": 0, \
      "examples": [ \
        25 \
      ] \
    } \
  } \
}'

var result = System.getModule("de.stschnell").jsonSchemaValidator(
  jsonContent,
  jsonSchema
)

return result.result