santhosh-tekuri/jsonschema

Feature: Output compiled schema as JSON – "Schema.Marshal()"

jazzu opened this issue · 3 comments

jazzu commented

The Problem

In my current project we manage many schemas, subschemas, and schemas for schemas, etc. and put them together as needed with AddResource()/Compile(). When creating new schemas, I find that finding possible mistakes is that much harder without seeing the compilation result. Hunting down possible mistakes with JSON shards is painful, with the feedback loop being too long for efficient development.

The Solution

Schema struct could have a method Marshal() which is essentially the same as, for example, stdlib function json.Marshal(). The resulting byte slice could then be easily inspected for errors in the compiled schema.

A bonus would be MarshalIndent() to pretty-print the schema JSON.

this feature does not solve the problem. the original schema file might have its own indentation style. even we give index in the resulting byte slice, user still have to locate the error again in original schema file.

when schema compilation fails it returns *SchemaError.
if there is any validation error which the schema, then *SchemaError.Err will be of type *ValidationError
*ValidationError.InstanceLocation gives you location within schema where the error is. the location is given as jsonpath.

if the information provided within the error is unable to locate where error is, then raise an issue with details.

i guess, if there is an error in another schema which is ref-ed from original schema, currently the error returned does not say which schema file has error. it always says the error is in main schema file.

if it is so, let me know. that is a bug and needs to be fixed.

jazzu commented

The trouble has been mostly about understanding how the refs end up pointing, but the more I work on the tougher issues, the more I grok JSON Schema. If I run into such nested-errors-reported-at-top-level issues, I will open another issue here.

Closing this, thanks for listening :-)