Class: PDK::Config::JSONSchemaNamespace

Inherits:
Namespace
  • Object
show all
Defined in:
lib/pdk/config/json_schema_namespace.rb

Direct Known Subclasses

JSONWithSchema, YAMLWithSchema

Instance Attribute Summary

Attributes inherited from Namespace

#file, #name, #parent

Instance Method Summary collapse

Methods inherited from Namespace

#[], #[]=, #child_namespace?, #fetch, #include_in_parent?, #mount, #namespace, #resolve, #setting

Constructor Details

#initialize(name = nil, file: nil, parent: nil, persistent_defaults: false, schema_file: nil, &block) ⇒ JSONSchemaNamespace

Initialises the PDK::Config::JSONSchemaNamespace object.

Parameters:

  • params (Hash)

    a customizable set of options

See Also:

  • Namespace.initialize


34
35
36
37
38
# File 'lib/pdk/config/json_schema_namespace.rb', line 34

def initialize(name = nil, file: nil, parent: nil, persistent_defaults: false, schema_file: nil, &block)
  super(name, file: file, parent: parent, persistent_defaults: persistent_defaults, &block)
  @schema_file = schema_file
  @unmanaged_settings = {}
end

Instance Method Details

#empty_schema?Boolean

Whether the schema is valid but empty.

Returns:

  • (Boolean)


50
51
52
# File 'lib/pdk/config/json_schema_namespace.rb', line 50

def empty_schema?
  document_schema.schema.empty?
end

#schemaHash

The JSON Schema for the namespace

Returns:

  • (Hash)


43
44
45
# File 'lib/pdk/config/json_schema_namespace.rb', line 43

def schema
  document_schema.schema
end

#schema_property_namesString[]

Name of all the top level properties for the schema

Returns:

  • (String[])


57
58
59
60
# File 'lib/pdk/config/json_schema_namespace.rb', line 57

def schema_property_names
  return [] if schema['properties'].nil?
  schema['properties'].keys
end

#to_hObject

Extends the to_h namespace method to include unmanaged settings

See Also:

  • Namespace.to_h


65
66
67
68
69
70
71
72
# File 'lib/pdk/config/json_schema_namespace.rb', line 65

def to_h
  # This may seem counter-intuitive but we need to call super first as the settings
  # may not have been loaded yet, which means @unmanaged_settings will be empty.
  # We call super first to force any file loading and then merge the unmanaged settings
  settings_hash = super
  @unmanaged_settings = {} if @unmanaged_settings.nil?
  @unmanaged_settings.merge(settings_hash)
end

#validate_document!(document) ⇒ Boolean

Validates a document (Hash table) against the schema

Returns:

  • (Boolean)


77
78
79
# File 'lib/pdk/config/json_schema_namespace.rb', line 77

def validate_document!(document)
  ::JSON::Validator.validate!(schema, document)
end