Skip to content

Add a maxLength rule for text columns#335

Merged
markstory merged 1 commit intocakephp:masterfrom
dmromanov:feature/text-maxlength-validator
Nov 24, 2017
Merged

Add a maxLength rule for text columns#335
markstory merged 1 commit intocakephp:masterfrom
dmromanov:feature/text-maxlength-validator

Conversation

@dmromanov
Copy link
Copy Markdown
Contributor

@dmromanov dmromanov commented May 16, 2017

#284
This pull-request depends on cakephp/cakephp/issues/11295 .

@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch from c3c128d to c50a2e6 Compare May 16, 2017 23:02
@codecov-io
Copy link
Copy Markdown

codecov-io commented May 16, 2017

Codecov Report

Merging #335 into master will increase coverage by 0.07%.
The diff coverage is 82.05%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #335      +/-   ##
============================================
+ Coverage     92.12%   92.19%   +0.07%     
- Complexity      664      672       +8     
============================================
  Files            25       25              
  Lines          2184     2205      +21     
============================================
+ Hits           2012     2033      +21     
  Misses          172      172
Impacted Files Coverage Δ Complexity Δ
src/View/Helper/BakeHelper.php 100% <100%> (ø) 65 <2> (+6) ⬆️
src/Shell/Task/ModelTask.php 95.02% <69.56%> (+0.06%) 156 <0> (+2) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3c3c512...f60967d. Read the comment docs.

@markstory markstory added this to the 1.3.x milestone May 17, 2017
Copy link
Copy Markdown
Member

@markstory markstory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just wondering if we should fix the possible args issue now.

if ($rule['rule'] && !isset($rule['provider']) && !isset($rule['args'])):
$validationMethods[] = sprintf("->%s('%s')", $rule['rule'], $field);
elseif ($rule['rule'] && !isset($rule['provider'])):
$validationMethods[] = sprintf("->%s('%s', %s)", $rule['rule'], $field, implode(', ', $rule['args']));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this have issues if the arguments are strings?

@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch 3 times, most recently from b94cf73 to 208a4ca Compare May 18, 2017 18:26
@dmromanov
Copy link
Copy Markdown
Contributor Author

@markstory String values are now wrapped with quotes.

Tests are failing because each DBMS has different length limits for string-type fields.

public function escapeArguments($args)
{
return array_map(function ($v) {
if (!is_numeric($v)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably escape ' inside strings 😄

@markstory
Copy link
Copy Markdown
Member

Tests will need to be passing before we can get this merged.

@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch 3 times, most recently from bb3761c to 68347e8 Compare May 19, 2017 11:28
@dmromanov
Copy link
Copy Markdown
Contributor Author

Ok, I'll change the code.

@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch 2 times, most recently from e1c5e08 to 68347e8 Compare June 5, 2017 09:28
@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch from 68347e8 to afe3e12 Compare June 28, 2017 21:49
@ADmad
Copy link
Copy Markdown
Member

ADmad commented Jul 1, 2017

Tests still failing.

@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch 3 times, most recently from c5e35b7 to 7b6b8f6 Compare October 6, 2017 16:10
@dmromanov
Copy link
Copy Markdown
Contributor Author

My changes fails to pass validation on some DBMS, because of some inconsistency between them. E.g. in SQLite, text fields can have infinite length, while in MySQL, it's not.

To work around this, I am thinking about two possible approaches:

  1. To mock a table schema and build it manually in tests, where a table schema is required.
  2. Make DBMS-specific tests, that would be marked as skipped, if they are run under different DMBS.

@ADmad @markstory Which one is better? Or maybe you can suggest a better solutions?

@markstory
Copy link
Copy Markdown
Member

Could we test this validator against varchar columns instead of text ones? Those should have more stable lengths across different db vendors.

@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch 4 times, most recently from 7d92a67 to d315b22 Compare October 7, 2017 17:24
@dmromanov
Copy link
Copy Markdown
Contributor Author

Depends on cakephp/cakephp/issues/11295

@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch from d315b22 to 900f52c Compare October 7, 2017 19:27
*
* Generates a tree of data for use testing the tree behavior
*/
class NumberTreesFixture extends TestFixture
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this fixture?

Copy link
Copy Markdown
Contributor Author

@dmromanov dmromanov Oct 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it's a copy of CakePHP's core NumberTreesFixture, but with a change: "name" column now has a length attribute. Since the length attribute is not needed in CakePHP core, and is not covered with tests, it is a subject to change in the future. So I decided to copy it into the repository.
This fixture is used in ModelTaskTest.php::testGetValidationTree().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see where the fixture was used. My mistake.

@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch 3 times, most recently from 7719309 to 8675cf5 Compare October 9, 2017 12:13
@markstory markstory modified the milestones: 1.3.x, 1.4.x Oct 20, 2017
@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch from 8675cf5 to f10333c Compare October 31, 2017 11:12
@markstory markstory modified the milestones: 1.4.x, 1.5.x Nov 22, 2017
@dmromanov dmromanov force-pushed the feature/text-maxlength-validator branch from f10333c to f60967d Compare November 23, 2017 12:34
@dmromanov
Copy link
Copy Markdown
Contributor Author

Now with TWIG support.

@markstory markstory merged commit 09de285 into cakephp:master Nov 24, 2017
@dmromanov dmromanov deleted the feature/text-maxlength-validator branch August 19, 2018 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants