Table of Contents

ValidatorExtensions Class

Definition

Namespace
Codebelt.Extensions.Newtonsoft.Json
Assemblies
Codebelt.Extensions.Newtonsoft.Json.dll
Source
src/Codebelt.Extensions.Newtonsoft.Json/ValidatorExtensions.cs

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

Name Description
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.

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.