Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global configuration options #170

Merged
merged 17 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/json_schemer.rb
Expand Up @@ -59,6 +59,7 @@
require 'json_schemer/openapi30/vocab/base'
require 'json_schemer/openapi30/vocab'
require 'json_schemer/openapi'
require 'json_schemer/configuration'
require 'json_schemer/schema'

module JSONSchemer
Expand Down Expand Up @@ -245,6 +246,14 @@ def openapi30_document
def openapi(document, **options)
OpenAPI.new(document, **options)
end

def configuration
@configuration ||= Configuration.new
end

def configure
omkarmoghe marked this conversation as resolved.
Show resolved Hide resolved
yield configuration
end
end

META_SCHEMA_CALLABLES_BY_BASE_URI_STR = {
Expand Down
79 changes: 79 additions & 0 deletions lib/json_schemer/configuration.rb
@@ -0,0 +1,79 @@
# frozen_string_literal: true

module JSONSchemer
class Configuration
module Defaults
davishmcclurg marked this conversation as resolved.
Show resolved Hide resolved
BASE_URI = URI('json-schemer://schema').freeze
FORMATS = {}.freeze
CONTENT_ENCODINGS = {}.freeze
CONTENT_MEDIA_TYPES = {}.freeze
KEYWORDS = {}.freeze
BEFORE_PROPERTY_VALIDATION = [].freeze
AFTER_PROPERTY_VALIDATION = [].freeze
INSERT_PROPERTY_DEFAULTS = false
PROPERTY_RESOLVER = proc do |instance, property, results_with_tree_validity|
unless results_with_tree_validity.size == 1
results_with_tree_validity = results_with_tree_validity.select(&:last)
end
annotations = results_with_tree_validity.to_set { |result, _tree_valid| result.annotation }

if annotations.size == 1
instance[property] = annotations.first.clone
true
else
false
end
end
REF_RESOLVER = proc { |uri| raise UnknownRef, uri.to_s }
REGEXP_RESOLVER = 'ruby'
OUTPUT_FORMAT = 'classic'
RESOLVE_ENUMERATORS = false
ACCESS_MODE = nil
end

attr_accessor(
davishmcclurg marked this conversation as resolved.
Show resolved Hide resolved
:base_uri,
:formats,
:content_encodings,
:content_media_types,
:keywords,
:insert_property_defaults,
:property_default_resolver,
:original_ref_resolver,
:original_regexp_resolver,
:output_format,
:resolve_enumerators,
:access_mode
)

attr_reader(
:before_property_validation,
:after_property_validation,
)

def initialize
davishmcclurg marked this conversation as resolved.
Show resolved Hide resolved
@base_uri = Defaults::BASE_URI
@formats = Defaults::FORMATS
@content_encodings = Defaults::CONTENT_ENCODINGS
@content_media_types = Defaults::CONTENT_MEDIA_TYPES
@keywords = Defaults::KEYWORDS
@before_property_validation = Defaults::BEFORE_PROPERTY_VALIDATION
@after_property_validation = Defaults::AFTER_PROPERTY_VALIDATION
@insert_property_defaults = Defaults::INSERT_PROPERTY_DEFAULTS
@property_default_resolver = Defaults::PROPERTY_RESOLVER
@original_ref_resolver = Defaults::REF_RESOLVER
@original_regexp_resolver = Defaults::REGEXP_RESOLVER
@output_format = Defaults::OUTPUT_FORMAT
@resolve_enumerators = Defaults::RESOLVE_ENUMERATORS
@access_mode = Defaults::ACCESS_MODE
end
end

def before_property_validation=(validations)
@before_property_validation = Array(validations)
end

def after_property_validation=(validations)
@after_property_validation = Array(validations)
end
end
48 changes: 15 additions & 33 deletions lib/json_schemer/schema.rb
Expand Up @@ -18,29 +18,11 @@ def original_instance(instance_location)
UNKNOWN_KEYWORD_CLASS = Draft202012::Vocab::Core::UnknownKeyword
NOT_KEYWORD_CLASS = Draft202012::Vocab::Applicator::Not
PROPERTIES_KEYWORD_CLASS = Draft202012::Vocab::Applicator::Properties
DEFAULT_BASE_URI = URI('json-schemer://schema').freeze
DEFAULT_FORMATS = {}.freeze
DEFAULT_CONTENT_ENCODINGS = {}.freeze
DEFAULT_CONTENT_MEDIA_TYPES = {}.freeze
DEFAULT_KEYWORDS = {}.freeze
DEFAULT_BEFORE_PROPERTY_VALIDATION = [].freeze
DEFAULT_AFTER_PROPERTY_VALIDATION = [].freeze
DEFAULT_REF_RESOLVER = proc { |uri| raise UnknownRef, uri.to_s }

NET_HTTP_REF_RESOLVER = proc { |uri| JSON.parse(Net::HTTP.get(uri)) }
RUBY_REGEXP_RESOLVER = proc { |pattern| Regexp.new(pattern) }
ECMA_REGEXP_RESOLVER = proc { |pattern| Regexp.new(EcmaRegexp.ruby_equivalent(pattern)) }

DEFAULT_PROPERTY_DEFAULT_RESOLVER = proc do |instance, property, results_with_tree_validity|
results_with_tree_validity = results_with_tree_validity.select(&:last) unless results_with_tree_validity.size == 1
annotations = results_with_tree_validity.to_set { |result, _tree_valid| result.annotation }
if annotations.size == 1
instance[property] = annotations.first.clone
true
else
false
end
end

attr_accessor :base_uri, :meta_schema, :keywords, :keyword_order
attr_reader :value, :parent, :root, :parsed
attr_reader :vocabulary, :format, :formats, :content_encodings, :content_media_types, :custom_keywords, :before_property_validation, :after_property_validation, :insert_property_defaults, :property_default_resolver
Expand All @@ -50,23 +32,23 @@ def initialize(
parent = nil,
root = self,
keyword = nil,
base_uri: DEFAULT_BASE_URI,
base_uri: JSONSchemer.configuration.base_uri,
meta_schema: nil,
vocabulary: nil,
format: true,
davishmcclurg marked this conversation as resolved.
Show resolved Hide resolved
formats: DEFAULT_FORMATS,
content_encodings: DEFAULT_CONTENT_ENCODINGS,
content_media_types: DEFAULT_CONTENT_MEDIA_TYPES,
keywords: DEFAULT_KEYWORDS,
before_property_validation: DEFAULT_BEFORE_PROPERTY_VALIDATION,
after_property_validation: DEFAULT_AFTER_PROPERTY_VALIDATION,
insert_property_defaults: false,
property_default_resolver: DEFAULT_PROPERTY_DEFAULT_RESOLVER,
ref_resolver: DEFAULT_REF_RESOLVER,
regexp_resolver: 'ruby',
output_format: 'classic',
resolve_enumerators: false,
access_mode: nil
formats: JSONSchemer.configuration.formats,
content_encodings: JSONSchemer.configuration.content_encodings,
content_media_types: JSONSchemer.configuration.content_media_types,
keywords: JSONSchemer.configuration.keywords,
before_property_validation: JSONSchemer.configuration.before_property_validation,
after_property_validation: JSONSchemer.configuration.after_property_validation,
insert_property_defaults: JSONSchemer.configuration.insert_property_defaults,
property_default_resolver: JSONSchemer.configuration.property_default_resolver,
ref_resolver: JSONSchemer.configuration.original_ref_resolver,
regexp_resolver: JSONSchemer.configuration.original_regexp_resolver,
output_format: JSONSchemer.configuration.output_format,
resolve_enumerators: JSONSchemer.configuration.resolve_enumerators,
access_mode: JSONSchemer.configuration.access_mode
)
@value = deep_stringify_keys(value)
@parent = parent
Expand Down
5 changes: 4 additions & 1 deletion test/json_schemer_test.rb
Expand Up @@ -267,7 +267,10 @@ def test_default_meta_schema
end

def test_draft4_default_id
assert_equal(JSONSchemer::Schema::DEFAULT_BASE_URI, JSONSchemer.schema(true, :meta_schema => JSONSchemer::Draft4::BASE_URI.to_s).base_uri)
assert_equal(
JSONSchemer::Configuration::Defaults::BASE_URI,
JSONSchemer.schema(true, :meta_schema => JSONSchemer::Draft4::BASE_URI.to_s).base_uri
)
end

def test_it_ignores_content_schema_without_content_media_type
Expand Down