fromSchemaMap static method

String fromSchemaMap(
  1. Map<String, dynamic> schema
)

Converts a schema map (from _introspectType) to a Schema.* expression.

Implementation

static String fromSchemaMap(Map<String, dynamic> schema) {
  final type = schema['type'] as String?;
  switch (type) {
    case 'string':
      return 'Schema.string()';
    case 'integer':
      return 'Schema.int()';
    case 'number':
      return 'Schema.number()';
    case 'boolean':
      return 'Schema.bool()';
    case 'array':
      final items = schema['items'] as Map<String, dynamic>?;
      if (items != null) {
        return 'Schema.list(items: ${fromSchemaMap(items)})';
      }
      return 'Schema.list()';
    case 'object':
      final props = schema['properties'] as Map<String, dynamic>?;
      if (props == null || props.isEmpty) {
        return 'Schema.object()';
      }
      final propEntries = props.entries
          .map((e) {
            return "'${e.key}': ${fromSchemaMap(e.value as Map<String, dynamic>)}";
          })
          .join(',\n      ');
      final required = schema['required'] as List?;
      if (required != null && required.isNotEmpty) {
        final reqStr = required.map((r) => "'$r'").join(', ');
        return 'Schema.object(\n    properties: {\n      $propEntries,\n    },\n    required: [$reqStr],\n  )';
      }
      return 'Schema.object(\n    properties: {\n      $propEntries,\n    },\n  )';
    default:
      return 'Schema.string()';
  }
}