supercharge the schemas (#874)

* fix map schema - woot it works

* update other schemas to include embeddable attrs

* update current user schema/examples
This commit is contained in:
Devin Howard 2016-10-29 22:58:31 +08:00 committed by GitHub
parent 1fbfd56d57
commit 5e0e44b436
12 changed files with 215 additions and 6 deletions

View file

@ -17,7 +17,7 @@ module Api
# only ask serializer to return is_admin field if we're on the
# current_user action
def default_scope
super.merge(show_is_admin: action_name == 'current')
super.merge(show_full_user: action_name == 'current')
end
end
end

View file

@ -8,7 +8,9 @@ module Api
:generation
attribute :is_admin,
if: -> { scope[:show_is_admin] && scope[:current_user] == object }
if: -> { scope[:show_full_user] && scope[:current_user] == object }
attribute :email,
if: -> { scope[:show_full_user] && scope[:current_user] == object }
def avatar
object.image.url(:sixtyfour)

View file

@ -4,6 +4,7 @@
"name": "user",
"avatar": "https://s3.amazonaws.com/metamaps-assets/site/user.png",
"generation": 0,
"is_admin": false
"is_admin": false,
"email": "user@example.com"
}
}

View file

@ -0,0 +1,34 @@
{
"name": "User",
"type": "object",
"properties": {
"id": {
"$ref": "_id.json"
},
"name": {
"type": "string"
},
"avatar": {
"format": "uri",
"type": "string"
},
"generation": {
"type": "integer",
"minimum": 0
},
"is_admin": {
"type": "boolean"
},
"email": {
"type": "string"
}
},
"required": [
"id",
"name",
"avatar",
"generation",
"is_admin",
"email"
]
}

View file

@ -30,35 +30,68 @@
"user_id": {
"$ref": "_id.json"
},
"user": {
"$ref": "_user.json"
},
"topic_ids": {
"type": "array",
"items": {
"$ref": "_id.json"
}
},
"topics": {
"type": "array",
"items": {
"$ref": "_topic.json"
}
},
"synapse_ids": {
"type": "array",
"items": {
"$ref": "_id.json"
}
},
"synapses": {
"type": "array",
"items": {
"$ref": "_synapse.json"
}
},
"mapping_ids": {
"type": "array",
"items": {
"$ref": "_id.json"
}
},
"mappings": {
"type": "array",
"items": {
"$ref": "_mapping.json"
}
},
"contributor_ids": {
"type": "array",
"items": {
"$ref": "_id.json"
}
},
"contributors": {
"type": "array",
"items": {
"$ref": "_user.json"
}
},
"collaborator_ids": {
"type": "array",
"items": {
"$ref": "_id.json"
}
},
"collaborators": {
"type": "array",
"items": {
"$ref": "_user.json"
}
}
},
"required": [
@ -70,5 +103,43 @@
"starred",
"created_at",
"updated_at"
],
"allOf": [
{
"oneOf": [
{ "required": [ "user_id" ] },
{ "required": [ "user" ] }
]
},
{
"oneOf": [
{ "required": [ "topic_ids" ] },
{ "required": [ "topics" ] }
]
},
{
"oneOf": [
{ "required": [ "synapse_ids" ] },
{ "required": [ "synapses" ] }
]
},
{
"oneOf": [
{ "required": [ "mapping_ids" ] },
{ "required": [ "mappings" ] }
]
},
{
"oneOf": [
{ "required": [ "contributor_ids" ] },
{ "required": [ "contributors" ] }
]
},
{
"oneOf": [
{ "required": [ "collaborator_ids" ] },
{ "required": [ "collaborators" ] }
]
}
]
}

View file

@ -27,8 +27,14 @@
"map_id": {
"$ref": "_id.json"
},
"map": {
"$ref": "_map.json"
},
"user_id": {
"$ref": "_id.json"
},
"user": {
"$ref": "_user.json"
}
},
"required": [
@ -37,5 +43,19 @@
"mappable_type",
"created_at",
"updated_at"
],
"allOf": [
{
"oneOf": [
{ "required": [ "map_id" ] },
{ "required": [ "map" ] }
]
},
{
"oneOf": [
{ "required": [ "user_id" ] },
{ "required": [ "user" ] }
]
}
]
}

View file

@ -24,11 +24,20 @@
"topic1_id": {
"$ref": "_id.json"
},
"topic1": {
"$ref": "_topic.json"
},
"topic2_id": {
"$ref": "_id.json"
},
"topic2": {
"$ref": "_topic.json"
},
"user_id": {
"$ref": "_id.json"
},
"user": {
"$ref": "_user.json"
}
},
"required": [
@ -38,5 +47,25 @@
"permission",
"created_at",
"updated_at"
],
"allOf": [
{
"oneOf": [
{ "required": [ "topic1_id" ] },
{ "required": [ "topic1" ] }
]
},
{
"oneOf": [
{ "required": [ "topic2_id" ] },
{ "required": [ "topic2" ] }
]
},
{
"oneOf": [
{ "required": [ "user_id" ] },
{ "required": [ "user" ] }
]
}
]
}

View file

@ -27,8 +27,14 @@
"user_id": {
"$ref": "_id.json"
},
"user": {
"$ref": "_user.json"
},
"metacode_id": {
"$ref": "_id.json"
},
"metacode": {
"$ref": "_metacode.json"
}
},
"required": [
@ -39,5 +45,19 @@
"permission",
"created_at",
"updated_at"
],
"allOf": [
{
"oneOf": [
{ "required": [ "user_id" ] },
{ "required": [ "user" ] }
]
},
{
"oneOf": [
{ "required": [ "metacode_id" ] },
{ "required": [ "metacode" ] }
]
}
]
}

View file

@ -15,9 +15,6 @@
"generation": {
"type": "integer",
"minimum": 0
},
"is_admin": {
"type": "boolean"
}
},
"required": [

View file

@ -0,0 +1,12 @@
{
"name": "Current User Envelope",
"type": "object",
"properties": {
"data": {
"$ref": "_current_user.json"
}
},
"required": [
"data"
]
}

View file

@ -78,6 +78,7 @@ RSpec.describe 'maps API', type: :request do
context 'RAML example' do
let(:resource) { get_json_example(:map) }
let(:collection) { get_json_example(:maps) }
let(:starred) { get_json_example(:map_starred) }
it 'resource matches schema' do
expect(resource).to match_json_schema(:map)
@ -86,5 +87,9 @@ RSpec.describe 'maps API', type: :request do
it 'collection matches schema' do
expect(collection).to match_json_schema(:maps)
end
it 'starred resource matches schema' do
expect(starred).to match_json_schema(:map)
end
end
end

View file

@ -30,4 +30,22 @@ RSpec.describe 'users API', type: :request do
expect(response).to match_json_schema(:user)
expect(JSON.parse(response.body)['data']['id']).to eq user.id
end
context 'RAML example' do
let(:resource) { get_json_example(:user) }
let(:collection) { get_json_example(:users) }
let(:current) { get_json_example(:current_user) }
it 'resource matches schema' do
expect(resource).to match_json_schema(:user)
end
it 'collection matches schema' do
expect(collection).to match_json_schema(:users)
end
it 'current_user resource matches schema' do
expect(current).to match_json_schema(:current_user)
end
end
end