Class: Meta::JsonSchema::ObjectSchema

Inherits:
BaseSchema
  • Object
show all
Defined in:
lib/meta/json_schema/schemas/object_schema.rb

Constant Summary collapse

USER_OPTIONS_CHECKER =
Utils::KeywordArgs::Builder.build do
  permit_extras true

  key :scope, normalizer: ->(value) {
    raise ArgumentError, 'scope 选项不可传递 nil' if value.nil?
    value = [value] unless value.is_a?(Array)
    value
  }
end

Constants inherited from BaseSchema

BaseSchema::OPTIONS_CHECKER

Instance Attribute Summary collapse

Attributes inherited from BaseSchema

#options

Instance Method Summary collapse

Methods inherited from BaseSchema

#filter?, #find_schema, #if?, #scoped, #staged, #to_schema, #value?

Constructor Details

#initialize(properties: nil, options: {}, locked_options: {}, schema_name_resolver: proc { nil }) ⇒ ObjectSchema

Returns a new instance of ObjectSchema.



20
21
22
23
24
25
26
27
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 20

def initialize(properties: nil, options: {}, locked_options: {}, schema_name_resolver: proc { nil })
  super(options)

  @properties = properties || Properties.new({}) # property 包含 stage,stage 包含 scope、schema
  @properties = Properties.new(@properties) if @properties.is_a?(Hash)
  @locked_options = USER_OPTIONS_CHECKER.check(locked_options || {})
  @schema_name_resolver = schema_name_resolver || proc { nil }
end

Instance Attribute Details

#locked_optionsObject (readonly)

Returns the value of attribute locked_options.



8
9
10
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 8

def locked_options
  @locked_options
end

#propertiesObject (readonly)

Returns the value of attribute properties.



8
9
10
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 8

def properties
  @properties
end

Instance Method Details

#dup(options) ⇒ Object

复制一个新的 ObjectSchema,只有 options 不同



30
31
32
33
34
35
36
37
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 30

def dup(options)
  self.class.new(
    properties: properties,
    options: options,
    locked_options: locked_options,
    schema_name_resolver: @schema_name_resolver
  )
end

#filter(object_value, user_options = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 39

def filter(object_value, user_options = {})
  # 合并 user_options
  user_options = USER_OPTIONS_CHECKER.check(user_options)
  user_options = merge_user_options(user_options, locked_options) if locked_options
  super
end

#locked_excludeObject



73
74
75
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 73

def locked_exclude
  locked_options && locked_options[:exclude]
end

#locked_scopeObject



69
70
71
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 69

def locked_scope
  locked_options && locked_options[:scope]
end

#merge_other_properties(properties) ⇒ Object

合并其他的属性,并返回一个新的 ObjectSchema



47
48
49
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 47

def merge_other_properties(properties)
  ObjectSchema.new(properties: self.properties.merge(properties))
end

#resolve_name(stage) ⇒ Object



51
52
53
54
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 51

def resolve_name(stage)
  locked_scopes = (locked_options || {})[:scope] || []
  @schema_name_resolver.call(stage, locked_scopes)
end

#to_schema_doc(user_options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/meta/json_schema/schemas/object_schema.rb', line 56

def to_schema_doc(user_options = {})
  user_options = USER_OPTIONS_CHECKER.check(user_options)
  user_options = merge_user_options(user_options, locked_options) if locked_options

  schema = { type: 'object' }
  schema[:description] = options[:description] if options[:description]

  properties, required_keys = @properties.to_swagger_doc(**user_options)
  schema[:properties] = properties unless properties.empty?
  schema[:required] = required_keys unless required_keys.empty?
  schema
end