Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could we introduce extension methods for validating JsonDocument/json string by json schema #237

Closed
WeihanLi opened this issue Mar 22, 2022 · 4 comments · Fixed by #238 or #240
Closed
Labels
enhancement New feature in an existing library

Comments

@WeihanLi
Copy link
Contributor

Environment

  • Nuget Package: JsonSchema.Net
  • Nuget Version: 2.1.2
  • OS: Windows/Linux
  • .Net Target: .NET 6

Can this library make doing something simpler?

Maybe we could introduce some extensions to make it more easier, for example:

public static ValidationResults Validate(this JsonSchema jsonSchema, JsonDocument jsonDocument, ValidationOptions validationOptions = null)
{
    return jsonSchema.Validate(jsonDocument.RootElement, validationOptions);
}

public static ValidationResults Validate(this JsonSchema jsonSchema, string jsonString, ValidationOptions validationOptions = null)
{
    using var jsonDocument = JsonDocument.Parse(jsonString);
    return jsonSchema.Validate(jsonDocument, validationOptions);
}

Describe alternatives you've considered

Not sure if it's reasonable and valuable to add the JsonDocument method as an instance method

Additional context

I'm new here, maybe there're some magic ways that I don't know

@WeihanLi WeihanLi added the enhancement New feature in an existing library label Mar 22, 2022
@WeihanLi WeihanLi changed the title Could we introduce an extension method for validating JsonDocument/json string by json schema easier Could we introduce extension methods for validating JsonDocument/json string by json schema easier Mar 22, 2022
@WeihanLi WeihanLi changed the title Could we introduce extension methods for validating JsonDocument/json string by json schema easier Could we introduce extension methods for validating JsonDocument/json string by json schema Mar 22, 2022
@gregsdennis
Copy link
Owner

gregsdennis commented Mar 22, 2022

I remember I had some problems with the string one in Manatee.Json. But that was due to the JSON model having an implicit cast from string (to make a JSON string), so there was ambiguity. I don't think that'll be an issue for this library.

I think these can be added in. Can you create a PR?

@WeihanLi
Copy link
Contributor Author

Sure, thanks

@gregsdennis
Copy link
Owner

@WeihanLi just letting you know that the move to JsonNode is causing a need to remove the extension that takes a string. It turns out that the implicit string -> JsonNode cast is taking precedence on these calls, which means the extension method isn't used.

This is a breaking change that will only appear at runtime. Your existing code that uses this extension method will likely still compile, but validations will now fail.

@WeihanLi
Copy link
Contributor Author

@gregsdennis many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature in an existing library
Projects
None yet
2 participants