Comments (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.
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.
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.
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)
- Support custom keywords in schema generation HOT 6
- Evaluate JsonRequired Attribute HOT 5
- Model with Nullable value type is invalid HOT 1
- Model is not valid when has integer value as string HOT 1
- that the custom metastructure could not be parsed HOT 4
- How to extend SchemaGenerator for DateTime HOT 7
- JsonSchema.NET DataValidation HOT 10
- Custom `IAttributeHandler` never called HOT 5
- Anchors naming of 2020 still following 2019 rules HOT 5
- Generating a Custom JSON Schema HOT 1
- ArgumentOutOfRangeException thrown when calling JsonSchema's Evaluate methods from multiple threads. HOT 3
- 2020-12 output incorrectly places annotations
- Nullable decimal type removes multipleOf HOT 1
- Boolean applicator keywords must be non-empty HOT 1
- Issues with rendering on docs site HOT 1
- Verified error reporting type HOT 18
- 'Required' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.RequiredAttribute' and 'Json.Schema.Generation.RequiredAttribute'? HOT 22
- Schema exception {"An item with the same key has already been added. Key: Child"} HOT 9
- Possibly wrong return type HOT 10
- Customizing the `format` generated for a field (without attributes) HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-everything.