Class: Udb::TopLevelDatabaseObject

Inherits:
DatabaseObject show all
Extended by:
T::Sig
Defined in:
lib/udb/obj/database_obj.rb,
lib/udb/condition.rb

Overview

base class for any object defined in its own YAML file

expected to contain at least:

$schema:
kind:
name:

Defined Under Namespace

Classes: SchemaError, SchemaValidationError, ValidationError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, data_path, arch)

Parameters:

  • data (Hash{String => T.untyped})

    Hash with fields to be added

  • data_path (String, Pathname)

    Path to the data file

  • arch (ConfiguredArchitecture)


445
446
447
# File 'lib/udb/obj/database_obj.rb', line 445

def initialize(data, data_path, arch)
  super(data, data_path, arch, DatabaseObject::Kind.deserialize(T.must_because(data["kind"]) { pp data }))
end

Class Method Details

.create_json_schemer_resolver(udb_resolver) ⇒ T.proc.params(pattern: Regexp).returns(T.untyped)

Parameters:

Returns:

  • (T.proc.params(pattern: Regexp).returns(T.untyped))


384
385
386
387
388
389
390
391
392
393
394
# File 'lib/udb/obj/database_obj.rb', line 384

def self.create_json_schemer_resolver(udb_resolver)
  proc do |pattern|
    if pattern.to_s =~ /^http/
      JSON.parse(T.must(Net::HTTP.get(pattern)))
    elsif pattern.to_s =~ /^json-schemer:\/\/schema/
      JSON.load_file("#{udb_resolver.schemas_path}#{URI(pattern.to_s).path}")
    else
      JSON.load_file(udb_resolver.schemas_path / pattern.to_s)
    end
  end
end

Instance Method Details

#key?(k) ⇒ Boolean

Parameters:

  • k (String)

Returns:

  • (Boolean)


456
# File 'lib/udb/obj/database_obj.rb', line 456

def key?(k) = @data.key?(k)

#keysArray<String>

Returns List of keys added by this DatabaseObject.

Returns:

  • (Array<String>)

    List of keys added by this DatabaseObject



451
# File 'lib/udb/obj/database_obj.rb', line 451

def keys = @data.keys

#validate(resolver)

This method returns an undefined value.

validate the data against it’s schema

Parameters:

Raises:



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/udb/obj/database_obj.rb', line 399

def validate(resolver)
  @@schemas[resolver] ||= {}
  schemas = @@schemas[resolver]
  ref_resolver = TopLevelDatabaseObject.create_json_schemer_resolver(resolver)

  if @data.key?("$schema")
    schema_path = data["$schema"]
    schema_file, obj_path = schema_path.split("#")
    schema =
      if schemas.key?(schema_file)
        schemas[schema_file]
      else
        schemas[schema_file] = JSONSchemer.schema(
          File.read("#{resolver.schemas_path}/#{schema_file}"),
          regexp_resolver: "ecma",
          ref_resolver:,
          insert_property_defaults: true
        )
        raise SchemaError, T.must(schemas[schema_file]).validate_schema unless T.must(schemas[schema_file]).valid_schema?

        schemas[schema_file]
      end

    unless obj_path.nil?
      obj_path_parts = obj_path.split("/")[1..]

      obj_path_parts.each do |k|
        schema = schema.fetch(k)
      end
    end

    # convert through JSON to handle anything supported in YAML but not JSON
    # (e.g., integer object keys will be converted to strings)
    jsonified_obj = JSON.parse(JSON.generate(@data))

    raise "Nothing there?" if jsonified_obj.nil?

    raise SchemaValidationError.new(@data_path, schema.validate(jsonified_obj)) unless schema.valid?(jsonified_obj)
  else
    Udb.logger.warn "No $schema for #{@data_path}"
  end
end