Class ValidatorExtensions
- Namespace
- Codebelt.Extensions.Newtonsoft.Json
- Assembly
- Codebelt.Extensions.Newtonsoft.Json.dll
Extension methods for the Validator class.
public static class ValidatorExtensions
- Inheritance
-
ValidatorExtensions
Examples
The following example validates JSON document format before parsing.
using System;
using Codebelt.Extensions.Newtonsoft.Json;
using Cuemon;
namespace Examples;
class ValidatorExtensionsExample
{
static void Main()
{
var validJson = """{ "id": "123", "name": "Test" }""";
var invalidJson = """{ "id" "123" }""";
try
{
Validator.ThrowIf.InvalidJsonDocument(validJson);
Console.WriteLine("Valid JSON passed validation");
}
catch (ArgumentException ex)
{
Console.WriteLine($"Validation failed: {ex.Message}");
}
try
{
Validator.ThrowIf.InvalidJsonDocument(invalidJson);
}
catch (ArgumentException ex)
{
Console.WriteLine($"Invalid JSON caught: {ex.Message}");
}
}
}
Methods
InvalidJsonDocument(Validator, ref JsonReader, string, string)
Validates and throws an ArgumentException if the specified argument is not a valid JSON representation as specified in RFC 8259.
public static void InvalidJsonDocument(this Validator _, ref JsonReader argument, string message = "Value must be a JSON representation that complies with RFC 8259.", string paramName = null)
Parameters
_ValidatorThe Validator to extend.
argumentJsonReaderThe Newtonsoft.Json.JsonReader to be evaluated.
messagestringA message that describes the error.
paramNamestringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argumentmust be a JSON representation that complies with RFC 8259.
InvalidJsonDocument(Validator, string, string, string)
Validates and throws an ArgumentException if the specified argument is not a valid JSON representation as specified in RFC 8259.
public static void InvalidJsonDocument(this Validator _, string argument, string message = "Value must be a JSON representation that complies with RFC 8259.", string paramName = null)
Parameters
_ValidatorThe Validator to extend.
argumentstringThe JSON string to be evaluated.
messagestringA message that describes the error.
paramNamestringThe name of the parameter that caused the exception.
Exceptions
- ArgumentException
argumentmust be a JSON representation that complies with RFC 8259.