Socket
Book a DemoInstallSign in
Socket

FluentAssertions.Extension.Json

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

FluentAssertions.Extension.Json

The library is an extensions for validating a json documents in Fluent assertions

1.1.2
Source
nugetNuGet
Version published
Maintainers
1
Created
Source

FluentAssertions.Extension.Json

The library is an extensions for validating a json documents in Fluent assertions

Unlike FluentAssertions.Json it is based on System.Text.Json instead of Newtonsoft.Json.

The syntax of the assertions is self-explaining.

Please check the example below for details:

Test Json file

{
    "a" : { "a1" : "text" },
    "b" : true,
    "c" : false,
    "d" : 1,
    "e" : 3.1415,
    "f" : "string",
    "i" : [ 1, 2, 3 ],
    "j" : null
}

The assertions

            //validate that the text is a correct json
            jsonText.Should().BeCorrectJson();

            //parse json using AsJson extension
            var json = jsonText.AsJson();

            //HaveProperty extension for Json Element
            json.HasProperty("a").Should().BeTrue();

            //validate that json has a property
            json.Should()
                .HaveProperty("a");

            //validate that json has no property
            json.Should()
                .HaveNoProperty("x");

            //validate that json has a property and the property is an object and has a text property
            //using which property
            json.Should()
                .HaveProperty("a")
                .Which.Should()
                    .NotBeNull()
                    .And
                    .BeObject()
                    .And
                    .HaveProperty("a1")
                        .Which.Should()
                        .Be("text");

            //or do the same without chaining
            json.Should()
                .HaveProperty("a");
            json.GetProperty("a").Should()
                    .BeObject()
                    .And
                    .HaveProperty("a1");

            json.GetProperty("a").GetProperty("a1").Should()
                    .Be("text");

            //validate boolean properties
            json.Should()
                .HaveProperty("b")
                .Which.Should()
                    .BeValue()
                    .And
                    .BeTrue()
                    .And
                    .Be(true);

            json.Should()
                .HaveProperty("c")
                .Which.Should()
                    .BeFalse()
                    .And
                    .Be(false);

            //check numbers
            json.Should()
                .HaveProperty("d")
                .Which.Should()
                    .Be(1)
                    .And
                    .BeIntegerMatching(x => x < 2);
;

            json.Should()
                .HaveProperty("e")
                .Which.Should()
                    .Be(3.1415)
                    .And
                    .Be(3.14, 0.005)
                    .BeNumberMatching(x => x > 3.0);

            //check string for equality
            json.Should()
                .HaveProperty("f")
                .Which.Should()
                    .Be("string")
                    .And
                    .Be("STRING", StringComparison.OrdinalIgnoreCase)
                    .And
                    .BeStringMatching(s => s.StartsWith("s"));

            //check string for matching a regular expression
            json.Should()
                .HaveProperty("f")
                .Which.Should()
                    .Match("^.tr.{3}$");

            //check three properties at a time
            json.Should()
                .HaveIntegerProperty("d", x => x == 1)
                .And
                .HaveNumberProperty("e", x => x > 3)
                .And
                .HaveStringProperty("f", s => s == "string");

            //check array
            json.Should()
                .HaveProperty("i")
                .Which.Should()
                .BeArray()
                .And
                .NotBeEmpty()
                .And
                .HaveLength(3)
                .And
                .HaveLengthAtLeast(1);

            json.GetProperty("i")[0].Should()
                .BeValue()
                .And
                .Be(1);

            json.Should()
                .HaveProperty("j")
                .Which.Should()
                    .BeNull();

Keywords

FAQs

Package last updated on 03 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.