diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-30 00:08:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-30 00:08:35 +0300 |
commit | d8b32df644a632b143d6b9967311301a2fc83a6b (patch) | |
tree | 130722547715d1f65104529d8a09b1ba123776d6 /app/validators | |
parent | b64a8161c9442d82897a341d6bf935dd3e748b06 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/validators')
-rw-r--r-- | app/validators/json_schema_validator.rb | 36 | ||||
-rw-r--r-- | app/validators/json_schemas/build_report_result_data.json | 12 | ||||
-rw-r--r-- | app/validators/json_schemas/build_report_result_data_junit.json | 13 |
3 files changed, 61 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..89dd660bc16 --- /dev/null +++ b/app/validators/json_schema_validator.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require "json-schema" + +# 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 + def initialize(options) + raise ArgumentError, "Expected 'filename' as an argument" unless options[:filename] + + 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..b364266b9d6 --- /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" }, + "junit": { + "type": "object", + "items": { "$ref": "./build_report_result_data_junit.json" } + } + }, + "additionalProperties": false +} diff --git a/app/validators/json_schemas/build_report_result_data_junit.json b/app/validators/json_schemas/build_report_result_data_junit.json new file mode 100644 index 00000000000..f69cbd4f16d --- /dev/null +++ b/app/validators/json_schemas/build_report_result_data_junit.json @@ -0,0 +1,13 @@ +{ + "description": "Build report result data junit", + "type": "object", + "properties": { + "name": { "type": "string" }, + "duration": { "type": "string" }, + "failed": { "type": "integer" }, + "errored": { "type": "integer" }, + "skipped": { "type": "integer" }, + "success": { "type": "integer" } + }, + "additionalProperties": false +} |