Skip to main content
Version: 3.0 Beta

Input Validation

This document describes the attributes and functions available for input validation in ZModel. See Input Validation for more details on how to use them.

Model-Level Attributes​

  • @@validate

    @@validate(_ value: Boolean, _ message: String?, _ path: String[]?)

    Defines a model-level validation rule that can involve multiple fields and logical combinations. Use the message parameter to provide an optional custom error message, and the path parameter to provide an optional path to the field that caused the error.

Field-Level Attributes​

All field-level attributes have a message parameter that allows you to provide a custom error message.

  • String & List Fields

    • @length

      @length(_ min: Int?, _ max: Int?, _ message: String?)

      Validates the length of a string or list field.

  • String Fields

    • @startsWith

      @startsWith(_ text: String, _ message: String?)

      Requires a string field to start with a given prefix.

    • @endsWith

      @endsWith(_ text: String, _ message: String?)

      Requires a string field to end with a given suffix.

    • @contains

      @contains(_text: String, _ message: String?)

      Requires a string field to contain a given substring.

    • @email

      @email(_ message: String?)

      Requires a string field to be a valid email address.

    • @url

      @url(_ message: String?)

      Requires a string field to be a valid URL.

    • @datetime

      @datetime(_ message: String?)

      Requires a string field to be a valid ISO 8601 datetime.

    • @regex

      @regex(_ regex: String, _ message: String?)

      Requires a string field to match a given regular expression.

    • @lower

      @lower(_ value: String)

      Transforms a string field to lowercase before saving to the database.

    • @upper

      @upper(_ value: String)

      Transforms a string field to uppercase before saving to the database.

    • @trim

      @trim(_ value: String)

      Trims whitespace from both ends of a string field before saving to the database.

  • Number Fields

    • @lt

      @lt(_ value: Any, _ message: String?)

      Requires a number field to be less than a given value.

    • @lte

      @lte(_ value: Any, _ message: String?)

      Requires a number field to be less than or equal to a given value.

    • @gt

      @gt(_ value: Any, _ message: String?)

      Requires a number field to be greater than a given value.

    • @gte

      @gte(_ value: Any, _ message: String?)

      Requires a number field to be greater than or equal to a given value.

Functions​

  • now()

    function now(): DateTime {}

    Returns the current datetime.

  • length()

    function length(field: Any): Int {}

    Returns the length of a string or list field.

  • startsWith()

    function startsWith(field: String, search: String): Boolean {}

    Checks if a string field starts with a given prefix.

  • endsWith()

    function endsWith(field: String, search: String): Boolean {}

    Checks if a string field ends with a given suffix.

  • contains()

    function contains(field: String, search: String): Boolean {}

    Checks if a string field contains a given substring.

  • isEmail()

    function isEmail(field: String): Boolean {}

    Checks if a string field is a valid email address.

  • isUrl()

    function isUrl(field: String): Boolean {}

    Checks if a string field is a valid URL.

  • isDateTime()

    function isDateTime(field: String): Boolean {}

    Checks if a string field is a valid ISO 8601 datetime.

  • regex()

    function regex(field: String, pattern: String): Boolean {}

    Checks if a string field matches a given regular expression.

  • has()

    function has(field: Any[], search: Any): Boolean {}

    Checks if a list field contains a given element.

  • hasSome()

    function hasSome(field: Any[], search: Any[]): Boolean {}

    Checks if a list field contains at least one element from a given list.

  • hasEvery()

    function hasEvery(field: Any[], search: Any[]): Boolean {}

    Checks if a list field contains all elements from a given list.

  • isEmpty()

    function isEmpty(field: Any[]): Boolean {}

    Checks if a list field is empty.

Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below