Implementing JSON Schema validation

SyntaxEditor Web Languages Add-on for WPF Forum

The latest build of this product (v22.1.3) was released 20 days ago, which was before this thread was created.
Posted 15 days ago by 10e
Version: 22.1.3
Avatar

I'm new to SyntaxEditor and read through the documentation and still unsure how to customize the JsonSyntaxLanguage to implement JSON Schema validation similar to how XmlSyntaxLanguage performs DTD validation with XmlValidator and perhaps the XmlSchemaResolver. I couldn't find those counterparts in the JsonSyntaxLanguage, so I assume they are custom only to the XmlSyntaxLanguage implementation?

Comments (2)

Answer - Posted 14 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

That is correct, we don't have anything at the moment built-in for JSON validation.  As far as I know, JSON validation isn't in .NET and is only in third-party libraries like Newtonsoft's Json.NET, right?

That being said, our syntax languages are fully extensible and you can add this kind of thing if you wish.

In our XML add-on, we override our XmlParser's CreateParseData method and call a Validate method in that, passing it the request and state parameters.  You could do the same kind of thing by making a class that inherits JsonParser, and override CreateParseData to call a Validate method you add, then call the base CreateParseData method.  Unregister our default JsonParser service from the language and register an instance of your custom JsonParser-based class instead.

Here's what our XmlParser.Validate method looks like:

protected virtual void Validate(IParseRequest request, IParserState state) {
	if (request == null)
		throw new ArgumentNullException("request");
	if (state == null)
		throw new ArgumentNullException("state");

	if ((request.TextBufferReader != null) && (request.Language != null)) {
		// If there is a validator service on the language...
		IXmlValidator validator = request.Language.GetService<IXmlValidator>();
		if (validator != null) {
			// Validate the XML
			var ast = (state.AstNodeBuilder != null ? state.AstNodeBuilder.RootNode : null);
			IEnumerable<IParseError> validationErrors = validator.Validate(request, ast);
			if (validationErrors != null) {
				// Add validation errors
				foreach (IParseError validationError in validationErrors) {
					if (validationError != null)
						state.ReportError(validationError);
				}
			}
		}
	}
}

We effectively look for another language service of type IXmlValidator and if found call into it with the request and AST result.  Then we take the IParseError results from that validation, and report them on the state object.

If you want to simplify things, instead of calling another validator language service, you could validate using Json.NET right from this Validate method.  The request.TextBufferReader will give you access to the snapshot's raw text.  Then report the errors that were found.

I hope that helps!


Actipro Software Support

Posted 14 days ago by 10e
Avatar

I appreciate the quick feedback and you are correct that JSON schema support is not included in .NET and only supported via 3rd party. From your example and explanation I now have a clear idea how to integrate schema validation for my needs. Thanks!

Add Comment

Please log in to a validated account to post comments.