Skip to content

Commit f32c681

Browse files
committed
Introduce allow_null option
1 parent 222a924 commit f32c681

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

EditorJS/BlockHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ private function validate($rules, $blockData)
139139
/**
140140
* Do not check element type if it is not required and null
141141
*/
142-
if (isset($rule['required']) && $rule['required'] === false && $value === null) {
142+
if (isset($rule['required']) && $rule['required'] === false &&
143+
isset($rule['allow_null']) && $rule['allow_null'] === true && $value === null) {
143144
continue;
144145
}
145146

tests/TypeTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,18 @@ public function testStringFailed()
6868
$this->assertException($callable, EditorJSException::class, null, 'Option \'string_test\' with value `17` must be string');
6969
}
7070

71-
public function testNullNotRequired()
71+
public function testAllowedNullNotRequired()
7272
{
73-
new EditorJS('{"blocks":[{"type":"test","data":{"string_test": null}}]}', $this->configuration);
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');
7483
}
7584

7685
public function testNullRequired()

tests/samples/type-test-config-required.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"test": {
44
"string_test": {
55
"type": "string",
6-
"required": true
6+
"required": true,
7+
"allow_null": true
78
}
89
}
910
}

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)