# Validator functions for {{ feature_name }} domain
# Type-specific validation rules for {{ feature_description | default(value="domain feature") }}
# Auto-generated for project: {{ project_name }}
{%- if imports %}
{%- for import in imports %}
let {{ import.name }} = import "{{ import.path }}"
{%- endfor %}

{% endif %}
{
{%- for field in fields %}

  # Validator for {{ field.name }}
{%- if field.doc %} - {{ field.doc }}{%- endif %}
  validate_{{ field.name }} = fun value => (
{%- if field.type == "Text" %}
    (std.is_string value) &&
    (std.string.length value > 0)
{%- if field.min_length %} &&
    (std.string.length value >= {{ field.min_length }})
{%- endif %}
{%- if field.max_length %} &&
    (std.string.length value <= {{ field.max_length }})
{%- endif %}
{%- else if field.type == "Number" %}
    (std.is_number value)
{%- if field.min %} &&
    (value >= {{ field.min }})
{%- endif %}
{%- if field.max %} &&
    (value <= {{ field.max }})
{%- endif %}
{%- else if field.type == "Confirm" %}
    (std.is_bool value)
{%- else if field.type == "Password" %}
    (std.is_string value) &&
    (std.string.length value > 0)
{%- if field.min_length %} &&
    (std.string.length value >= {{ field.min_length }})
{%- endif %}
{%- else if field.type == "Select" or field.type == "MultiSelect" %}
    (std.is_string value) &&
    (std.array.contains {{ field.options | tojson }} value)
{%- else if field.type == "RepeatingGroup" %}
    (std.is_array value)
{%- if field.min_items %} &&
    (std.array.length value >= {{ field.min_items }})
{%- endif %}
{%- if field.max_items %} &&
    (std.array.length value <= {{ field.max_items }})
{%- endif %}
{%- else %}
    true
{%- endif %}
  ),
{%- endfor %}
}
