Skip to content

[Proposal]: Move type validation into types from other places #1952

@stloyd

Description

@stloyd

Describe the Proposal

While working on #1912 and a bit on #1927, I noticed that validation rules for type and entries are not stored in one place, i.e.:

  • json is in StringTypeChecker::isJson(),
  • type_json->isValid() calls internally: StringTypeChecker::isJson(),
  • UUID is in StringTypeChecker::isUuid() but regex is in Uuid value object,
  • HTML is in an HTMLDocument value object,

This introduces some inconsistency, as UuidEntry::withValue() will work only with Uuid VO, even though UuidEntry accepts strings. This is because TypeDetector uses only strict validation on entries, whereas EntryFactory is based on both TypeDetector & StringTypeChecker.

API Adjustments

Adjust method in Type::isValid() interface and related types to hold the real validation of what they are inside of the types.

Change type methods like StringTypeChecker::isJson() and to something like:

Subject: [PATCH] Move validation to types
---
Index: src/lib/types/src/Flow/Types/Type/Logical/JsonType.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/lib/types/src/Flow/Types/Type/Logical/JsonType.php b/src/lib/types/src/Flow/Types/Type/Logical/JsonType.php
--- a/src/lib/types/src/Flow/Types/Type/Logical/JsonType.php	(revision d18f6af36e318061500eefad0d1f9bb4c86e9164)
+++ b/src/lib/types/src/Flow/Types/Type/Logical/JsonType.php	(date 1762173380920)
@@ -46,7 +46,30 @@
             return false;
         }
 
-        return (new StringTypeChecker($value))->isJson();
+        if ($value === '') {
+            return false;
+        }
+
+        if ('{' !== $value[0] && '[' !== $value[0]) {
+            return false;
+        }
+
+        if (\function_exists('json_validate')) {
+            return \json_validate($value);
+        }
+
+        if (
+            (!\str_starts_with($value, '{') || !\str_ends_with($value, '}'))
+            && (!\str_starts_with($value, '[') || !\str_ends_with($value, ']'))
+        ) {
+            return false;
+        }
+
+        try {
+            return \is_array(\json_decode($value, true, flags: \JSON_THROW_ON_ERROR));
+        } catch (\Exception) {
+            return false;
+        }
     }
 
     public function normalize() : array
Index: src/lib/types/src/Flow/Types/Type/Native/String/StringTypeChecker.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/lib/types/src/Flow/Types/Type/Native/String/StringTypeChecker.php b/src/lib/types/src/Flow/Types/Type/Native/String/StringTypeChecker.php
--- a/src/lib/types/src/Flow/Types/Type/Native/String/StringTypeChecker.php	(revision d18f6af36e318061500eefad0d1f9bb4c86e9164)
+++ b/src/lib/types/src/Flow/Types/Type/Native/String/StringTypeChecker.php	(date 1762173380925)
@@ -5,6 +5,7 @@
 namespace Flow\Types\Type\Native\String;
 
 use Flow\Types\Value\{HTMLDocument, Uuid};
+use function Flow\Types\DSL\type_json;
 
 final readonly class StringTypeChecker
 {
@@ -123,30 +124,7 @@
 
     public function isJson() : bool
     {
-        if ($this->string === '') {
-            return false;
-        }
-
-        if ('{' !== $this->string[0] && '[' !== $this->string[0]) {
-            return false;
-        }
-
-        if (\function_exists('json_validate')) {
-            return \json_validate($this->string);
-        }
-
-        if (
-            (!\str_starts_with($this->string, '{') || !\str_ends_with($this->string, '}'))
-            && (!\str_starts_with($this->string, '[') || !\str_ends_with($this->string, ']'))
-        ) {
-            return false;
-        }
-
-        try {
-            return \is_array(\json_decode($this->string, true, flags: \JSON_THROW_ON_ERROR));
-        } catch (\Exception) {
-            return false;
-        }
+        return type_json()->isValid($this->string);
     }
 
     public function isNull() : bool

Are you intending to also work on proposed change?

Yes

Are you interested in sponsoring this change?

No

Integration & Dependencies

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions