Skip to content

Assert with optinal description #7 - #8

Merged
dg merged 3 commits into
nette:masterfrom
integer:assert-with-description
Sep 15, 2019
Merged

Assert with optinal description #7#8
dg merged 3 commits into
nette:masterfrom
integer:assert-with-description

Conversation

@integer

@integer integer commented Jul 11, 2019

Copy link
Copy Markdown
Contributor

With this PR you can add description to your asserts and this description will be printed in error messages.

$schema = Expect::string()
        ->assert('ctype_digit', 'Is number')
        ->assert(function ($s) { return strlen($s) >= 3; }, 'Minimal lenght');

@integer
integer force-pushed the assert-with-description branch 3 times, most recently from 9e5851a to fb99032 Compare July 11, 2019 15:05
@integer
integer force-pushed the assert-with-description branch from fb99032 to 4fb7680 Compare July 11, 2019 15:31
@dg

dg commented Jul 12, 2019

Copy link
Copy Markdown
Member

It could be implemented in an alternative way, but I don't know what's better.

$schema = Expect::arrayOf('string')
	->assert(function ($v) { 
		if (count($v) % 2 !== 0) { // count must be even number
			throw new AssertException('Even items in array');
		}
	}); 

@mabar

mabar commented Jul 12, 2019

Copy link
Copy Markdown
Contributor

I would like exception, it allows non-statical error messages

@dg

dg commented Jul 12, 2019

Copy link
Copy Markdown
Member

After experience with forms and addRule() I would prefer exceptions today.

@integer

integer commented Jul 12, 2019

Copy link
Copy Markdown
Contributor Author

Exceptions requires more writing, but you can prepare better message. I will try exceptions in my project. Thank you for hint.

@dg
dg merged commit 60ba751 into nette:master Sep 15, 2019
dg pushed a commit that referenced this pull request Sep 15, 2019
dg pushed a commit that referenced this pull request Sep 15, 2019
dg pushed a commit that referenced this pull request Sep 24, 2019
dg pushed a commit that referenced this pull request Oct 31, 2019
dg pushed a commit that referenced this pull request Oct 31, 2019
dg pushed a commit that referenced this pull request Jan 6, 2020
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.

3 participants