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, #read_only!, #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


36
37
38
39
40
# File 'lib/pdk/config/json_schema_namespace.rb', line 36

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)


52
53
54
# File 'lib/pdk/config/json_schema_namespace.rb', line 52

def empty_schema?
  document_schema.schema.empty?
end

#schemaHash

The JSON Schema for the namespace

Returns:

  • (Hash)


45
46
47
# File 'lib/pdk/config/json_schema_namespace.rb', line 45

def schema
  document_schema.schema
end

#schema_property_namesString[]

Name of all the top level properties for the schema

Returns:

  • (String[])


59
60
61
62
63
# File 'lib/pdk/config/json_schema_namespace.rb', line 59

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


68
69
70
71
72
73
74
75
# File 'lib/pdk/config/json_schema_namespace.rb', line 68

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)


80
81
82
# File 'lib/pdk/config/json_schema_namespace.rb', line 80

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