Logos DX
    Preparing search index...

    Function isOptional

    • Optional value check with custom validation.

      Returns true if value is undefined/null OR if the custom check passes. Useful for validating optional parameters with specific criteria.

      Type Parameters

      • T

      Parameters

      • val: T | undefined

        value to check

      • check: boolean | AssertTestFn<T>

        function or boolean to validate the value

      Returns boolean

      true if value is optional or passes the check

      // With function check
      function processData(data: any, timeout?: number) {
      assert(isOptional(timeout, (t) => t > 0), 'Timeout must be positive');
      // Process data...
      }
      // With boolean check
      const isValid = validateInput(input);
      if (isOptional(config.strict, isValid)) {
      // Either strict mode is off or input is valid
      processInput(input);
      }
      // Check optional email format
      isOptional(user.email, (email) => email.includes('@')) // true if email is undefined or contains @