Class: PDK::Config::JSONSchemaSetting
- Defined in:
- lib/pdk/config/json_schema_setting.rb
Instance Attribute Summary
Attributes inherited from Setting
Instance Method Summary collapse
-
#default ⇒ Object?
Evaluate the default setting, firstly from the JSON schema and then from any other default evaluators in the settings chain.
-
#initialize(_name, namespace, _initial_value) ⇒ JSONSchemaSetting
constructor
Initialises the PDK::Config::JSONSchemaSetting object.
-
#validate!(value) ⇒ Object
Verifies that the new setting value is valid by calling the JSON schema validator on a hash which includes the new setting.
Methods inherited from Setting
#default_to, #qualified_name, #to_s, #validate, #value, #value=
Constructor Details
#initialize(_name, namespace, _initial_value) ⇒ JSONSchemaSetting
Initialises the PDK::Config::JSONSchemaSetting object.
9 10 11 12 13 |
# File 'lib/pdk/config/json_schema_setting.rb', line 9 def initialize(_name, namespace, _initial_value) raise 'The JSONSchemaSetting object can only be created within the JSONSchemaNamespace' unless namespace.is_a?(PDK::Config::JSONSchemaNamespace) super end |
Instance Method Details
#default ⇒ Object?
Evaluate the default setting, firstly from the JSON schema and then from any other default evaluators in the settings chain.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pdk/config/json_schema_setting.rb', line 39 def default # Return the default from the schema document if it exists if namespace.schema_property_names.include?(@name) prop_schema = namespace.schema['properties'][@name] return prop_schema['default'] unless prop_schema['default'].nil? end # ... otherwise call the settings chain default # and if that doesn't exist, just return nil @previous_setting&.default end |
#validate!(value) ⇒ Object
Verifies that the new setting value is valid by calling the JSON schema validator on a hash which includes the new setting
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pdk/config/json_schema_setting.rb', line 19 def validate!(value) # Get the existing namespace data new_document = namespace.to_h # ... set the new value new_document[@name] = value begin # ... add validate it namespace.validate_document!(new_document) rescue ::JSON::Schema::ValidationError => e raise ArgumentError, format('%{key} %{message}', key: qualified_name, message: e.) end end |