Coder Social home page Coder Social logo

Comments (4)

whatevergeek avatar whatevergeek commented on May 22, 2024 4

thanks a lot for the clarification @gregsdennis

I've set the OutputFormat as advised and it works now.
Here's how I've implemented:

    public class Validator
    {
        public string Schema { get; set; }
        public bool IsValid(string data, out string result)
        {
            if (!string.IsNullOrWhiteSpace(Schema))
            {
                var doc = JsonDocument.Parse(data);
                JsonSchema schema = JsonSchema.FromText(Schema);

                var options = new ValidationOptions { OutputFormat = OutputFormat.Detailed };
                var validationResult = schema.Validate(doc.RootElement, options);

                result = GetValidationResultMessage(validationResult);
                return validationResult.IsValid;
            }
            else
            {
                throw new Exception("Schema not set");
            }
        }

        private string GetValidationResultMessage(ValidationResults validationResult)
        {
            var resultMessage = string.Empty;
            if (validationResult.IsValid)
            {
                resultMessage = "Validation Succeeded.";
            }
            else if (validationResult.NestedResults.Count > 0)
            {
                resultMessage = "Validation Errors: ";
                foreach (var result in validationResult.NestedResults)
                {
                    resultMessage += $"{result.Message} found at {result.SchemaLocation.Source}| ";
                }
            }
            else
            {
                resultMessage = $"Validation Errors: {validationResult.Message} found at {validationResult.SchemaLocation.Source}| ";
            }

            return resultMessage;
        }

    }

I've added some test cases in the sample demo project to confirm the behaviour.
Just an observation, if validationResult.NestedResults.Count is more than 1, the main validationResult.Message returns null.
Hence, I had to check for it in my implementation above. Is this behavior intended?
Anyway, I'm ok to proceed with this. Feel free to let me know if there's a better usage.

from json-everything.

whatevergeek avatar whatevergeek commented on May 22, 2024 1

thanks a lot @gregsdennis
i'm so glad to be able to finally use your library
i'm closing this question now
all the best and thanks for your open source effort/contributions
highly appreciated! :-)

from json-everything.

gregsdennis avatar gregsdennis commented on May 22, 2024

How coincidental! I was looking at your repo yesterday.

It looks like you're getting just the basic validation result (and the default because it's faster), which is just a pass/fail. To get more information, including an error message, you need to set up for one of the other output formats.

var options = new ValidationOptions { OutputFormat = OutputFormat.Detailed };
var results = schema.Validate(instance, options);

See the docs for more information.

from json-everything.

gregsdennis avatar gregsdennis commented on May 22, 2024

This behavior is intended. Keywords that hold schemas are called "applicators." These keywords have no message (in this implementation due to my choice) because it's the messages from the contained keywords that hold the interesting data.

The spec says that the error messaging is something that the implementation can decide.

from json-everything.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.