Giter Club home page Giter Club logo

Comments (5)

davishmcclurg avatar davishmcclurg commented on June 26, 2024 1

@Jbern16 you're referenced schema doesn't need the wrapping Host key:

>> schemaWithRefs  = {
?>   "$id" => "NAME_HERE",
?>   "$schema" => "http://json-schema.org/draft-04/schema#",
?>   "type" => "object",
?>   "description" => "A Host entity has been created or changed",
?>   "required" => ["host"],
?>   "additionalProperties" => false,
?>   "properties" => {
?>     "host" => {
?>       "description" => "Host",
?>       "allOf" => [{"$ref" => "https://my-url-here.com/EDM.json#Host"}],
?>       "required" => ["attribute1"]
>>     }
>>   }
>> }
=> {"$id"=>"NAME_HERE", "$schema"=>"http://json-schema.org/draft-04/schema#", "type"=>"object", "description"=>"A Host entity has been created or changed", "required"=>["host"], "additionalProperties"=>false, "properties"=>{"host"=>{"description"=>"Host", "allOf"=>[{"$ref"=>"https://my-url-here.com/EDM.json#Host"}], "required"=>["attribute1"]}}}
>>
>> refSchema = {
?>   "$id" => "#Host",
?>   "properties" => { "attribute1" => { "type" => "string"  } }
>> }
=> {"$id"=>"#Host", "properties"=>{"attribute1"=>{"type"=>"string"}}}
>>
>> schema = JSONSchemer.schema(schemaWithRefs, ref_resolver: proc { refSchema })
=> #<JSONSchemer::Schema::Draft4:0x00007fe5b932fce8 @root={"$id"=>"NAME_HERE", "$schema"=>"http://json-schema.org/draft-04/schema#", "type"=>"object", "description"=>"A Host entity has been created or changed", "required"=>["host"], "additionalProperties"=>false, "properties"=>{"host"=>{"description"=>"Host", "allOf"=>[{"$ref"=>"https://my-url-here.com/EDM.json#Host"}], "required"=>["attribute1"]}}}, @format=true, @insert_property_defaults=false, @formats=nil, @keywords=nil, @ref_resolver=#<Proc:0x00007fe5b932fdd8@(irb):47>>
>>
>> schema.valid?({ 'host' => { "attribute1" => nil } })
=> false
>> schema.valid?({ 'host' => { "attribute1" => 'x' } })
=> true

from json_schemer.

Jbern16 avatar Jbern16 commented on June 26, 2024

heh closing, the issue was with the capitalization of Host not matching the data

from json_schemer.

Jbern16 avatar Jbern16 commented on June 26, 2024

Sorry, re-opening. That doesn't actually seem to solve the issue, it was invalidating for a different reason.

from json_schemer.

Jbern16 avatar Jbern16 commented on June 26, 2024

Thanks! I’ll try this out. At first glance I don’t think this will work as Host is part of a larger schema and contains pointers to other formats / refs, so don’t think I can just extract that one piece. Maybe I can solve this utilizing a ref resolver.

Thanks again for your help

from json_schemer.

davishmcclurg avatar davishmcclurg commented on June 26, 2024

At first glance I don’t think this will work as Host is part of a larger schema and contains pointers to other formats / refs, so don’t think I can just extract that one piece. Maybe I can solve this utilizing a ref resolver.

@Jbern16 ah, I think I misunderstood what you were trying to do. You should be able to make it work by referencing the subschema with a JSON pointer instead of $id: {"$ref": "http://localhost:8000/ref.json#/Host"}

Using $id is a little bit tricky and I think it will only work if the $id is at the top level or under definitions:

def resolve_ids(schema, ids = {}, parent_uri = nil, pointer = '')
if schema.is_a?(Array)
schema.each_with_index { |subschema, index| resolve_ids(subschema, ids, parent_uri, "#{pointer}/#{index}") }
elsif schema.is_a?(Hash)
id = schema[id_keyword]
uri = join_uri(parent_uri, id)
unless uri == parent_uri
ids[uri.to_s] = {
schema: schema,
pointer: pointer
}
end
if definitions = schema['definitions']
definitions.each { |key, subschema| resolve_ids(subschema, ids, uri, "#{pointer}/definitions/#{key}") }
end
end
ids
end

from json_schemer.

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.