Frontmatter.fromMap constructor

Frontmatter.fromMap(
  1. Map<String, Object?> value, {
  2. bool allowUnknownFields = true,
})

Implementation

factory Frontmatter.fromMap(
  Map<String, Object?> value, {
  bool allowUnknownFields = true,
}) {
  final Set<String> unknown = value.keys
      .where((String key) => !_allowedFrontmatterKeys.contains(key))
      .toSet();
  if (!allowUnknownFields && unknown.isNotEmpty) {
    throw ArgumentError(
      'Unknown frontmatter fields: ${unknown.toList()..sort()}',
    );
  }

  final Object? nameValue = value['name'];
  final Object? descriptionValue = value['description'];
  if (nameValue is! String) {
    throw ArgumentError('name is required and must be a string');
  }
  if (descriptionValue is! String) {
    throw ArgumentError('description is required and must be a string');
  }

  final String? allowedTools =
      _readOptionalString(value['allowed_tools']) ??
      _readOptionalString(value['allowed-tools']);

  return Frontmatter(
    name: nameValue,
    description: descriptionValue,
    license: _readOptionalString(value['license']),
    compatibility: _readOptionalString(value['compatibility']),
    allowedTools: allowedTools,
    metadata: _readMetadata(value['metadata']),
    extraFields: allowUnknownFields
        ? <String, Object?>{for (final String key in unknown) key: value[key]}
        : <String, Object?>{},
  );
}