Skip to content

Latest commit

 

History

History
456 lines (400 loc) · 11.6 KB

STRING.md

File metadata and controls

456 lines (400 loc) · 11.6 KB

String methods

  1. min
  • Checks if the string has a size bigger than a specified minimum value.
     min(minValue: number, message?: string)
  • Parameters:
    • minValue: Minimum string length.
    • message (optional): Overrides the default error message.
  • Example:
     string().min(5, "Minimum length should be 5 characters");
  1. max
  • Checks if the string has a size smaller than a specified maximum value.
     max(maxValue: number, message?: string)
  • Parameters:
    • maxValue: Maximum string length.
    • message (optional): Overrides the default error message.
  • Example:
     string().max(10, "Maximum length should be 10 characters");
  1. between
  • Checks if the string length is between a specified minimum and maximum value.
     between(minValue:number, maxValue: number, message?: string)
  • Parameters:
    • minValue: Minimum string length.
    • maxValue: Maximum string length.
    • message (optional): Overrides the default error message.
  • Example:
     string().between(5, 10, "Length should be between 5 and 10 characters");
  1. length
  • Checks if the string has a specific length.
     length(length: number, message?: string)
  • Parameters:
    • length: Expected string length.
    • message (optional): Overrides the default error message.
  • Example:
     string().length(8, "Length should be exactly 8 characters");
  1. equals
  • Checks if the string is equal to a specified value or an array of values.
     equals(value: number, message?: string)
  • Parameters:
    • value: Value(s) to compare with.
    • message (optional): Overrides the default error message.
  • Example:
     string().equals("password", "Value should be 'password'");
  1. pattern
  • Matches the string against a regular expression pattern.
     pattern(reg: RegExp, message?: string)
  • Parameters:
    • reg: Regular expression pattern.
    • message (optional): Overrides the default error message.
  • Example:
     string().pattern(/[a-zA-Z]+/, "Should contain only alphabetic characters");
  1. empty
  • Checks if the string is empty.
     empty(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().empty("Value should be empty");
  1. contains
  • Checks if the string contains a specified value.
     pattern(value: string, message?: string)
  • Parameters:
    • value: Value to check if contained.
    • message (optional): Overrides the default error message.
  • Example:
     string().contains("example", "Should contain 'example'");
  1. numeric
  • Checks if the string contains only numeric characters.
     numeric(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().numeric("Should contain only numeric characters");
  1. alpha
  • Checks if the string contains only alphabetic characters.
     alpha(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().alpha("Should contain only alphabetic characters");
  1. alphanum
  • Checks if the string contains only alphanumeric characters.
     alphanum(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().alphanum("Should contain only alphanumeric characters");
  1. alphadash
  • Checks if the string contains only alphanumeric characters, dashes, and underscores.
     alphadash(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().alphadash("Should contain only alphanumeric characters, dashes, and underscores");
  1. hex
  • Checks if the string is a hexadecimal value.
     hex(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().hex("Should be a hexadecimal value");
  1. base64
  • Checks if the string is a base64 value.
     base64(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().base64("Should be a base64 value");
  1. uuid
  • Checks if the string is a valid UUID.
     uuid(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().uuid("Should be a valid UUID");
  1. url
  • Checks if the string is a valid URL.
     url(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().url("Should be a valid URL");
  1. cuid
  • Checks if the string is a CUID format.
     cuid(message?: string)
  • Parameters:
    • message (optional): Overrides the default error message.
  • Example:
     string().cuid("Should be a CUID");
  1. email
  • Checks if the string is a valid email address.
     email(mode?: 'basic' | 'precise', message?: string)
  • Parameters:
    • mode (optional): Defines if it's basic or precise validation.
    • message (optional): Overrides the default error message.
  • Example:
     string().email("basic", "Should be a valid email address");
  1. postalCode
  • Checks if the string is a valid postal code.
     postalCode(postalCode?: PostalCodeInfo | ((value: NonNullable<Input>, form: any) => PostalCodeInfo), message?: string)
  • Parameters:
    • postalCode: Postal code to validate or a function that returns the desired postal code.
    • message (optional): Overrides the default error message.
  • Example:
     import { PostalCodes } from '@resourge/schemas/postalCodes';
    
     string().postalCode(PostalCodes.US, "Should be a valid US postal code");
  1. phoneNumber
  • Checks if the string is a valid phone number.
     phoneNumber(phoneNumber?: PhoneNumberInfo | ((value: NonNullable<Input>, form: any) => PhoneNumberInfo), message?: string)
  • Parameters:
    • phoneNumber: Phone number to validate or a function that returns the desired phone number.
    • message (optional): Overrides the default error message.
  • Example:
     import { PhoneNumbers } from '@resourge/schemas/phoneNumbers';
    
     string().phoneNumber(PhoneNumbers.en_US, "Should be a valid US phone number");
  1. enum
  • Checks if the string is a value of an enum.
     enum(enumObject: { [name: string]: any }, message?: string)
  • Parameters:
    • enumObject: Enum object.
    • message (optional): Overrides the default error message.
  • Example:
     string().enum(MyEnum, "Should be a value of MyEnum");
  1. test
  • Allows you to define custom synchronous validation methods for the schema.
     test(method: TestMethodConfig<RuleBooleanMethod<Input, Form>>)
  • Parameters:
    • method: Configuration object containing the validation method and error message.
      • is: The validation method which returns a boolean indicating whether the value is valid.
      • message: Error message to be displayed when validation fails.
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().test({
       is: (value) => value.length >= 3,
       message: 'Username must be at least 3 characters long.',
     });
  1. asyncTest
  • Enables the definition of custom asynchronous validation methods for the schema.
     asyncTest(method: TestMethodConfig<AsyncRuleBooleanMethod<Input, Form>>)
  • Parameters:
    • method: Configuration object containing the asynchronous validation method and error message.
      • is: The asynchronous validation method which returns a promise resolving to a boolean indicating whether the value is valid.
      • message: Error message to be displayed when validation fails.
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().asyncTest({
       is: async (value) => Promise.resolve(value.length >= 3),
       message: 'Username must be at least 3 characters long.',
     });
  1. when
  • Facilitates conditional validation based on a specified condition.
     when(name: string, config: WhenConfig<S, Value, Form>)
     when(config: WhenConfig<S, Value, Form>)
  • Parameters:
    • name (optional): Name of the condition (if provided).
    • config: Configuration object defining the condition and corresponding actions.
      • is: The condition function which returns a boolean.
      • then: A callback function to be executed if the condition is true.
      • otherwise: A callback function to be executed if the condition is false.
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().when({
       is: (value) => value.length >= 3,
       then: (schema) => schema.required(),
       otherwise: (schema) => schema.notOptional()
     });
  1. onlyOnTouch
  • Allows validation to occur only when a field is interacted with (touched).
     onlyOnTouch(onlyOnTouch?: (schema: this) => this)
  • Parameters:
    • onlyOnTouch (optional): A custom callback function to define validation behavior when validation occurs only on touch.
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().onlyOnTouch();
  1. notOnlyOnTouch
  • Disables the validation to occur only on touch, allowing validation on any interaction.
     notOnlyOnTouch()
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().notOnlyOnTouch();
  1. required
  • Makes a field mandatory for validation.
     required(message?: string)
  • Parameters:
    • message (optional): Custom error message to be displayed when the value is required but missing.
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().required("Value is required");
  1. notRequired
  • Marks a field as optional for validation.
     notRequired()
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().notRequired();
  1. optional
  • Allows a field to be optional during validation.
     optional()
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().optional();;
  1. notOptional
  • Ensures that a field is not optional for validation.
     notOptional(message?: string)
  • Parameters:
    • message (optional): Custom error message to be displayed when the value is not optional but is missing.
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().notOptional("Value is not optional");;
  1. nullable
  • Permits null values during validation.
     nullable()
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().nullable();
  1. notNullable
  • Prohibits null values during validation.
     notNullable(message?: string)
  • Parameters:
    • message (optional): Custom error message to be displayed when the value is not nullable but is null.
  • Example
     import { string } from '@resourge/schema';
    
     // Define an string schema
     string().notNullable("Value cannot be null");;