Skip to content

Commit 222a924

Browse files
committed
Allow null as typed value for any not required field
1 parent 21f355a commit 222a924

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

EditorJS/BlockHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ private function validate($rules, $blockData)
136136
continue;
137137
}
138138

139+
/**
140+
* Do not check element type if it is not required and null
141+
*/
142+
if (isset($rule['required']) && $rule['required'] === false && $value === null) {
143+
continue;
144+
}
145+
139146
/**
140147
* Validate element types
141148
*/

tests/TypeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class TypeTest extends TestCase
1212
{
1313
const CONFIGURATION_FILE = TESTS_DIR . "/samples/type-test-config.json";
14+
const CONFIGURATION_FILE_REQUIRED = TESTS_DIR . "/samples/type-test-config-required.json";
1415

1516
/**
1617
* Sample configuration
@@ -66,4 +67,19 @@ public function testStringFailed()
6667

6768
$this->assertException($callable, EditorJSException::class, null, 'Option \'string_test\' with value `17` must be string');
6869
}
70+
71+
public function testNullNotRequired()
72+
{
73+
new EditorJS('{"blocks":[{"type":"test","data":{"string_test": null}}]}', $this->configuration);
74+
}
75+
76+
public function testNullRequired()
77+
{
78+
new EditorJS('{"blocks":[{"type":"test","data":{"string_test": "qwe"}}]}', file_get_contents(TypeTest::CONFIGURATION_FILE_REQUIRED));
79+
80+
$callable = function () {
81+
new EditorJS('{"blocks":[{"type":"test","data":{"string_test": null}}]}', file_get_contents(TypeTest::CONFIGURATION_FILE_REQUIRED));
82+
};
83+
$this->assertException($callable, EditorJSException::class, null, 'Not found required param `string_test`');
84+
}
6985
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"tools": {
3+
"test": {
4+
"string_test": {
5+
"type": "string",
6+
"required": true
7+
}
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)