Class: Schemas::JsonSchemaValidator

Inherits:
Validator
  • Object
show all
Defined in:
app/models/schemas/json_schema_validator.rb

Overview

Checks payloads against a JSON Schema, using json_schemer. The JSON Schema version (draft) comes from the schema's $schema keyword, and 2020-12 is used when it has none. Formats, such as uuid, are checked, and remote references are not fetched.

Defined Under Namespace

Classes: InvalidSchema

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ JsonSchemaValidator

Returns a new instance of JsonSchemaValidator.

Parameters:

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/schemas/json_schema_validator.rb', line 15

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

Instance Method Details

#validate(payload) ⇒ Array<Hash>

Checks a payload against the schema.

A missing required property is reported against the object that lacks it.

Parameters:

  • payload (Object) —

    the parsed JSON payload

Returns:

  • (Array<Hash>) —

    the errors, each with a JSON pointer (:pointer) and a message (:detail); empty when the payload is valid



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/schemas/json_schema_validator.rb', line 26

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end