diff --git a/composer.json b/composer.json index cf4f01e..f4c080f 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "require": { "php": ">=8.1", "utopia-php/compression": "0.1.*", - "utopia-php/telemetry": "0.1.*" + "utopia-php/telemetry": "0.1.*", + "utopia-php/validators": "0.1.*" }, "require-dev": { "phpunit/phpunit": "^9.5.25", diff --git a/composer.lock b/composer.lock index 51702c5..737cede 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "703d47ec02ec2d09dda755ab4924cae6", + "content-hash": "e07ec1b1f2bc1ee69c29e3d9e8b00ba4", "packages": [ { "name": "brick/math", @@ -1961,6 +1961,51 @@ "source": "https://github.com/utopia-php/telemetry/tree/0.1.1" }, "time": "2025-03-17T11:57:52+00:00" + }, + { + "name": "utopia-php/validators", + "version": "0.1.0", + "source": { + "type": "git", + "url": "https://github.com/utopia-php/validators.git", + "reference": "5c57d5b6cf964f8981807c1d3ea8df620c869080" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/utopia-php/validators/zipball/5c57d5b6cf964f8981807c1d3ea8df620c869080", + "reference": "5c57d5b6cf964f8981807c1d3ea8df620c869080", + "shasum": "" + }, + "require": { + "php": ">=8.0" + }, + "require-dev": { + "laravel/pint": "1.*", + "phpstan/phpstan": "1.*", + "phpunit/phpunit": "11.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "Utopia\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A lightweight collection of reusable validators for Utopia projects", + "keywords": [ + "php", + "utopia", + "validation", + "validator" + ], + "support": { + "issues": "https://github.com/utopia-php/validators/issues", + "source": "https://github.com/utopia-php/validators/tree/0.1.0" + }, + "time": "2025-11-18T11:05:46+00:00" } ], "packages-dev": [ @@ -5067,5 +5112,5 @@ "php": ">=8.1" }, "platform-dev": {}, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.9.0" } diff --git a/src/Validator.php b/src/Validator.php deleted file mode 100755 index d58dfea..0000000 --- a/src/Validator.php +++ /dev/null @@ -1,57 +0,0 @@ - $validators - */ - public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED) - { - } - - /** - * Get Validators - * - * Returns validators array - * - * @return array - */ - public function getValidators(): array - { - return $this->validators; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - if (!(\is_null($this->failedRule))) { - $description = $this->failedRule->getDescription(); - } else { - $description = $this->validators[0]->getDescription(); - } - - return $description; - } - - /** - * Is valid - * - * Validation will pass when all rules are valid if only one of the rules is invalid validation will fail. - * - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - foreach ($this->validators as $rule) { - $valid = $rule->isValid($value); - - $this->failedRule = $rule; - - if ($valid) { - return true; - } - } - - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return true; - } -} diff --git a/src/Validator/ArrayList.php b/src/Validator/ArrayList.php deleted file mode 100644 index f7c38fc..0000000 --- a/src/Validator/ArrayList.php +++ /dev/null @@ -1,116 +0,0 @@ -validator = $validator; - $this->length = $length; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - $msg = 'Value must a valid array'; - - if ($this->length > 0) { - $msg .= ' no longer than ' . $this->length . ' items'; - } - - return $msg . ' and ' . $this->validator->getDescription(); - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return true; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return $this->validator->getType(); - } - - /** - * Get Nested Validator - * - * @return Validator - */ - public function getValidator(): Validator - { - return $this->validator; - } - - /** - * Is valid - * - * Validation will pass when $value is valid array and validator is valid. - * - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - if (!\is_array($value)) { - return false; - } - - if ($this->length && \count($value) > $this->length) { - return false; - } - - foreach ($value as $element) { - if (!$this->validator->isValid($element)) { - return false; - } - } - - return true; - } -} diff --git a/src/Validator/Assoc.php b/src/Validator/Assoc.php deleted file mode 100644 index 5261868..0000000 --- a/src/Validator/Assoc.php +++ /dev/null @@ -1,73 +0,0 @@ - 65535) { - return false; - } - - return \array_keys($value) !== \range(0, \count($value) - 1); - } -} diff --git a/src/Validator/Boolean.php b/src/Validator/Boolean.php deleted file mode 100644 index 50c1c98..0000000 --- a/src/Validator/Boolean.php +++ /dev/null @@ -1,94 +0,0 @@ -loose = $loose; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'Value must be a valid boolean'; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_BOOLEAN; - } - - /** - * Is valid - * - * Validation will pass when $value has a boolean value. - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - if ($this->loose && ($value === 'true' || $value === 'false')) { // Accept strings - return true; - } - - if ($this->loose && ($value === '1' || $value === '0')) { // Accept numeric strings - return true; - } - - if ($this->loose && ($value === 1 || $value === 0)) { // Accept integers - return true; - } - - if (\is_bool($value)) { - return true; - } - - return false; - } -} diff --git a/src/Validator/Domain.php b/src/Validator/Domain.php deleted file mode 100644 index b27493e..0000000 --- a/src/Validator/Domain.php +++ /dev/null @@ -1,145 +0,0 @@ - $restrictions Set of conditions that prevent validation from passing - * @param bool $hostnames Allow only valid hostnames - */ - public function __construct( - protected array $restrictions = [], - private readonly bool $hostnames = true, - ) { - } - - /** - * Helper for creating domain restriction rule. - * Such rules prevent validation from passing, so this behaves as deny-list. - * - * @param string $hostname A domain base, such as top-level domain or subdomain. Restriction is only applied if domain matches this hostname - * @param int $levels Specify what level (top-level, subdomain, sub-subdomain, ..) domain must be. Example: "stage.appwrite.io" is level 3 - * @param array $prefixDenyList Disallowed beginning of domain, useful for reserved behaviours, such as prefixing "branch-" for preview domains - * - */ - public static function createRestriction(string $hostname, ?int $levels = null, array $prefixDenyList = []) - { - return [ - 'hostname' => $hostname, - 'levels' => $levels, - 'prefixDenyList' => $prefixDenyList, - ]; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'Value must be a valid domain'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid domain. - * - * Validates domain names against RFC 1034, RFC 1035, RFC 952, RFC 1123, RFC 2732, RFC 2181, and RFC 1123. - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - if (empty($value)) { - return false; - } - - if (!is_string($value)) { - return false; - } - - if ( - \filter_var( - $value, - FILTER_VALIDATE_DOMAIN, - $this->hostnames ? FILTER_FLAG_HOSTNAME : 0 - ) === false - ) { - return false; - } - - if (\str_ends_with($value, '.') || \str_ends_with($value, '-')) { - return false; - } - - foreach ($this->restrictions as $restriction) { - $hostname = $restriction['hostname']; - $levels = $restriction['levels']; - $prefixDenyList = $restriction['prefixDenyList']; - - // Only apply restriction rules to relevant domains - if (!\str_ends_with($value, $hostname)) { - continue; - } - - // Domain-level restriction - if (!is_null($levels)) { - $expectedPartsCount = $levels; - $partsCount = \count(\explode('.', $value, $expectedPartsCount + 1)); - if ($partsCount !== $expectedPartsCount) { - return false; - } - } - - // Domain prefix (beginning) restriction - if (!empty($prefixDenyList)) { - foreach ($prefixDenyList as $deniedPrefix) { - if (\str_starts_with($value, $deniedPrefix)) { - return false; - } - } - } - } - - return true; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/src/Validator/FloatValidator.php b/src/Validator/FloatValidator.php deleted file mode 100755 index 3641424..0000000 --- a/src/Validator/FloatValidator.php +++ /dev/null @@ -1,88 +0,0 @@ -loose = $loose; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'Value must be a valid float'; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_FLOAT; - } - - /** - * Is valid - * - * Validation will pass when $value is float. - * - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - if ($this->loose) { - if (!\is_numeric($value)) { - return false; - } - $value = $value + 0; - } - if (!\is_float($value) && !\is_int($value)) { - return false; - } - - return true; - } -} diff --git a/src/Validator/HexColor.php b/src/Validator/HexColor.php deleted file mode 100644 index 9a45f4c..0000000 --- a/src/Validator/HexColor.php +++ /dev/null @@ -1,53 +0,0 @@ -whitelist = $whitelist; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'URL host must be one of: ' . \implode(', ', $this->whitelist); - } - - /** - * Is valid - * - * Validation will pass when $value starts with one of the given hosts - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - $urlValidator = new URL(); - - if (!$urlValidator->isValid($value)) { - return false; - } - - $hostnameValidator = new Hostname($this->whitelist); - - return $hostnameValidator->isValid(\parse_url($value, PHP_URL_HOST)); - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/src/Validator/Hostname.php b/src/Validator/Hostname.php deleted file mode 100644 index b26d62d..0000000 --- a/src/Validator/Hostname.php +++ /dev/null @@ -1,114 +0,0 @@ -allowList = $allowList; - } - - /** - * @return string - */ - public function getDescription(): string - { - return 'Value must be a valid hostname without path, port and protocol.'; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } - - /** - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - // Validate proper format - if (!\is_string($value) || empty($value)) { - return false; - } - - // Max length 253 chars: https://en.wikipedia.org/wiki/Hostname#:~:text=The%20entire%20hostname%2C%20including%20the,maximum%20of%20253%20ASCII%20characters - if (\mb_strlen($value) > 253) { - return false; - } - - // This tests: 'http://', 'https://', and 'myapp.com/route' - if (\str_contains($value, '/')) { - return false; - } - - // This tests for: 'myapp.com:3000' - if (\str_contains($value, ':')) { - return false; - } - - // Logic #1: Empty allowList means everything is allowed - if (empty($this->allowList)) { - return true; - } - - // Logic #2: Allow List not empty, there are rules to check - // Loop through all allowed hostnames until match is found - foreach ($this->allowList as $allowedHostname) { - // If exact match; allow - // If *, allow everything - if ($value === $allowedHostname || $allowedHostname === '*') { - return true; - } - - // If wildcard symbol used - if (\str_starts_with($allowedHostname, '*')) { - // Remove starting * symbol before comparing - $allowedHostname = substr($allowedHostname, 1); - - // If rest of hostname match; allow - // Notice allowedHostname still includes starting dot. Root domain is NOT allowed by wildcard. - if (\str_ends_with($value, $allowedHostname)) { - return true; - } - } - } - - // If finished loop above without result, match is not found - return false; - } -} diff --git a/src/Validator/IP.php b/src/Validator/IP.php deleted file mode 100644 index 69e1f95..0000000 --- a/src/Validator/IP.php +++ /dev/null @@ -1,113 +0,0 @@ -type = $type; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'Value must be a valid IP address'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid IP address. - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - switch ($this->type) { - case self::ALL: - if (\filter_var($value, FILTER_VALIDATE_IP)) { - return true; - } - break; - - case self::V4: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - return true; - } - break; - - case self::V6: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - return true; - } - break; - - default: - return false; - } - - return false; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/src/Validator/Integer.php b/src/Validator/Integer.php deleted file mode 100755 index ed88521..0000000 --- a/src/Validator/Integer.php +++ /dev/null @@ -1,88 +0,0 @@ -loose = $loose; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'Value must be a valid integer'; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_INTEGER; - } - - /** - * Is valid - * - * Validation will pass when $value is integer. - * - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - if ($this->loose) { - if (!\is_numeric($value)) { - return false; - } - $value = $value + 0; - } - if (!\is_int($value)) { - return false; - } - - return true; - } -} diff --git a/src/Validator/JSON.php b/src/Validator/JSON.php deleted file mode 100644 index a3bf88a..0000000 --- a/src/Validator/JSON.php +++ /dev/null @@ -1,59 +0,0 @@ -addRule($rule); - } - - $this->type = $type; - } - /** - * Add rule - * - * Add a new rule to the end of the rules containing array - * - * @param Validator $rule - * @return $this - */ - public function addRule(Validator $rule) - { - $this->rules[] = $rule; - - return $this; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - $description = ''; - foreach ($this->rules as $key => $rule) { - $description .= ++$key . '. ' . $rule->getDescription() . " \n"; - } - - return $description; - } - - /** - * Is valid - * - * Validation will pass when all rules are valid if only one of the rules is invalid validation will fail. - * - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - foreach ($this->rules as $rule) { /* @var $rule Validator */ - if (false === $rule->isValid($value)) { - return false; - } - } - - return true; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return true; - } -} diff --git a/src/Validator/Nullable.php b/src/Validator/Nullable.php deleted file mode 100644 index 3d0d872..0000000 --- a/src/Validator/Nullable.php +++ /dev/null @@ -1,73 +0,0 @@ -validator->getDescription() . ' or null'; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return $this->validator->getType(); - } - - /** - * @return Validator - */ - public function getValidator(): Validator - { - return $this->validator; - } - - /** - * Is valid - * - * Validation will pass when $value is text with valid length. - * - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - if (\is_null($value)) { - return true; - } - - return $this->validator->isValid($value); - } -} diff --git a/src/Validator/Numeric.php b/src/Validator/Numeric.php deleted file mode 100755 index e3db46c..0000000 --- a/src/Validator/Numeric.php +++ /dev/null @@ -1,66 +0,0 @@ -min = $min; - $this->max = $max; - $this->format = $format; - } - - /** - * Get Range Minimum Value - * - * @return int|float - */ - public function getMin(): int|float - { - return $this->min; - } - - /** - * Get Range Maximum Value - * - * @return int|float - */ - public function getMax(): int|float - { - return $this->max; - } - - /** - * Get Range Format - * - * @return string - */ - public function getFormat(): string - { - return $this->format; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'Value must be a valid range between '.\number_format($this->min).' and '.\number_format($this->max); - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return $this->format; - } - - /** - * Is valid - * - * Validation will pass when $value number is bigger or equal than $min number and lower or equal than $max. - * Not strict, considers any valid integer to be a valid float - * Considers infinity to be a valid integer - * - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - if (!parent::isValid($value)) { - return false; - } - - switch ($this->format) { - case self::TYPE_INTEGER: - // Accept infinity as an integer - // Since gettype(INF) === TYPE_FLOAT - if ($value === INF || $value === -INF) { - break; // move to check if value is within range - } - $value = $value + 0; - if (!is_int($value)) { - return false; - } - break; - case self::TYPE_FLOAT: - if (!is_numeric($value)) { - return false; - } - $value = $value + 0.0; - break; - default: - return false; - } - - if ($this->min <= $value && $this->max >= $value) { - return true; - } - - return false; - } -} diff --git a/src/Validator/Text.php b/src/Validator/Text.php deleted file mode 100644 index 8be82d4..0000000 --- a/src/Validator/Text.php +++ /dev/null @@ -1,138 +0,0 @@ -length = $length; - $this->min = $min; - $this->allowList = $allowList; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - $message = 'Value must be a valid string'; - - if ($this->min === $this->length) { - $message .= ' and exactly '.$this->length.' chars'; - } else { - if ($this->min) { - $message .= ' and at least '.$this->min.' chars'; - } - - if ($this->length) { - $message .= ' and no longer than '.$this->length.' chars'; - } - } - - if ($this->allowList) { - $message .= ' and only consist of \''.\implode(', ', $this->allowList).'\' chars'; - } - - return $message; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } - - /** - * Is valid - * - * Validation will pass when $value is text with valid length. - * - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - if (!\is_string($value)) { - return false; - } - - if (\mb_strlen($value) < $this->min) { - return false; - } - - if (\mb_strlen($value) > $this->length && $this->length !== 0) { - return false; - } - - if (\count($this->allowList) > 0) { - foreach (\str_split($value) as $char) { - if (!\in_array($char, $this->allowList)) { - return false; - } - } - } - - return true; - } -} diff --git a/src/Validator/URL.php b/src/Validator/URL.php deleted file mode 100644 index 1c12008..0000000 --- a/src/Validator/URL.php +++ /dev/null @@ -1,86 +0,0 @@ -allowedSchemes = $allowedSchemes; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - if (!empty($this->allowedSchemes)) { - return 'Value must be a valid URL with following schemes (' . \implode(', ', $this->allowedSchemes) . ')'; - } - - return 'Value must be a valid URL'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid URL. - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - if (\filter_var($value, FILTER_VALIDATE_URL) === false) { - return false; - } - - if (!empty($this->allowedSchemes) && !\in_array(\parse_url($value, PHP_URL_SCHEME), $this->allowedSchemes)) { - return false; - } - - return true; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php deleted file mode 100755 index b374bb4..0000000 --- a/src/Validator/WhiteList.php +++ /dev/null @@ -1,119 +0,0 @@ -list = $list; - $this->strict = $strict; - $this->type = $type; - - if (!$this->strict) { - foreach ($this->list as $key => &$value) { - $this->list[$key] = \strtolower($value); - } - } - } - - /** - * Get List of All Allowed Values - * - * @return array - */ - public function getList(): array - { - return $this->list; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'Value must be one of ('.\implode(', ', $this->list).')'; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return $this->type; - } - - /** - * Is valid - * - * Validation will pass if $value is in the white list array. - * - * @param mixed $value - * @return bool - */ - public function isValid(mixed $value): bool - { - if (\is_array($value)) { - return false; - } - - $value = ($this->strict) ? $value : \strtolower($value); - - if (!\in_array($value, $this->list, $this->strict)) { - return false; - } - - return true; - } -} diff --git a/src/Validator/Wildcard.php b/src/Validator/Wildcard.php deleted file mode 100644 index d8c4779..0000000 --- a/src/Validator/Wildcard.php +++ /dev/null @@ -1,62 +0,0 @@ -assertFalse($arrayList->isValid(['text'])); - $this->assertEquals('Value must a valid array and Value must be a valid integer', $arrayList->getDescription()); - - $arrayList = new ArrayList(new Integer(), 3); - $this->assertFalse($arrayList->isValid(['a', 'b', 'c', 'd'])); - $this->assertEquals('Value must a valid array no longer than 3 items and Value must be a valid integer', $arrayList->getDescription()); - } - - public function testCanValidateTextValues(): void - { - $arrayList = new ArrayList(new Text(100)); - $this->assertTrue($arrayList->isArray(), 'true'); - $this->assertTrue($arrayList->isValid([0 => 'string', 1 => 'string'])); - $this->assertTrue($arrayList->isValid(['string', 'string'])); - $this->assertFalse($arrayList->isValid(['string', 'string', 3])); - $this->assertFalse($arrayList->isValid('string')); - $this->assertFalse($arrayList->isValid('string')); - $this->assertEquals(\Utopia\Validator::TYPE_STRING, $arrayList->getType()); - $this->assertInstanceOf(Text::class, $arrayList->getValidator()); - } - - public function testCanValidateNumericValues(): void - { - $arrayList = new ArrayList(new Numeric()); - $this->assertTrue($arrayList->isValid([1, 2, 3])); - $this->assertFalse($arrayList->isValid(1)); - $this->assertFalse($arrayList->isValid('string')); - $this->assertEquals(\Utopia\Validator::TYPE_MIXED, $arrayList->getType()); - $this->assertInstanceOf(Numeric::class, $arrayList->getValidator()); - } - - public function testCanValidateNumericValuesWithBoundaries(): void - { - $arrayList = new ArrayList(new Numeric(), 2); - $this->assertTrue($arrayList->isValid([1])); - $this->assertTrue($arrayList->isValid([1, 2])); - $this->assertFalse($arrayList->isValid([1, 2, 3])); - $this->assertEquals($arrayList->getType(), \Utopia\Validator::TYPE_MIXED); - $this->assertInstanceOf(Numeric::class, $arrayList->getValidator()); - } -} diff --git a/tests/Validator/AssocTest.php b/tests/Validator/AssocTest.php deleted file mode 100755 index 8bf1c96..0000000 --- a/tests/Validator/AssocTest.php +++ /dev/null @@ -1,43 +0,0 @@ -assoc = new Assoc(); - } - - public function tearDown(): void - { - $this->assoc = null; - } - - public function testCanValidateAssocArray(): void - { - $this->assertTrue($this->assoc->isValid(['1' => 'a', '0' => 'b', '2' => 'c'])); - $this->assertTrue($this->assoc->isValid(['a' => 'a', 'b' => 'b', 'c' => 'c'])); - $this->assertTrue($this->assoc->isValid([])); - $this->assertTrue($this->assoc->isValid(['value' => str_repeat('-', 62000)])); - $this->assertTrue($this->assoc->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_ARRAY, $this->assoc->getType()); - } - - public function testCantValidateSequentialArray(): void - { - $this->assertFalse($this->assoc->isValid([0 => 'string', 1 => 'string'])); - $this->assertFalse($this->assoc->isValid(['a'])); - $this->assertFalse($this->assoc->isValid(['a', 'b', 'c'])); - $this->assertFalse($this->assoc->isValid(['0' => 'a', '1' => 'b', '2' => 'c'])); - } - - public function testCantValidateAssocArrayWithOver65kCharacters(): void - { - $this->assertFalse($this->assoc->isValid(['value' => str_repeat('-', 66000)])); - } -} diff --git a/tests/Validator/BooleanTest.php b/tests/Validator/BooleanTest.php deleted file mode 100755 index b174291..0000000 --- a/tests/Validator/BooleanTest.php +++ /dev/null @@ -1,46 +0,0 @@ -assertTrue($boolean->isValid(true)); - $this->assertTrue($boolean->isValid(false)); - $this->assertFalse($boolean->isValid('false')); - $this->assertFalse($boolean->isValid('true')); - $this->assertFalse($boolean->isValid('0')); - $this->assertFalse($boolean->isValid('1')); - $this->assertFalse($boolean->isValid(0)); - $this->assertFalse($boolean->isValid(1)); - $this->assertFalse($boolean->isValid(['string', 'string'])); - $this->assertFalse($boolean->isValid('string')); - $this->assertFalse($boolean->isValid(1.2)); - $this->assertFalse($boolean->isArray()); - $this->assertEquals($boolean->getType(), \Utopia\Validator::TYPE_BOOLEAN); - } - - public function testCanValidateLoosely() - { - $boolean = new Boolean(true); - - $this->assertTrue($boolean->isValid(true)); - $this->assertTrue($boolean->isValid(false)); - $this->assertTrue($boolean->isValid('false')); - $this->assertTrue($boolean->isValid('true')); - $this->assertTrue($boolean->isValid('0')); - $this->assertTrue($boolean->isValid('1')); - $this->assertTrue($boolean->isValid(0)); - $this->assertTrue($boolean->isValid(1)); - $this->assertFalse($boolean->isValid(['string', 'string'])); - $this->assertFalse($boolean->isValid('string')); - $this->assertFalse($boolean->isValid(1.2)); - $this->assertFalse($boolean->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_BOOLEAN, $boolean->getType()); - } -} diff --git a/tests/Validator/DomainTest.php b/tests/Validator/DomainTest.php deleted file mode 100644 index 39313a9..0000000 --- a/tests/Validator/DomainTest.php +++ /dev/null @@ -1,86 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class DomainTest extends TestCase -{ - protected Domain $domain; - - public function setUp(): void - { - $this->domain = new Domain(); - } - - public function testIsValid() - { - // Assertions - $this->assertEquals(true, $this->domain->isValid('example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example-app.com')); - $this->assertEquals(false, $this->domain->isValid('subdomain.example_app.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain-new.example.com')); - $this->assertEquals(false, $this->domain->isValid('subdomain_new.example.com')); - $this->assertEquals(true, $this->domain->isValid('localhost')); - $this->assertEquals(true, $this->domain->isValid('example.io')); - $this->assertEquals(true, $this->domain->isValid('example.org')); - $this->assertEquals(true, $this->domain->isValid('example.org')); - $this->assertEquals(false, $this->domain->isValid(false)); - $this->assertEquals(false, $this->domain->isValid('api.appwrite.io.')); - $this->assertEquals(false, $this->domain->isValid('.api.appwrite.io')); - $this->assertEquals(false, $this->domain->isValid('.api.appwrite.io')); - $this->assertEquals(false, $this->domain->isValid('api..appwrite.io')); - $this->assertEquals(false, $this->domain->isValid('api-.appwrite.io')); - $this->assertEquals(false, $this->domain->isValid('api.-appwrite.io')); - $this->assertEquals(false, $this->domain->isValid('app write.io')); - $this->assertEquals(false, $this->domain->isValid(' appwrite.io')); - $this->assertEquals(false, $this->domain->isValid('appwrite.io ')); - $this->assertEquals(false, $this->domain->isValid('-appwrite.io')); - $this->assertEquals(false, $this->domain->isValid('appwrite.io-')); - $this->assertEquals(false, $this->domain->isValid('.')); - $this->assertEquals(false, $this->domain->isValid('..')); - $this->assertEquals(false, $this->domain->isValid('')); - $this->assertEquals(false, $this->domain->isValid(['string', 'string'])); - $this->assertEquals(false, $this->domain->isValid(1)); - $this->assertEquals(false, $this->domain->isValid(1.2)); - } - - public function testRestrictions() - { - $validator = new Domain([ - Domain::createRestriction('appwrite.network', 3, ['preview-', 'branch-']), - Domain::createRestriction('fra.appwrite.run', 4), - ]); - - $this->assertEquals(true, $validator->isValid('google.com')); - $this->assertEquals(true, $validator->isValid('stage.google.com')); - $this->assertEquals(true, $validator->isValid('shard4.stage.google.com')); - - $this->assertEquals(false, $validator->isValid('appwrite.network')); - $this->assertEquals(false, $validator->isValid('preview-a.appwrite.network')); - $this->assertEquals(false, $validator->isValid('branch-a.appwrite.network')); - $this->assertEquals(true, $validator->isValid('google.appwrite.network')); - $this->assertEquals(false, $validator->isValid('stage.google.appwrite.network')); - $this->assertEquals(false, $validator->isValid('shard4.stage.google.appwrite.network')); - - $this->assertEquals(false, $validator->isValid('fra.appwrite.run')); - $this->assertEquals(true, $validator->isValid('appwrite.run')); - $this->assertEquals(true, $validator->isValid('google.fra.appwrite.run')); - $this->assertEquals(false, $validator->isValid('shard4.google.fra.appwrite.run')); - $this->assertEquals(true, $validator->isValid('branch-google.fra.appwrite.run')); - $this->assertEquals(true, $validator->isValid('preview-google.fra.appwrite.run')); - } -} diff --git a/tests/Validator/FloatValidatorTest.php b/tests/Validator/FloatValidatorTest.php deleted file mode 100755 index 80d8f4d..0000000 --- a/tests/Validator/FloatValidatorTest.php +++ /dev/null @@ -1,39 +0,0 @@ -assertTrue($validator->isValid(27.25)); - $this->assertTrue($validator->isValid(23)); - $this->assertTrue($validator->isValid(23.5)); - $this->assertTrue($validator->isValid(1e7)); - $this->assertFalse($validator->isValid('abc')); - $this->assertFalse($validator->isValid(true)); - $this->assertFalse($validator->isValid('23.5')); - $this->assertFalse($validator->isValid('23')); - $this->assertFalse($validator->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_FLOAT, $validator->getType()); - } - - public function testCanValidateLoosely(): void - { - $validator = new FloatValidator(true); - - $this->assertTrue($validator->isValid(27.25)); - $this->assertTrue($validator->isValid(23)); - $this->assertTrue($validator->isValid(23.5)); - $this->assertTrue($validator->isValid(1e7)); - $this->assertTrue($validator->isValid('23.5')); - $this->assertTrue($validator->isValid('23')); - $this->assertFalse($validator->isValid('abc')); - $this->assertFalse($validator->isValid(true)); - $this->assertFalse($validator->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_FLOAT, $validator->getType()); - } -} diff --git a/tests/Validator/HexColorTest.php b/tests/Validator/HexColorTest.php deleted file mode 100755 index 93b7aa6..0000000 --- a/tests/Validator/HexColorTest.php +++ /dev/null @@ -1,26 +0,0 @@ -assertTrue($hexColor->isValid('000')); - $this->assertTrue($hexColor->isValid('ffffff')); - $this->assertTrue($hexColor->isValid('fff')); - $this->assertTrue($hexColor->isValid('000000')); - - $this->assertFalse($hexColor->isValid('AB10BC99')); - $this->assertFalse($hexColor->isValid('AR1012')); - $this->assertFalse($hexColor->isValid('ab12bc99')); - $this->assertFalse($hexColor->isValid('00')); - $this->assertFalse($hexColor->isValid('ffff')); - $this->assertFalse($hexColor->isArray()); - - $this->assertEquals(\Utopia\Validator::TYPE_STRING, $hexColor->getType()); - } -} diff --git a/tests/Validator/HostTest.php b/tests/Validator/HostTest.php deleted file mode 100644 index 2e18e2e..0000000 --- a/tests/Validator/HostTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class HostTest extends TestCase -{ - protected Host $host; - - public function setUp(): void - { - $this->host = new Host(['example.io', 'subdomain.example.test', 'localhost', '*.appwrite.io']); - } - - public function testIsValid() - { - // Assertions - $this->assertEquals($this->host->isValid('https://example.io/link'), true); - $this->assertEquals($this->host->isValid('https://localhost'), true); - $this->assertEquals($this->host->isValid('localhost'), false); - $this->assertEquals($this->host->isValid('http://subdomain.example.test/path'), true); - $this->assertEquals($this->host->isValid('http://test.subdomain.example.test/path'), false); - $this->assertEquals($this->host->isValid('http://appwrite.io/path'), false); - $this->assertEquals($this->host->isValid('http://me.appwrite.io/path'), true); - $this->assertEquals($this->host->isValid('http://you.appwrite.io/path'), true); - $this->assertEquals($this->host->isValid('http://us.together.appwrite.io/path'), true); - $this->assertEquals($this->host->getType(), 'string'); - } -} diff --git a/tests/Validator/HostnameTest.php b/tests/Validator/HostnameTest.php deleted file mode 100755 index a1e492e..0000000 --- a/tests/Validator/HostnameTest.php +++ /dev/null @@ -1,106 +0,0 @@ -assertEquals(\Utopia\Validator::TYPE_STRING, $validator->getType()); - $this->assertFalse($validator->isArray()); - - $this->assertTrue($validator->isValid('myweb.com')); - $this->assertTrue($validator->isValid('httpmyweb.com')); - $this->assertTrue($validator->isValid('httpsmyweb.com')); - $this->assertTrue($validator->isValid('wsmyweb.com')); - $this->assertTrue($validator->isValid('wssmyweb.com')); - $this->assertTrue($validator->isValid('vercel.app')); - $this->assertTrue($validator->isValid('web.vercel.app')); - $this->assertTrue($validator->isValid('my-web.vercel.app')); - $this->assertTrue($validator->isValid('my-project.my-web.vercel.app')); - $this->assertTrue($validator->isValid('my-commit.my-project.my-web.vercel.app')); - $this->assertTrue($validator->isValid('myapp.co.uk')); - $this->assertTrue($validator->isValid('*.myapp.com')); - $this->assertTrue($validator->isValid('myapp.*')); - - $this->assertFalse($validator->isValid('https://myweb.com')); - $this->assertFalse($validator->isValid('ws://myweb.com')); - $this->assertFalse($validator->isValid('wss://myweb.com')); - $this->assertFalse($validator->isValid('http://myweb.com')); - $this->assertFalse($validator->isValid('http://myweb.com:3000')); - $this->assertFalse($validator->isValid('http://myweb.com/blog')); - $this->assertFalse($validator->isValid('myweb.com:80')); - $this->assertFalse($validator->isValid('myweb.com:3000')); - $this->assertFalse($validator->isValid('myweb.com/blog')); - $this->assertFalse($validator->isValid('myweb.com/blog/article1')); - - // Max length test - $domain = \str_repeat('bestdomain', 25); // 250 chars total - - $domain .= '.sk'; // Exactly at the limit - $this->assertTrue($validator->isValid($domain)); - - $domain .= 'a'; // Exactly over the limit - $this->assertFalse($validator->isValid($domain)); - } - - public function testCanValidateHostnamesWithAllowList(): void - { - // allowList tests - $validator = new Hostname([ - 'myweb.vercel.app', - 'myweb.com', - '*.myapp.com', - ]); - - $this->assertTrue($validator->isValid('myweb.vercel.app')); - $this->assertFalse($validator->isValid('myweb.vercel.com')); - $this->assertFalse($validator->isValid('myweb2.vercel.app')); - $this->assertFalse($validator->isValid('vercel.app')); - $this->assertFalse($validator->isValid('mycommit.myweb.vercel.app')); - - $this->assertTrue($validator->isValid('myweb.com')); - $this->assertFalse($validator->isValid('myweb.eu')); - $this->assertFalse($validator->isValid('project.myweb.eu')); - $this->assertFalse($validator->isValid('commit.project.myweb.eu')); - - $this->assertTrue($validator->isValid('project1.myapp.com')); - $this->assertTrue($validator->isValid('project2.myapp.com')); - $this->assertTrue($validator->isValid('project-with-dash.myapp.com')); - $this->assertTrue($validator->isValid('anything.myapp.com')); - $this->assertTrue($validator->isValid('commit.anything.myapp.com')); - $this->assertFalse($validator->isValid('anything.myapp.eu')); - $this->assertFalse($validator->isValid('myapp.com')); - - $validator = new Hostname(['localhost']); - $this->assertTrue($validator->isValid('localhost')); - } - - public function testCanValidateHostnamesWithWildcard(): void - { - $validator = new Hostname(); - $this->assertTrue($validator->isValid('*')); - - $validator = new Hostname(['netlify.*']); - $this->assertFalse($validator->isValid('netlify.com')); - $this->assertFalse($validator->isValid('netlify.eu')); - $this->assertFalse($validator->isValid('netlify.app')); - - $validator = new Hostname(['*.*.app.io']); - $this->assertFalse($validator->isValid('app.io')); - $this->assertFalse($validator->isValid('project.app.io')); - $this->assertFalse($validator->isValid('commit.project.app.io')); - $this->assertFalse($validator->isValid('api.commit.project.app.io')); - - $validator = new Hostname(['*']); - $this->assertTrue($validator->isValid('*')); - $this->assertTrue($validator->isValid('localhost')); - $this->assertTrue($validator->isValid('anything')); // Like localhost - $this->assertTrue($validator->isValid('anything.com')); - $this->assertTrue($validator->isValid('anything.with.subdomains.eu')); - } -} diff --git a/tests/Validator/IPTest.php b/tests/Validator/IPTest.php deleted file mode 100644 index cd2c8c8..0000000 --- a/tests/Validator/IPTest.php +++ /dev/null @@ -1,79 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class IPTest extends TestCase -{ - protected IP $validator; - - public function testIsValidIP() - { - $validator = new IP(); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($validator->isValid('109.67.204.101'), true); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - $this->assertEquals($validator->getType(), 'string'); - } - - public function testIsValidIPALL() - { - $validator = new IP(IP::ALL); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($validator->isValid('109.67.204.101'), true); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } - - public function testIsValidIPV4() - { - $validator = new IP(IP::V4); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), false); - $this->assertEquals($validator->isValid('109.67.204.101'), true); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } - - public function testIsValidIPV6() - { - $validator = new IP(IP::V6); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($validator->isValid('109.67.204.101'), false); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } -} diff --git a/tests/Validator/IntegerTest.php b/tests/Validator/IntegerTest.php deleted file mode 100755 index 4161b3a..0000000 --- a/tests/Validator/IntegerTest.php +++ /dev/null @@ -1,36 +0,0 @@ -assertTrue($validator->isValid(23)); - $this->assertFalse($validator->isValid('23')); - $this->assertFalse($validator->isValid(23.5)); - $this->assertFalse($validator->isValid('23.5')); - $this->assertFalse($validator->isValid(null)); - $this->assertFalse($validator->isValid(true)); - $this->assertFalse($validator->isValid(false)); - $this->assertFalse($validator->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_INTEGER, $validator->getType()); - } - - public function testCanValidateLoosely() - { - $validator = new Integer(true); - $this->assertTrue($validator->isValid(23)); - $this->assertTrue($validator->isValid('23')); - $this->assertFalse($validator->isValid(23.5)); - $this->assertFalse($validator->isValid('23.5')); - $this->assertFalse($validator->isValid(null)); - $this->assertFalse($validator->isValid(true)); - $this->assertFalse($validator->isValid(false)); - $this->assertFalse($validator->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_INTEGER, $validator->getType()); - } -} diff --git a/tests/Validator/JSONTest.php b/tests/Validator/JSONTest.php deleted file mode 100755 index bafce37..0000000 --- a/tests/Validator/JSONTest.php +++ /dev/null @@ -1,28 +0,0 @@ -assertTrue($json->isValid('{}')); - $this->assertTrue($json->isValid([])); - $this->assertTrue($json->isValid(['test'])); - $this->assertTrue($json->isValid(['test' => 'demo'])); - $this->assertTrue($json->isValid('{"test": "demo"}')); - - $this->assertFalse($json->isValid('')); - $this->assertFalse($json->isValid(false)); - $this->assertFalse($json->isValid(null)); - $this->assertFalse($json->isValid('string')); - $this->assertFalse($json->isValid(1)); - $this->assertFalse($json->isValid(1.2)); - $this->assertFalse($json->isValid("{'test': 'demo'}")); - $this->assertFalse($json->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_OBJECT, $json->getType()); - } -} diff --git a/tests/Validator/MultipleOfTest.php b/tests/Validator/MultipleOfTest.php deleted file mode 100644 index 9b31b03..0000000 --- a/tests/Validator/MultipleOfTest.php +++ /dev/null @@ -1,31 +0,0 @@ -assertTrue($vaidator->isValid($validTextValidUrl)); - $this->assertTrue($vaidator->isValid($validTextInvalidUrl)); - $this->assertTrue($vaidator->isValid($invalidTextValidUrl)); - $this->assertFalse($vaidator->isValid($invalidTextInvalidUrl)); - - $this->assertCount(2, $vaidator->getValidators()); - $this->assertEquals("Utopia\Validator\Text", \get_class($vaidator->getValidators()[0])); - $this->assertEquals("Utopia\Validator\URL", \get_class($vaidator->getValidators()[1])); - } -} diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php deleted file mode 100644 index 8ef1ee5..0000000 --- a/tests/Validator/MultipleTest.php +++ /dev/null @@ -1,35 +0,0 @@ -validator = new Multiple([new Text(20), new URL()], Multiple::TYPE_STRING); - } - - public function testIsValid() - { - $this->assertEquals('string', $this->validator->getType()); - $this->assertEquals("1. Value must be a valid string and at least 1 chars and no longer than 20 chars \n2. Value must be a valid URL \n", $this->validator->getDescription()); - - // Valid URL but invalid text length - $this->assertFalse($this->validator->isValid('http://example.com/very-long-url')); - - // Valid text within length, but invalid URL - $this->assertFalse($this->validator->isValid('hello world')); - - // Both conditions satisfied - $this->assertTrue($this->validator->isValid('http://example.com')); - $this->assertTrue($this->validator->isValid('https://google.com')); - - // Neither condition satisfied - $this->assertFalse($this->validator->isValid('example.com/hello-world')); - $this->assertFalse($this->validator->isValid('')); - } -} diff --git a/tests/Validator/NullableTest.php b/tests/Validator/NullableTest.php deleted file mode 100755 index fa15c53..0000000 --- a/tests/Validator/NullableTest.php +++ /dev/null @@ -1,22 +0,0 @@ -assertTrue($validator->isValid('text')); - $this->assertTrue($validator->isValid(null)); - $this->assertFalse($validator->isValid(123)); - } - - public function testCanReturnValidator(): void - { - $validator = new Nullable(new Text(0)); - $this->assertTrue($validator->getValidator() instanceof Text); - } -} diff --git a/tests/Validator/NumericTest.php b/tests/Validator/NumericTest.php deleted file mode 100755 index d90be6b..0000000 --- a/tests/Validator/NumericTest.php +++ /dev/null @@ -1,24 +0,0 @@ -assertTrue($numeric->isValid('42')); - $this->assertTrue($numeric->isValid(1337)); - $this->assertTrue($numeric->isValid(0x539)); - $this->assertTrue($numeric->isValid(02471)); - $this->assertTrue($numeric->isValid(1337e0)); - $this->assertTrue($numeric->isValid(9.1)); - $this->assertFalse($numeric->isValid('not numeric')); - $this->assertFalse($numeric->isValid([])); - $this->assertFalse($numeric->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_MIXED, $numeric->getType()); - } -} diff --git a/tests/Validator/RangeTest.php b/tests/Validator/RangeTest.php deleted file mode 100755 index 2bd1ee6..0000000 --- a/tests/Validator/RangeTest.php +++ /dev/null @@ -1,60 +0,0 @@ -assertTrue($range->isValid(0)); - $this->assertTrue($range->isValid(1)); - $this->assertTrue($range->isValid(4)); - $this->assertTrue($range->isValid(5)); - $this->assertTrue($range->isValid('5')); - $this->assertFalse($range->isValid('1.5')); - $this->assertFalse($range->isValid(6)); - $this->assertFalse($range->isValid(-1)); - $this->assertEquals(0, $range->getMin()); - $this->assertEquals(5, $range->getMax()); - $this->assertFalse($range->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_INTEGER, $range->getFormat()); - $this->assertEquals(\Utopia\Validator::TYPE_INTEGER, $range->getType()); - } - - public function testCanValidateFloatRange() - { - $range = new Range(0, 1, \Utopia\Validator::TYPE_FLOAT); - - $this->assertTrue($range->isValid(0.0)); - $this->assertTrue($range->isValid(1.0)); - $this->assertTrue($range->isValid(0.5)); - $this->assertTrue($range->isValid('0.5')); - $this->assertTrue($range->isValid('0.6')); - $this->assertFalse($range->isValid(4)); - $this->assertFalse($range->isValid(1.5)); - $this->assertFalse($range->isValid(-1)); - $this->assertEquals(0, $range->getMin()); - $this->assertEquals(1, $range->getMax()); - $this->assertFalse($range->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_FLOAT, $range->getFormat()); - $this->assertEquals(\Utopia\Validator::TYPE_FLOAT, $range->getType(), \Utopia\Validator::TYPE_FLOAT); - } - - public function canValidateInfinityRange() - { - $integer = new Range(5, INF, \Utopia\Validator::TYPE_INTEGER); - $float = new Range(-INF, 45.6, \Utopia\Validator::TYPE_FLOAT); - - $this->assertTrue($integer->isValid(25)); - $this->assertFalse($integer->isValid(3)); - $this->assertTrue($integer->isValid(INF)); - $this->assertTrue($float->isValid(32.1)); - $this->assertFalse($float->isValid(97.6)); - $this->assertTrue($float->isValid(-INF)); - } -} diff --git a/tests/Validator/TextTest.php b/tests/Validator/TextTest.php deleted file mode 100755 index cb98704..0000000 --- a/tests/Validator/TextTest.php +++ /dev/null @@ -1,88 +0,0 @@ -assertTrue($validator->isValid('text')); - $this->assertTrue($validator->isValid('7')); - $this->assertTrue($validator->isValid('7.9')); - $this->assertTrue($validator->isValid('["seven"]')); - $this->assertFalse($validator->isValid(['seven'])); - $this->assertFalse($validator->isValid(['seven', 8, 9.0])); - $this->assertFalse($validator->isValid(false)); - $this->assertFalse($validator->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_STRING, $validator->getType()); - } - - public function testCanValidateBoundaries(): void - { - $validator = new Text(5); - $this->assertTrue($validator->isValid('hell')); - $this->assertTrue($validator->isValid('hello')); - $this->assertFalse($validator->isValid('hellow')); - $this->assertFalse($validator->isValid('')); - - $validator = new Text(5, 3); - $this->assertTrue($validator->isValid('hel')); - $this->assertTrue($validator->isValid('hell')); - $this->assertTrue($validator->isValid('hello')); - $this->assertFalse($validator->isValid('hellow')); - $this->assertFalse($validator->isValid('he')); - $this->assertFalse($validator->isValid('h')); - } - - public function testCanValidateTextWithAllowList(): void - { - // Test lowercase alphabet - $validator = new Text(100, allowList: Text::ALPHABET_LOWER); - $this->assertFalse($validator->isArray()); - $this->assertTrue($validator->isValid('qwertzuiopasdfghjklyxcvbnm')); - $this->assertTrue($validator->isValid('hello')); - $this->assertTrue($validator->isValid('world')); - $this->assertFalse($validator->isValid('hello world')); - $this->assertFalse($validator->isValid('Hello')); - $this->assertFalse($validator->isValid('worlD')); - $this->assertFalse($validator->isValid('hello123')); - - // Test uppercase alphabet - $validator = new Text(100, allowList: Text::ALPHABET_UPPER); - $this->assertFalse($validator->isArray()); - $this->assertTrue($validator->isValid('QWERTZUIOPASDFGHJKLYXCVBNM')); - $this->assertTrue($validator->isValid('HELLO')); - $this->assertTrue($validator->isValid('WORLD')); - $this->assertFalse($validator->isValid('HELLO WORLD')); - $this->assertFalse($validator->isValid('hELLO')); - $this->assertFalse($validator->isValid('WORLd')); - $this->assertFalse($validator->isValid('HELLO123')); - - // Test numbers - $validator = new Text(100, allowList: Text::NUMBERS); - $this->assertFalse($validator->isArray()); - $this->assertTrue($validator->isValid('1234567890')); - $this->assertTrue($validator->isValid('123')); - $this->assertFalse($validator->isValid('123 456')); - $this->assertFalse($validator->isValid('hello123')); - - // Test combination of allowLists - $validator = new Text(100, allowList: [ - ...Text::ALPHABET_LOWER, - ...Text::ALPHABET_UPPER, - ...Text::NUMBERS, - ]); - - $this->assertFalse($validator->isArray()); - $this->assertTrue($validator->isValid('1234567890')); - $this->assertTrue($validator->isValid('qwertzuiopasdfghjklyxcvbnm')); - $this->assertTrue($validator->isValid('QWERTZUIOPASDFGHJKLYXCVBNM')); - $this->assertTrue($validator->isValid('QWERTZUIOPASDFGHJKLYXCVBNMqwertzuiopasdfghjklyxcvbnm1234567890')); - $this->assertFalse($validator->isValid('hello-world')); - $this->assertFalse($validator->isValid('hello_world')); - $this->assertFalse($validator->isValid('hello/world')); - } -} diff --git a/tests/Validator/URLTest.php b/tests/Validator/URLTest.php deleted file mode 100644 index 46f024c..0000000 --- a/tests/Validator/URLTest.php +++ /dev/null @@ -1,55 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class URLTest extends TestCase -{ - protected ?URL $url; - - public function setUp(): void - { - $this->url = new URL(); - } - - public function tearDown(): void - { - $this->url = null; - } - - public function testIsValid(): void - { - $this->assertEquals('Value must be a valid URL', $this->url->getDescription()); - $this->assertEquals(true, $this->url->isValid('http://example.com')); - $this->assertEquals(true, $this->url->isValid('https://example.com')); - $this->assertEquals(true, $this->url->isValid('htts://example.com')); // does not validate protocol - $this->assertEquals(false, $this->url->isValid('example.com')); // though, requires some kind of protocol - $this->assertEquals(false, $this->url->isValid('http:/example.com')); - $this->assertEquals(true, $this->url->isValid('http://exa-mple.com')); - $this->assertEquals(false, $this->url->isValid('htt@s://example.com')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E')); - } - - public function testIsValidAllowedSchemes(): void - { - $this->url = new URL(['http', 'https']); - $this->assertEquals('Value must be a valid URL with following schemes (http, https)', $this->url->getDescription()); - $this->assertEquals(true, $this->url->isValid('http://example.com')); - $this->assertEquals(true, $this->url->isValid('https://example.com')); - $this->assertEquals(false, $this->url->isValid('gopher://www.example.com')); - } -} diff --git a/tests/Validator/WhiteListTest.php b/tests/Validator/WhiteListTest.php deleted file mode 100755 index 9d88f75..0000000 --- a/tests/Validator/WhiteListTest.php +++ /dev/null @@ -1,58 +0,0 @@ -assertTrue($whiteList->isValid(3)); - $this->assertTrue($whiteList->isValid(4)); - $this->assertTrue($whiteList->isValid('string1')); - $this->assertTrue($whiteList->isValid('string2')); - - $this->assertFalse($whiteList->isValid('string3')); - $this->assertFalse($whiteList->isValid('STRING1')); - $this->assertFalse($whiteList->isValid('strIng1')); - $this->assertFalse($whiteList->isValid('3')); - $this->assertFalse($whiteList->isValid(5)); - $this->assertFalse($whiteList->isArray()); - $this->assertEquals($whiteList->getList(), ['string1', 'string2', 3, 4]); - $this->assertEquals(\Utopia\Validator::TYPE_STRING, $whiteList->getType()); - } - - public function testCanValidateLoosely(): void - { - $whiteList = new WhiteList(['string1', 'string2', 3, 4]); - - $this->assertTrue($whiteList->isValid(3)); - $this->assertTrue($whiteList->isValid(4)); - $this->assertTrue($whiteList->isValid('string1')); - $this->assertTrue($whiteList->isValid('string2')); - $this->assertTrue($whiteList->isValid('STRING1')); - $this->assertTrue($whiteList->isValid('strIng1')); - $this->assertTrue($whiteList->isValid('3')); - $this->assertTrue($whiteList->isValid('4')); - $this->assertFalse($whiteList->isValid('string3')); - $this->assertFalse($whiteList->isValid(5)); - $this->assertEquals($whiteList->getList(), ['string1', 'string2', 3, 4]); - - $whiteList = new WhiteList(['STRING1', 'STRING2', 3, 4]); - - $this->assertTrue($whiteList->isValid(3)); - $this->assertTrue($whiteList->isValid(4)); - $this->assertTrue($whiteList->isValid('string1')); - $this->assertTrue($whiteList->isValid('string2')); - $this->assertTrue($whiteList->isValid('STRING1')); - $this->assertTrue($whiteList->isValid('strIng1')); - $this->assertTrue($whiteList->isValid('3')); - $this->assertTrue($whiteList->isValid('4')); - $this->assertFalse($whiteList->isValid('string3')); - $this->assertFalse($whiteList->isValid(5)); - $this->assertEquals($whiteList->getList(), ['string1', 'string2', 3, 4]); - } -} diff --git a/tests/Validator/WildcardTest.php b/tests/Validator/WildcardTest.php deleted file mode 100644 index b79c358..0000000 --- a/tests/Validator/WildcardTest.php +++ /dev/null @@ -1,21 +0,0 @@ -assertTrue($validator->isValid([0 => 'string', 1 => 'string'])); - $this->assertTrue($validator->isValid('')); - $this->assertTrue($validator->isValid([])); - $this->assertTrue($validator->isValid(1)); - $this->assertTrue($validator->isValid(true)); - $this->assertTrue($validator->isValid(false)); - $this->assertFalse($validator->isArray()); - $this->assertEquals(\Utopia\Validator::TYPE_STRING, $validator->getType()); - } -}