Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ gem 'httparty'
# Autoload dotenv in Rails. (https://github.com/bkeepers/dotenv)
gem 'dotenv-rails'

gem 'activerecord_json_validator'

# ================================= #
# ENVIRONMENT SPECIFIC DEPENDENCIES #
# ================================= #
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ GEM
activemodel (= 5.2.6.2)
activesupport (= 5.2.6.2)
arel (>= 9.0)
activerecord_json_validator (2.1.0)
activerecord (>= 4.2.0, < 8)
json_schemer (~> 0.2.18)
activestorage (5.2.6.2)
actionpack (= 5.2.6.2)
activerecord (= 5.2.6.2)
Expand Down Expand Up @@ -130,6 +133,8 @@ GEM
dragonfly-s3_data_store (1.3.0)
dragonfly (~> 1.0)
fog-aws
ecma-re-validator (0.4.0)
regexp_parser (~> 2.2)
erubi (1.10.0)
excon (0.91.0)
execjs (2.8.1)
Expand Down Expand Up @@ -207,6 +212,7 @@ GEM
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
hana (1.3.7)
hashdiff (1.0.1)
hashie (5.0.0)
highline (2.0.3)
Expand All @@ -223,6 +229,11 @@ GEM
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.6.1)
json_schemer (0.2.19)
ecma-re-validator (~> 0.3)
hana (~> 1.3)
regexp_parser (~> 2.0)
uri_template (~> 0.7)
jwt (2.3.0)
kaminari (1.2.2)
activesupport (>= 4.1.0)
Expand Down Expand Up @@ -474,6 +485,7 @@ GEM
thread_safe (~> 0.1)
unicode-display_width (2.1.0)
uniform_notifier (1.14.2)
uri_template (0.7.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (3.7.0)
Expand Down Expand Up @@ -515,6 +527,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
activerecord_json_validator
annotate
annotate_gem
api-pagination
Expand Down
16 changes: 16 additions & 0 deletions app/models/license.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ class License < ApplicationRecord
# Remove any preferred licenses that could not be found in the table
licenses.compact
}

# varchar(255) NOT NULL
validates :name,
presence: { message: PRESENCE_MESSAGE },
length: { in: 0..255, allow_nil: false }

# varchar(255) NOT NULL
validates :identifier,
presence: { message: PRESENCE_MESSAGE },
length: { in: 0..255, allow_nil: false }

# varchar(255) NOT NULL
validates :uri,
presence: { message: PRESENCE_MESSAGE },
length: { in: 0..255, allow_nil: false }

end
59 changes: 59 additions & 0 deletions app/models/metadata_standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@
# rdamsc_id :string
#
class MetadataStandard < ApplicationRecord

# =============
# = Constants =
# =============

# keep "=>" syntax as json_schemer requires string keys
SCHEMA_RELATED_ENTITIES = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"type" => "array",
"items" => {
"type" => "object",
"properties" => {
"id" => { "type" => "string" },
"role" => { "type" => "string" }
}
}
}

SCHEMA_LOCATIONS = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"type" => "array",
"items" => {
"type" => "object",
"properties" => {
"url" => { "type" => "string" },
"type" => { "type" => "string" }
}
}
}

# ================
# = Associations =
# ================
Expand All @@ -29,4 +59,33 @@ class MetadataStandard < ApplicationRecord
term = term.downcase
where('LOWER(title) LIKE ?', "%#{term}%").or(where('LOWER(description) LIKE ?', "%#{term}%"))
}

# varchar(255) DEFAULT NULL
validates :title,
length: { maximum: 255 }

# varchar(255) DEFAULT NULL
validates :rdamsc_id,
length: { maximum: 255 }

# varchar(255) DEFAULT NULL
validates :uri,
length: { maximum: 255 }

# json DEFAULT NULL
validates :related_entities,
json: {
schema: SCHEMA_RELATED_ENTITIES,
message: ->(errors) { errors }
},
allow_nil: true

# json DEFAULT NULL
validates :locations,
json: {
schema: SCHEMA_LOCATIONS,
message: ->(errors) { errors }
},
allow_nil: true

end
102 changes: 102 additions & 0 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,73 @@
#

class Repository < ApplicationRecord

# =============
# = Constants =
# =============

# keep "=>" syntax as json_schemer requires string keys
SCHEMA_INFO = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"type" => "object",
"properties" => {
"types" => {
"type" => "array",
"items" => {
"type" => "string"
}
},
"keywords" => {
"type" => "array",
"items" => {
"type" => "string"
}
},
"subjects" => {
"type" => "array",
"items" => {
"type" => "string"
}
},
"access" => {
"type" => "string",
"enum" => ["open", "restricted", "closed"]
},
"provider_types" => {
"type" => "array",
"items" => {
"type" => "string"
}
},
"upload_types" => {
"type" => "array",
"items" => {
"type" => "object",
"properties" => {
"type" => { "type" => "string" },
"restriction" => { "type" => "string" }
},
"required" => ["type","restriction"]
}
},
"policies" => {
"type" => "array",
"items" => {
"type" => "object",
"properties" => {
"name" => { "type" => "string" },
"url" => { "type" => "string" }
},
"required" => ["name","url"]
}
},
"pid_system" => {
"type" => "string"
}
}
}


# ================
# = Associations =
# ================
Expand Down Expand Up @@ -51,4 +118,39 @@ class Repository < ApplicationRecord
scope :by_facet, lambda { |facet|
where(safe_json_where_clause(column: 'info', hash_key: 'keywords'), "%#{facet}%")
}

# ===============
# = Validations =
# ===============

# varchar(255) NOT NULL
validates :name,
presence: { message: PRESENCE_MESSAGE },
length: { in: 0..255, allow_nil: false }

# text NOT NULL
validates :description,
presence: { message: PRESENCE_MESSAGE }

# varchar(255) NOT NULL
validates :uri,
presence: { message: PRESENCE_MESSAGE },
length: { in: 0..255, allow_nil: false }

# varchar(255) DEFAULT NULL
validates :homepage,
length: { maximum: 255 }

# varchar(255) DEFAULT NULL
validates :contact,
length: { maximum: 255 }

# json DEFAULT NULL
validates :info,
json: {
schema: SCHEMA_INFO,
message: ->(errors) { errors }
},
allow_nil: true

end