salesking / json_schema_tools Goto Github PK
View Code? Open in Web Editor NEWTools for building and handling a JSON Schema powered API's
Tools for building and handling a JSON Schema powered API's
ActiveModel::Validations::EachValidator seems to be renamed or moved.
Need to build a switch to detect ActiveModel version 3 vs. 4
this may be a cleaner approach to prevent bad behavoir & habits
Does this work on things formatted in
http //json-schema.org/draft-02/schema#
as opposed to
http //json-schema.org/draft-04/schema#
I'm having a little trouble implementing, just wondering if i'm wasting my time / if its not compatible with draft-02 of the schema. Thanks!
Defining a method on a model that has the same name as a key that has been passed causes an error.
foo
foo
) and any value.from_hash
to create an instance of the modelclass Bar
include SchemaTools::Modules::Attributes
include SchemaTools::Modules::Validations
include SchemaTools::Modules::AsSchema
has_schema_attrs 'bar'
validate_with 'bar'
def foo
# do something
end
end
Bar.from_hash({ 'foo' => 'test value' })
e.g so a date-string is converted to a Date when we build object proprerties from a json string:
@VidurMalik added a change for nested objects, which are not present in the output json when nil:
#19
Still need to handle the other field types: plain properties & nested arrays.
A reasonable approach could be to add the following options:
ignore_null: true
=> which would leave out all nil values from the output jsonstrict: true
=> the parser throws an error when an schema property does not exists on the object e.g obj.respond_to?(field)
Such could be needed to find out why a key is missing in the json: does it exists or is it nil?Example:
Directory Structure
SCHEMA_PATH/v1/child.json
SCHEMA_PATH/v2/child.json
SCHEMA_PATH/v2/parent.json
If parent.json
references v2/child.json
, the current implementation of RefResolver::find_local_file_path
will return v1/child.json
instead of the expected v2/child.json
.
It will also return v1/child.json
even if the absolute path to v2/child.json
is specified in the $ref
.
So we deliver a field as string even if it is a number:
https://github.com/salesking/json_schema_tools/blob/master/lib/schema_tools/modules/hash.rb#L70
Let's start with number, string. Date&Datetime seem to be parsed right already by ruby .to_s
You can try it with schemas from https://github.com/fidor/fidor_schema.
Say I wanted to instantiate a schema from the response of a request to a URL or a read from a tuple in a document store, cache or database.
It seems all examples use a path
parameter to a filesystem.
Can't I instantiate a schema from a string (or array of strings) so that it can come from anywhere?
Hello,
I was reviewing this project. I was hoping that it would support a JSON file which contains references to itself. This is valid in JSON Schema, but it appears that this tool will take those references and always look to another json file in the project rather than see if that is a self reference. https://github.com/salesking/json_schema_tools/blob/master/lib/schema_tools/ref_resolver.rb#L32
Here is an example of a self referential JSON Schema:
{
"title":"Application Dashboards",
"type":"object",
"properties":{
"version":{
"type":{
"enum":[
1.0
]
}
},
"dashboards":{
"type":"array",
"minItems":1,
"items":{
"type":"object",
"properties":{
"name":{
"type":"string"
},
"team":{
"$ref":"#/definitions/team"
},
"services":{
"type":"array",
"items":{
"$ref":"#/definitions/service"
}
},
"layout":{
"$ref":"#/definitions/layout"
}
},
"required":[
"name",
"layout",
"services"
]
}
}
},
"required":[
"dashboards",
"version"
],
"definitions":{
"layout":{
"type":"object",
"oneOf":[
{
"title":"Quadrant Dashboard Layout",
"type":"object",
"properties":{
"type":{
"enum":[
"grid"
]
},
"widgets":{
"type":"array",
"items":{
"properties":{
"widget_id":{
"description":"The unique id that references a specific widget",
"type":"string"
},
"x":{
"description":"The grid column on which to place this widget. Origin is upper-left",
"type":"integer"
},
"y":{
"description":"The grid row on which to place this widget. Origin is upper-left",
"type":"integer"
},
"span_x":{
"description":"How many columns this widget will span",
"type":"integer"
},
"span_y":{
"description":"How many rows this widget will span",
"type":"integer"
}
},
"required":[
"widget_id",
"x",
"y",
"span_x",
"span_y"
]
}
}
},
"required":[
"type",
"widgets"
]
}
]
},
"team":{
"description":"The information about the team that supports this dashboard",
"type":"object",
"properties":{
"name":{
"description":"The name of this team",
"type":"string"
},
"email":{
"description":"The best contact email address for this team, usually a distribution list",
"type":"string",
"format":"email"
},
"defects":{
"description":"A URL to JIRA to input tickets for defects for this team",
"type":"string",
"format":"uri"
},
"slack":{
"description":"The slack chat room where a user can get support for this team",
"type":"string",
"pattern":"^\\#[a-z-]{1,21}$"
},
"pager_duty":{
"description":"The email @hanoc.pagerduty.com to trigger a PD alert for this team",
"type":"string",
"format":"email"
}
},
"required":[
"name",
"email"
]
},
"service":{
"description":"The information about the service that this dashboard reflects",
"type":"object",
"properties":{
"name":{
"description":"The name of this application",
"type":"string"
},
"portfolio":{
"description":"The portfolio that this application supports",
"type":"string"
},
"product":{
"description":"The product that this application supports",
"type":"string"
},
"slack":{
"description":"The slack chat room where a user can get support for this application",
"type":"string",
"pattern":"^\\#[a-z-]{1,21}$"
},
"repositories":{
"description":"An array of repositories that contribute to this application",
"type":"array",
"items":{
"type":"object",
"properties":{
"url":{
"description":"The url of the github repo for this application",
"type":"string",
"format":"uri"
}
},
"required":[
"url"
]
},
"minItems":1,
"uniqueItems":true
}
},
"required":[
"name",
"repositories",
"portfolio",
"product"
]
}
}
}
Its possible I am missing something, does this project support references?
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.