Class: MetadataPresenter::ValidateSchema
- Inherits:
-
Object
- Object
- MetadataPresenter::ValidateSchema
- Defined in:
- app/validators/metadata_presenter/validate_schema.rb
Class Method Summary collapse
- .before(controller) ⇒ Object
- .find(schema_name) ⇒ Object
- .validate(metadata, schema_name) ⇒ Object
- .validate_pages(pages) ⇒ Object
Class Method Details
.before(controller) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/validators/metadata_presenter/validate_schema.rb', line 6 def before(controller) return unless controller.request.post? validate(controller.request.params, 'request.service') = controller.request.params['metadata'] validate_pages(['pages']) validate_pages(['standalone_pages']) rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError, SchemaNotFoundError => e controller.render( json: ErrorsSerializer.new(message: e.).attributes, status: :unprocessable_entity ) end |
.find(schema_name) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/validators/metadata_presenter/validate_schema.rb', line 26 def find(schema_name) schema_file = schema_name.gsub('.', '/') schema = JSON.parse( File.read( File.join( Rails.application.config.schemas_directory, "#{schema_file}.json" ) ) ) jschema = JSON::Schema.new(schema, Addressable::URI.parse(schema['_name'])) JSON::Validator.add_schema(jschema) JSON::Validator.schema_for_uri(schema_name).schema rescue Errno::ENOENT raise SchemaNotFoundError, "Schema not found => #{schema_name}" end |
.validate(metadata, schema_name) ⇒ Object
21 22 23 24 |
# File 'app/validators/metadata_presenter/validate_schema.rb', line 21 def validate(, schema_name) schema = JSON::Validator.schema_for_uri(schema_name)&.schema || find(schema_name) JSON::Validator.validate!(schema, ) end |
.validate_pages(pages) ⇒ Object
42 43 44 45 46 |
# File 'app/validators/metadata_presenter/validate_schema.rb', line 42 def validate_pages(pages) Array(pages).each do |page| validate(page, page['_type']) end end |