Skip to content

Commit b2c6586

Browse files
authored
Merge pull request #44 from editor-js/choose-allowed-null
Choose allowed null
2 parents c1b79ce + f32c681 commit b2c6586

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

EditorJS/BlockHandler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ 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 &&
143+
isset($rule['allow_null']) && $rule['allow_null'] === true && $value === null) {
144+
continue;
145+
}
146+
139147
/**
140148
* Validate element types
141149
*/

tests/TypeTest.php

Lines changed: 25 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,28 @@ 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 testAllowedNullNotRequired()
72+
{
73+
new EditorJS('{"blocks":[{"type":"test","data":{"int_test": null}}]}', $this->configuration);
74+
}
75+
76+
public function testDisallowedNullNotRequired()
77+
{
78+
$callable = function () {
79+
new EditorJS('{"blocks":[{"type":"test","data":{"string_test": null}}]}', $this->configuration);
80+
};
81+
82+
$this->assertException($callable, EditorJSException::class, null, 'string_test\' with value `` must be string');
83+
}
84+
85+
public function testNullRequired()
86+
{
87+
new EditorJS('{"blocks":[{"type":"test","data":{"string_test": "qwe"}}]}', file_get_contents(TypeTest::CONFIGURATION_FILE_REQUIRED));
88+
89+
$callable = function () {
90+
new EditorJS('{"blocks":[{"type":"test","data":{"string_test": null}}]}', file_get_contents(TypeTest::CONFIGURATION_FILE_REQUIRED));
91+
};
92+
$this->assertException($callable, EditorJSException::class, null, 'Not found required param `string_test`');
93+
}
6994
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"tools": {
3+
"test": {
4+
"string_test": {
5+
"type": "string",
6+
"required": true,
7+
"allow_null": true
8+
}
9+
}
10+
}
11+
}

tests/samples/type-test-config.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
},
88
"int_test": {
99
"type": "integer",
10-
"required": false
10+
"required": false,
11+
"allow_null": true
1112
},
1213
"string_test": {
1314
"type": "string",
14-
"required": false
15+
"required": false,
16+
"allow_null": false
1517
}
1618
}
1719
}

0 commit comments

Comments
 (0)