lib/cocina/generator/schema_base.rb in cocina-models-0.58.2 vs lib/cocina/generator/schema_base.rb in cocina-models-0.59.0
- old
+ new
@@ -2,18 +2,19 @@
module Cocina
module Generator
# Base class for generating from openapi
class SchemaBase
- attr_reader :schema_doc, :key, :required, :nullable, :parent
+ attr_reader :schema_doc, :key, :required, :nullable, :parent, :relaxed
- def initialize(schema_doc, key: nil, required: false, nullable: false, parent: nil)
+ def initialize(schema_doc, key: nil, required: false, nullable: false, parent: nil, relaxed: false)
@schema_doc = schema_doc
@key = key
@required = required
@nullable = nullable
@parent = parent
+ @relaxed = relaxed
end
def filename
"#{name.underscore}.rb"
end
@@ -23,19 +24,19 @@
end
# Allows non-required values to not be provided. This allows smaller
# requests as not every field needs to be present.
def omittable
- return '' if required
+ return '' if required && !relaxed
'.meta(omittable: true)'
end
# Allows nillable values to be set to nil. This is useful when doing
# an update and you want to clear out a value.
def optional
- nullable ? '.optional' : ''
+ nullable || relaxed ? '.optional' : ''
end
def quote(item)
return item unless schema_doc.type == 'string'
@@ -52,24 +53,34 @@
return '' unless schema_doc.example
"# example: #{schema_doc.example}\n"
end
+ def relaxed_comment
+ return '' unless relaxed
+
+ "# Validation of this property is relaxed. See the openapi for full validation.\n"
+ end
+
def dry_datatype(doc)
case doc.type
when 'integer'
'Strict::Integer'
when 'string'
string_dry_datatype(doc)
when 'boolean'
'Strict::Bool'
else
- if doc.one_of&.map(&:type).all? { |o| %w[integer string].include?(o) }
+ if any_datatype?(doc)
'Nominal::Any'
else
raise "#{schema_doc.type} not supported"
end
end
+ end
+
+ def any_datatype?(doc)
+ relaxed || doc.one_of&.map(&:type).all? { |o| %w[integer string].include?(o) }
end
def string_dry_datatype(doc)
case doc.format
when 'date-time'