Skip to main content
Version: Next

JSON Validate

JSON Validate ( vJson ; vSchema ) -> Function result

ParameterTypeDescription
vJsonObject🡒JSON object to validate
vSchemaObject🡒JSON schema used to validate JSON objects
Function resultObject🡐Validation status and errors (if any)

Description

The JSON Validate command checks the compliance of the vJson JSON contents with the rules defined in the vSchema JSON schema. If the JSON is invalid, the command returns a detailed description of error(s).

In vJson, pass a JSON object containing the JSON contents to be validated.

Note: Validating a JSON string consists of checking that it follows the rules defined in a JSON schema. This is different from checking that the JSON is well-formed, which is done by the JSON Parse command.

In vSchema, pass the JSON schema to use for the validation. For more information on how to create a JSON schema, you may consult the json-schema.org web site.

Note: To validate a JSON object, 4D uses the norm described in the JSON Schema Validation document (this draft is still being written and can evolve in the future). 4D's current implementation is based upon the version 4 of the draft.

If the JSON schema is not valid, 4D returns a Null object and throws an error that can be caught by an on error call method.

The JSON Validate returns an object that provides the status of the validation. This object can contain the following properties:

Property nameTypeDescription
successBooleanTrue if vJson is validated, false otherwise. If false, the errors property is also returned
errorsObject collectionList of error objects if the vJson is not validated (see below)

Each error object of the errors collection contains the following properties:

Property nameTypeDescription
codeNumberError code
jsonPathStringJSON path that cannot be validated in vJson
lineNumberLine number of the error in the JSON file. This property is filled if the JSON has been parsed by JSON Parse with the * parameter. Otherwise, the property is omitted.
messageStringError message
offsetNumberLine offset of the error in the JSON file. This property is filled if the JSON has been parsed by JSON Parse with the * parameter. Otherwise, the property is omitted.
schemaPathsStringJSON path in the schema that causes the validation error
Error management

The following errors may be returned :

CodeJSON KeywordMessage
2multipleOfError while validating against 'multipleOf' key.
3maximumThe value provided should not be greater than specified in the schema ("{s1}").
4exclusiveMaximumThe value provided should be less than specified in the schema ("{s1}").
5minimumThe value provided should not be less than specified in the schema ("{s1}").
6exclusiveMinimumThe value provided should be greater than specified in the schema ("{s1}").
7maxLengthThe string is longer than specified in the schema.
8minLengthThe string is shorter than specified in the schema.
9patternThe string "{s1}" does not match the pattern in the schema:{s2}.
10additionalItemsError while validating an array. JSON contains more elements than specified in the schema.
11maxItemsThe array contains more items than specified in the schema.
12minItemsThe array contains less items than specified in the schema.
13uniqueItemsError while validating an array. Elements are not unique. Another instance of "{s1}" is already in the array.
14maxPropertiesThe number of properties is greater than specified in the schema.
15minPropertiesThe number of properties is less than specified in the schema.
16requiredThe required property "{s1}" is missing.
17additionalPropertiesNo additional properties allowed by the schema. The property(ies) {s1} should be removed.
18dependenciesThe property "{s1}" requires the property "{s2}".
19enumError while validating against 'enum' key. "{s1}" does not match any enum element in the schema.
20typeIncorrect type. Expected type is: {s1}
21oneOfThe JSON matches more than one value.
22oneOfThe JSON does not match any value.
23notThe JSON is valid against the value of 'not'.
24formatThe string does not match ("{s1}")

Example

You want to validate a JSON object with a schema and get the list of validation errors, if any, and store error lines and messages in a text variable:

 var $oResult : Object
 $oResult:=JSON Validate(JSON Parse(myJson;*);mySchema)
 If($oResult.success) //validation successful
    ...
 Else //validation failed
    var $vLNbErr : Integer
    var $vTerrLine : Text
    $vLNbErr:=$oResult.errors.length ///get the number of error(s)
    ALERT(String($vLNbErr)+" validation error(s) found.")
    For($i;0;$vLNbErr)
       $vTerrLine:=$vTerrLine+$oResult.errors[$i].message+" "+String($oResult.errors[$i].line)+Carriage return
    End for
 End if

Note: This example requires that object notation is activated (see the Compatibility page).

See also

JSON Parse