validateJson

Validates a JSON string against a JSON Schema (also given as a string) and returns an array of violation structs, each with message (the human-readable problem) and pointerToViolation (a JSON pointer to the offending member, "#" for the document root). The array is empty when the JSON is valid. When throwOnError is true and there are violations, an error is thrown with the first violation's message instead. baseUri resolves relative schema references. Available when the jsonschema cargo feature is enabled.

validateJson(json, schema [, throwOnError] [, baseUri]) → returns array

This function requires RustCFML.  Not supported on Lucee, Adobe ColdFusion, etc.

Argument Reference

json string
Required

The JSON string to validate.

schema string
Required

The JSON Schema string to validate against.

throwOnError boolean
Default: false

True to throw on the first violation instead of returning the array (default false).
Values:
  • true
  • false

baseUri string

Optional base URI for resolving relative $refs in the schema.

Examples
Sample code invoking the validateJson function

A valid document returns an empty array; an invalid one lists the violations.

schema = serializeJSON({ type: "object", properties: { name: { type: "string" } }, required: ["name"] });
writeDump(arrayLen(validateJson(serializeJSON({name: "Alex"}), schema)));
errors = validateJson(serializeJSON({age: 30}), schema);
writeDump(errors[1].message);

Expected Result: 0 name is a required property

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub