prepareSeoActionForm function

SeoActionFormPlan? prepareSeoActionForm(
  1. SeoActionFormDefinition definition
)

Implementation

SeoActionFormPlan? prepareSeoActionForm(SeoActionFormDefinition definition) {
  final actionId = definition.actionId.trim();
  final returnPath = _canonicalReturnPath(definition.returnPath);
  if (!isValidSeoInteractionId(actionId) ||
      returnPath == null ||
      definition.fields.isEmpty ||
      definition.fields.length > seoActionFormMaxFields ||
      definition.headingLevel < 1 ||
      definition.headingLevel > 6) {
    return null;
  }

  final heading =
      _canonicalText(definition.heading, seoActionFormMaxLabelLength);
  final description =
      _canonicalText(definition.description, seoActionFormMaxDescriptionLength);
  final submitLabel =
      _canonicalText(definition.submitLabel, seoActionFormMaxLabelLength);
  final pendingLabel =
      _canonicalText(definition.pendingLabel, seoActionFormMaxLabelLength);
  final failureLabel =
      _canonicalText(definition.failureLabel, seoActionFormMaxLabelLength);
  final statusLabel =
      _canonicalText(definition.statusLabel, seoActionFormMaxLabelLength);
  final messages = _canonicalMessages(definition.messages);
  final contextToken = definition.contextToken == null
      ? null
      : _canonicalContextToken(definition.contextToken!);
  if (heading == null ||
      description == null ||
      submitLabel == null ||
      pendingLabel == null ||
      failureLabel == null ||
      statusLabel == null ||
      messages == null ||
      (definition.contextToken != null && contextToken == null)) {
    return null;
  }

  final names = <String>{};
  final fields = <SeoActionFormPlanField>[];
  var aggregate = 0;
  var initialAggregate = 0;
  var imageFields = 0;
  for (final raw in definition.fields) {
    final name = raw.name.trim();
    final label = _canonicalText(raw.label, seoActionFormMaxLabelLength);
    final fieldDescription = raw.description == null
        ? null
        : _canonicalText(
            raw.description!,
            seoActionFormMaxDescriptionLength,
          );
    final maxLength = raw.maxLength ?? _defaultMaxLength(raw.kind);
    final choicePrompt = raw.choicePrompt == null
        ? null
        : _canonicalText(raw.choicePrompt!, seoActionFormMaxLabelLength);
    final options = <SeoActionFormPlanOption>[];
    final optionValues = <String>{};
    for (final option in raw.options) {
      final value = option.value.trim();
      final optionLabel =
          _canonicalText(option.label, seoActionFormMaxLabelLength);
      if (!_optionValue.hasMatch(value) ||
          !optionValues.add(value) ||
          optionLabel == null) {
        return null;
      }
      options.add(SeoActionFormPlanOption(value: value, label: optionLabel));
    }
    if (raw.kind == SeoActionFormFieldKind.image && ++imageFields > 1) {
      return null;
    }
    if (!_fieldName.hasMatch(name) ||
        !names.add(name) ||
        label == null ||
        (raw.description != null && fieldDescription == null) ||
        !_validChoice(raw.kind, choicePrompt, options) ||
        !_validFieldBounds(raw.kind, raw.minLength, maxLength) ||
        !_validAutocomplete(raw.kind, raw.autocomplete)) {
      return null;
    }
    final initialValue = _canonicalInitialValue(
      raw.initialValue,
      kind: raw.kind,
      minLength: raw.minLength,
      maxLength: maxLength,
      options: options,
    );
    if ((raw.initialValue != null && initialValue == null) ||
        (raw.kind != SeoActionFormFieldKind.consent && raw.initialConsent) ||
        (raw.kind == SeoActionFormFieldKind.consent &&
            raw.initialValue != null) ||
        (raw.kind == SeoActionFormFieldKind.image &&
            (raw.initialValue != null || raw.initialConsent))) {
      return null;
    }
    initialAggregate += initialValue?.length ?? 0;
    if (initialAggregate > seoActionFormMaxAggregateInputLength) return null;
    aggregate += name.length +
        label.length +
        (fieldDescription?.length ?? 0) +
        (choicePrompt?.length ?? 0) +
        options.fold<int>(
          0,
          (sum, option) => sum + option.value.length + option.label.length,
        );
    if (aggregate > seoActionFormMaxAggregateResultLength) return null;
    fields.add(SeoActionFormPlanField(
      name: name,
      label: label,
      kind: raw.kind,
      description: fieldDescription,
      required: raw.required,
      minLength: raw.minLength,
      maxLength: maxLength,
      autocomplete: raw.autocomplete,
      choicePrompt: choicePrompt,
      options: List.unmodifiable(options),
      initialValue: initialValue,
      initialConsent: raw.initialConsent,
    ));
  }

  return SeoActionFormPlan._(
    actionId: actionId,
    returnPath: returnPath,
    heading: heading,
    description: description,
    submitLabel: submitLabel,
    pendingLabel: pendingLabel,
    failureLabel: failureLabel,
    statusLabel: statusLabel,
    fields: List.unmodifiable(fields),
    headingLevel: definition.headingLevel,
    messages: messages,
    contextToken: contextToken,
  );
}