# Custom validators for {{ feature_name }}
#
# Feature-specific validation logic.

let common = import "./common.ncl" in

{
{% for validator in validators %}
  # {{ validator.description | default(value="Custom validator for " ~ validator.name) }}
  {{ validator.name }} = {% if validator.validator_type == "Range" %}common.Range {{ validator.min | default(value="0") }} {{ validator.max | default(value="100") }} "{{ validator.name }}"
{% elif validator.validator_type == "Pattern" %}common.MatchesPattern "{{ validator.pattern | default(value=".*") }}" "{{ validator.name }}"
{% elif validator.validator_type == "ArrayUniqueness" %}fun label =>
    std.contract.from_predicate (fun arr =>
      std.is_array arr &&
      let values = std.array.map (fun item => item.{{ validator.unique_field | default(value="id") }}) arr in
      std.array.length values == std.array.length (std.array.sort values)
    ) {
      label = label,
      message = "array items must have unique {{ validator.unique_field | default(value="id") }} values",
    }
{% elif validator.validator_type == "Composite" %}fun label =>
    std.contract.from_predicate (fun value =>
      # Composite validation logic
      {% for field in validator.applies_to %}
      {{ field }}_valid value &&
      {% endfor %}
      true
    ) {
      label = label,
      message = "composite validation failed for {{ validator.name }}",
    }
{% else %}fun label =>
    std.contract.from_predicate (fun value =>
      # Custom predicate logic
      true  # TODO: Implement custom validation
    ) {
      label = label,
      message = "{{ validator.description | default(value="validation failed") }}",
    }
{% endif %},

{% endfor %}
  # Master validation function for {{ feature_name }}
  validate = fun config =>
    config
    # Apply all validators here
{% for validator in validators %}
    # | {{ validator.name }} "{{ feature_name }}.{{ validator.name }}"
{% endfor %}
    ,
}
