Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active October 29, 2024 19:38
Show Gist options
  • Save joepie91/cd107b3a566264b28a3494689d73e589 to your computer and use it in GitHub Desktop.
Save joepie91/cd107b3a566264b28a3494689d73e589 to your computer and use it in GitHub Desktop.
Quick reference for `checkit` validators

Presence

  • exists - The field must exist, and not be undefined.
  • required - The field must exist, and not be undefined, null or an empty string.
  • empty - The field must be some kind of "empty". Things that are considered "empty" are as follows:
    • "" (empty string)
    • [] (empty array)
    • {} (empty object)
    • Other falsey values

Character set

  • alpha - a-z, A-Z
  • alphaNumeric - a-z, A-Z, 0-9
  • alphaUnderscore - a-z, A-Z, 0-9, _
  • alphaDash - a-z, A-Z, 0-9, _, -

Value

Length-related validators may apply to both strings and arrays.

  • exactLength:length- The value must have a length of exactly length.
  • minLength:length - The value must have a length of at least length.
  • maxLength:length - The value must have a length of at most length.
  • contains:needle - The value must contain the specified needle (applies to both strings and arrays).
  • accepted - Must be a value that indicates agreement - varies by language (defaulting to en):
    • en, fr, nl - "yes", "on", "1", 1, "true", true
    • es - "yes", "on", "1", 1, "true", true, "si"
    • ru - "yes", "on", "1", 1, "true", true, "да"

Value (numbers)

Note that "numbers" refers to both Number-type values, and strings containing numeric values!

  • numeric - Must be a finite numeric value of some sort.
  • integer - Must be an integer value (either positive or negative).
  • natural - Must be a natural number (ie. an integer value of 0 or higher).
  • naturalNonZero - Must be a natural number, but higher than 0 (ie. an integer value of 1 or higher).
  • between:min:max - The value must numerically be between the min and max values (exclusive).
  • range:min:max - The value must numerically be within the min and max values (inclusive).
  • lessThan:maxValue - The value must numerically be less than the specified maxValue (exclusive).
  • lessThanEqualTo:maxValue - The value must numerically be less than or equal to the specified maxValue (inclusive).
  • greaterThan:minValue - The value must numerically be greater than the specified minValue (exclusive).
  • greaterThanEqualTo:minValue - The value must numerically be greater than or equal to the specified minValue (inclusive).

Relations to other fields

  • matchesField:field - The value in this field must equal the value in the specified other field.
  • different:field - The value in this field must not equal the value in the specified other field.

JavaScript types

  • NaN - Must be NaN.
  • null - Must be null
  • string - Must be a String.
  • number - Must be a Number.
  • array - Must be an Array.
  • plainObject - Must be a plain object (ie. object literal).
  • date - Must be a Date object.
  • function - Must be a Function.
  • regExp - Must be a RegExp object.
  • arguments - Must be an arguments object.

Format

  • email - Must be a validly formatted e-mail address.
  • luhn - Must be a validly formatted creditcard number (according to a Luhn regular expression).
  • url - Must be a validly formatted URL.
  • ipv4 - Must be a validly formatted IPv4 address.
  • ipv6 - Must be a validly formatted IPv6 address.
  • uuid - Must be a validly formatted UUID.
  • base64 - Must be a validly formatted base64 string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment