diff options
Diffstat (limited to 'app/validators')
4 files changed, 71 insertions, 0 deletions
diff --git a/app/validators/json_schema_validator.rb b/app/validators/json_schema_validator.rb new file mode 100644 index 00000000000..f8c1727035c --- /dev/null +++ b/app/validators/json_schema_validator.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true +# +# JsonSchemaValidator +# +# Custom validator for json schema. +# Create a json schema within the json_schemas directory +# +# class Project < ActiveRecord::Base +# validates :data, json_schema: { filename: "file" } +# end +# +class JsonSchemaValidator < ActiveModel::EachValidator + FILENAME_ALLOWED = /\A[a-z0-9_-]*\Z/.freeze + FilenameError = Class.new(StandardError) + + def initialize(options) + raise ArgumentError, "Expected 'filename' as an argument" unless options[:filename] + raise FilenameError, "Must be a valid 'filename'" unless options[:filename].match?(FILENAME_ALLOWED) + + super(options) + end + + def validate_each(record, attribute, value) + unless valid_schema?(value) + record.errors.add(attribute, "must be a valid json schema") + end + end + + private + + def valid_schema?(value) + JSON::Validator.validate(schema_path, value) + end + + def schema_path + Rails.root.join('app', 'validators', 'json_schemas', "#{options[:filename]}.json").to_s + end +end diff --git a/app/validators/json_schemas/build_report_result_data.json b/app/validators/json_schemas/build_report_result_data.json new file mode 100644 index 00000000000..0fb4fd6d0b7 --- /dev/null +++ b/app/validators/json_schemas/build_report_result_data.json @@ -0,0 +1,12 @@ +{ + "description": "Build report result data", + "type": "object", + "properties": { + "coverage": { "type": "float" }, + "tests": { + "type": "object", + "items": { "$ref": "./build_report_result_data_tests.json" } + } + }, + "additionalProperties": false +} diff --git a/app/validators/json_schemas/build_report_result_data_tests.json b/app/validators/json_schemas/build_report_result_data_tests.json new file mode 100644 index 00000000000..b38559e727f --- /dev/null +++ b/app/validators/json_schemas/build_report_result_data_tests.json @@ -0,0 +1,13 @@ +{ + "description": "Build report result data tests", + "type": "object", + "properties": { + "name": { "type": "string" }, + "duration": { "type": "string" }, + "failed": { "type": "integer" }, + "errored": { "type": "integer" }, + "skipped": { "type": "integer" }, + "success": { "type": "integer" } + }, + "additionalProperties": false +} diff --git a/app/validators/json_schemas/daily_build_group_report_result_data.json b/app/validators/json_schemas/daily_build_group_report_result_data.json new file mode 100644 index 00000000000..2524ac63050 --- /dev/null +++ b/app/validators/json_schemas/daily_build_group_report_result_data.json @@ -0,0 +1,8 @@ +{ + "description": "Daily build group report result data", + "type": "object", + "properties": { + "coverage": { "type": "float" } + }, + "additionalProperties": false +} |