lib/cocina/generator/schema_base.rb in cocina-models-0.87.0 vs lib/cocina/generator/schema_base.rb in cocina-models-0.87.1

- old
+ new

@@ -22,14 +22,20 @@ def name key || schema_doc.name end - # Allows nillable values to be set to nil. This is useful when doing - # an update and you want to clear out a value. + # Allows nullable values to be set to nil. This is useful when doing an + # update and you want to clear out a value. The logic also permits custom + # types (e.g., `Barcode`, `SourceId`) to be nullable if they are not + # required. def optional - nullable || relaxed ? '.optional' : '' + return '.optional' if nullable || + relaxed || + (custom_type? && !required) + + '' end def quote(item) return item unless schema_doc.type == 'string' @@ -56,9 +62,14 @@ def relaxed_comment return '' unless relaxed "# Validation of this property is relaxed. See the openapi for full validation.\n" + end + + # dry-types-based types contain the word `Types` (e.g., `Types::String`), and custom types (e.g., `SourceId`) do not + def custom_type? + !dry_datatype(schema_doc).match?('Types') end def dry_datatype(doc) return doc.name if doc.name.present? && schemas.include?(doc.name)