Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Jun 3, 2020
1 parent 26b3d08 commit 96eb5df
Show file tree
Hide file tree
Showing 11 changed files with 524 additions and 60 deletions.
55 changes: 55 additions & 0 deletions lib/rbs/json_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require "json-schema"

module RBS
class JSONValidator
class Validator
attr_reader :file

def initialize(name)
@file = Pathname(__dir__) + "../../schema/#{name}.json"
end

def validate(object, fragment: nil)
JSON::Validator.validate(
file.to_s,
object,
{ fragment: fragment }
)
end

def validate!(object, fragment: nil)
JSON::Validator.validate!(
file.to_s,
object,
{ fragment: fragment }
)
end
end

class <<self
def location
Validator.new("location")
end

def types
Validator.new("types")
end

def comment
Validator.new("comment")
end

def method_type
Validator.new("methodType")
end

def members
Validator.new("members")
end

def decls
Validator.new("decls")
end
end
end
end
1 change: 1 addition & 0 deletions rbs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rubocop-rubycw"
spec.add_development_dependency "minitest-reporters", "~> 1.3.6"
spec.add_development_dependency "json", "~> 2.3.0"
spec.add_development_dependency "json-schema", "~> 2.8"
end
2 changes: 1 addition & 1 deletion schema/annotation.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Annotation associated to a declaration or a member: `%a{rbs:test}`, `%a{steep:deprecated}`, ...",
"type": "object",
"properties": {
Expand Down
30 changes: 21 additions & 9 deletions schema/comment.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"comment": {
"type": "object",
"properties": {
"string": {
"type": "string"
},
"location": {
"$ref": "location.json"
}
},
"required": ["string", "location"]
}
},
"title": "Comment associated with a declaration or a member",
"type": "object",
"properties": {
"string": {
"type": "string"
"oneOf": [
{
"$ref": "#/definitions/comment"
},
"location": {
"$ref": "location.json"
{
"type": "null"
}
},
"required": ["string", "location"]
]
}
91 changes: 91 additions & 0 deletions schema/decls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"alias": {
"title": "Type alias declaration: `type foo = Integer`, ...",
"type": "object",
"properties": {
"declaration": {
"type": "string",
"enum": ["alias"]
},
"name": {
"type": "string"
},
"type": {
"$ref": "types.json"
},
"annotations": {
"type": "array",
"items": {
"$ref": "annotation.json"
}
},
"location": {
"$ref": "location.json"
},
"comment": {
"$ref": "comment.json"
}
},
"required": ["declaration", "name", "type", "annotations", "location", "comment"]
},
"constant": {
"title": "Constant declaration: `VERSION: String`, ...",
"type": "object",
"properties": {
"declaration": {
"type": "string",
"enum": ["constant"]
},
"name": {
"type": "string"
},
"type": {
"$ref": "types.json"
},
"location": {
"$ref": "location.json"
},
"comment": {
"$ref": "comment.json"
}
},
"required": ["declaration", "name", "type", "comment", "location"]
},
"global": {
"title": "Global declaration: `$DEBUG: bool`, ...",
"type": "object",
"properties": {
"declaration": {
"type": "string",
"enum": ["global"]
},
"name": {
"type": "string"
},
"type": {
"$ref": "types.json"
},
"location": {
"$ref": "location.json"
},
"comment": {
"$ref": "comment.json"
}
},
"required": ["declaration", "name", "type", "comment", "location"]
}
},
"oneOf": [
{
"$ref": "#/definitions/alias"
},
{
"$ref": "#/definitions/constant"
},
{
"$ref": "#/definitions/global"
}
]
}
2 changes: 1 addition & 1 deletion schema/function.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"param": {
"title": "Function parameter with type and optional name: `Integer size`, `::String name`, `untyped`, ...",
Expand Down
43 changes: 30 additions & 13 deletions schema/location.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"point": {
"type": "object",
Expand All @@ -17,23 +17,40 @@
"type": "object",
"properties": {
"name": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "null"
}
]
}
},
"required": ["name"]
},
"location": {
"type": "object",
"properties": {
"start": {
"$ref": "#/definitions/point"
},
"end": {
"$ref": "#/definitions/point"
},
"buffer": {
"$ref": "#/definitions/buffer"
}
},
"required": ["start", "end", "buffer"]
}
},
"type": "object",
"properties": {
"start": {
"$ref": "#/definitions/point"
},
"end": {
"$ref": "#/definitions/point"
"oneOf": [
{
"$ref": "#/definitions/location"
},
"buffer": {
"$ref": "#/definitions/buffer"
{
"type": "null"
}
},
"required": ["start", "end", "buffer"]
]
}
34 changes: 5 additions & 29 deletions schema/members.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"methodDefinition": {
"type": "object",
"properties": {
"member": {
"type": "string",
"const": "method_definition"
"enum": ["method_definition"]
},
"kind": {
"oneOf": [
{
"const": "instance"
},
{
"const": "singleton"
},
{
"const": "singleton_instance"
}
]
"enum": ["instance", "singleton", "singleton_instance"]
},
"types": {
"type": "array",
Expand All @@ -28,14 +18,7 @@
}
},
"comment": {
"oneOf": [
{
"$ref": "comment.json"
},
{
"type": "null"
}
]
"$ref": "comment.json"
},
"annotations": {
"type": "array",
Expand Down Expand Up @@ -73,14 +56,7 @@
"$ref": "location.json"
},
"comment": {
"oneOf": [
{
"$ref": "comment.json"
},
{
"type": "null"
}
]
"$ref": "comment.json"
}
},
"required": ["member", "name", "type", "location", "comment"]
Expand Down
6 changes: 4 additions & 2 deletions schema/methodType.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"block": {
"type": "object",
Expand Down Expand Up @@ -28,7 +28,9 @@
},
"block": {
"oneOf": [
{ "$ref": "#/definitions/block" },
{
"$ref": "#/definitions/block"
},
{
"type": "null"
}
Expand Down
Loading

0 comments on commit 96eb5df

Please sign in to comment.