Skip to content

Commit

Permalink
Merge pull request #41 from offirgolan/dependent-keys
Browse files Browse the repository at this point in the history
dependentKey common validator options
  • Loading branch information
offirgolan committed Sep 21, 2015
2 parents a0bf19d + a194542 commit 545c3d2
Show file tree
Hide file tree
Showing 7 changed files with 606 additions and 415 deletions.
7 changes: 7 additions & 0 deletions addon/utils/validations-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
isNone,
guidFor,
isEmpty,
isArray,
computed,
makeArray,
canInvoke,
Expand Down Expand Up @@ -197,6 +198,12 @@ function getCPDependentKeysFor(attribute, validations) {
} else if (type === 'collection' && (options === true || options.collection === true)) {
dependentKeys.push(`_model.${attribute}.[]`);
}

if(isArray(options.dependentKeys)) {
options.dependentKeys.forEach(k => {
dependentKeys.push(`_model.${k}`);
});
}
});

return dependentKeys.uniq();
Expand Down
16 changes: 16 additions & 0 deletions docs/docs/validators/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ validator('date', {
// 'Date of birth can't be blank'
```

## dependentKeys
A list of other model specific dependents for you validator.

```javascript
// Examples
validator('has-friends', {
dependentKeys: ['friends.[]']
})
validator('has-valid-friends', {
dependentKeys: ['friends.@each.username']
})
validator('x-validator', {
dependentKeys: ['username', 'email', 'meta.foo.bar']
})
```

## message

This option can take two forms. It can either be a `string` or a `function`. If a string is used, then it will overwrite all error message types for the specified validator. Some messages are passed values such as the `confirmation` validator and can be accessed via `%@`. To overwrite this, we can simply do
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/public/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,5 @@ <h2 id="basic-usage">Basic Usage</h2>

<!--
MkDocs version : 0.14.0
Build Date UTC : 2015-09-15 19:01:53.979975
Build Date UTC : 2015-09-19 22:32:58.325787
-->
7 changes: 6 additions & 1 deletion tests/dummy/public/docs/mkdocs/search_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,19 @@
},
{
"location": "/validators/common/index.html",
"text": "attributeDescription\n\n\nA descriptor for your attribute used in generating the error messages. Defaults to \nThis field'\n\n\n// Examples\nvalidator('date', {\n attributeDescription: 'Date of birth'\n})\n// If validation is run and the attribute is empty, the error returned will be:\n// 'Date of birth can't be blank'\n\n\n\n\nmessage\n\n\nThis option can take two forms. It can either be a \nstring\n or a \nfunction\n. If a string is used, then it will overwrite all error message types for the specified validator. Some messages are passed values such as the \nconfirmation\n validator and can be accessed via \n%@\n. To overwrite this, we can simply do\n\n\n// Example: String\nvalidator('confirmation', {\n message: 'does not match %@. What are you even thinking?!'\n})\n\n\n\n\nWe can pass a \nfunction\n into our message option for even more customization capabilities.\n\n\n// Example: Function\nvalidator('date', {\n message: function(type, options, value) {\n if (type === 'before') {\n return 'should really be before %@';\n }\n if (type === 'after') {\n return 'should really be after %@';\n }\n }\n})\n\n\n\n\nThe message function is given the following arguments:\n\n\n\n\ntype\n (\nString\n): The error message type\n\n\noptions\n (\nObject\n): The validator options that were defined in the model\n\n\nvalue\n: The current value being evaluated\n\n\n\n\nThe return value must be a \nstring\n. If nothing is returned (\nundefined\n), defaults to the default error message of the specified type.\n\n\nWithin this function, the context is set to that of the current validator. This gives you access to the model, defaultMessages, options and more.",
"text": "attributeDescription\n\n\nA descriptor for your attribute used in generating the error messages. Defaults to \nThis field'\n\n\n// Examples\nvalidator('date', {\n attributeDescription: 'Date of birth'\n})\n// If validation is run and the attribute is empty, the error returned will be:\n// 'Date of birth can't be blank'\n\n\n\n\ndependentKeys\n\n\nA list of other model specific dependents for you validator.\n\n\n// Examples\nvalidator('has-friends', {\n dependentKeys: ['friends.[]']\n})\nvalidator('has-valid-friends', {\n dependentKeys: ['friends.@each.username']\n})\nvalidator('x-validator', {\n dependentKeys: ['username', 'email', 'meta.foo.bar']\n})\n\n\n\n\nmessage\n\n\nThis option can take two forms. It can either be a \nstring\n or a \nfunction\n. If a string is used, then it will overwrite all error message types for the specified validator. Some messages are passed values such as the \nconfirmation\n validator and can be accessed via \n%@\n. To overwrite this, we can simply do\n\n\n// Example: String\nvalidator('confirmation', {\n message: 'does not match %@. What are you even thinking?!'\n})\n\n\n\n\nWe can pass a \nfunction\n into our message option for even more customization capabilities.\n\n\n// Example: Function\nvalidator('date', {\n message: function(type, options, value) {\n if (type === 'before') {\n return 'should really be before %@';\n }\n if (type === 'after') {\n return 'should really be after %@';\n }\n }\n})\n\n\n\n\nThe message function is given the following arguments:\n\n\n\n\ntype\n (\nString\n): The error message type\n\n\noptions\n (\nObject\n): The validator options that were defined in the model\n\n\nvalue\n: The current value being evaluated\n\n\n\n\nThe return value must be a \nstring\n. If nothing is returned (\nundefined\n), defaults to the default error message of the specified type.\n\n\nWithin this function, the context is set to that of the current validator. This gives you access to the model, defaultMessages, options and more.",
"title": "Common Options"
},
{
"location": "/validators/common/index.html#attributedescription",
"text": "A descriptor for your attribute used in generating the error messages. Defaults to This field' // Examples\nvalidator('date', {\n attributeDescription: 'Date of birth'\n})\n// If validation is run and the attribute is empty, the error returned will be:\n// 'Date of birth can't be blank'",
"title": "attributeDescription"
},
{
"location": "/validators/common/index.html#dependentkeys",
"text": "A list of other model specific dependents for you validator. // Examples\nvalidator('has-friends', {\n dependentKeys: ['friends.[]']\n})\nvalidator('has-valid-friends', {\n dependentKeys: ['friends.@each.username']\n})\nvalidator('x-validator', {\n dependentKeys: ['username', 'email', 'meta.foo.bar']\n})",
"title": "dependentKeys"
},
{
"location": "/validators/common/index.html#message",
"text": "This option can take two forms. It can either be a string or a function . If a string is used, then it will overwrite all error message types for the specified validator. Some messages are passed values such as the confirmation validator and can be accessed via %@ . To overwrite this, we can simply do // Example: String\nvalidator('confirmation', {\n message: 'does not match %@. What are you even thinking?!'\n}) We can pass a function into our message option for even more customization capabilities. // Example: Function\nvalidator('date', {\n message: function(type, options, value) {\n if (type === 'before') {\n return 'should really be before %@';\n }\n if (type === 'after') {\n return 'should really be after %@';\n }\n }\n}) The message function is given the following arguments: type ( String ): The error message type options ( Object ): The validator options that were defined in the model value : The current value being evaluated The return value must be a string . If nothing is returned ( undefined ), defaults to the default error message of the specified type. Within this function, the context is set to that of the current validator. This gives you access to the model, defaultMessages, options and more.",
Expand Down
34 changes: 17 additions & 17 deletions tests/dummy/public/docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

<url>
<loc>None/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>



<url>
<loc>None/validating/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>



<url>
<loc>None/templating/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

Expand All @@ -29,85 +29,85 @@

<url>
<loc>None/validators/common/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/presence/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/exclusion/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/length/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/date/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/format/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/dependent/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/confirmation/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/collection/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/belongs-to/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/has-many/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/function/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/custom/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

<url>
<loc>None/validators/messages/index.html</loc>
<lastmod>2015-09-15</lastmod>
<lastmod>2015-09-19</lastmod>
<changefreq>daily</changefreq>
</url>

Expand Down
17 changes: 17 additions & 0 deletions tests/dummy/public/docs/validators/common/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
<li class="toctree-l3"><a href="#attributedescription">attributeDescription</a></li>


<li class="toctree-l3"><a href="#dependentkeys">dependentKeys</a></li>


<li class="toctree-l3"><a href="#message">message</a></li>


Expand Down Expand Up @@ -237,6 +240,20 @@ <h2 id="attributedescription">attributeDescription</h2>
// 'Date of birth can't be blank'
</code></pre>

<h2 id="dependentkeys">dependentKeys</h2>
<p>A list of other model specific dependents for you validator.</p>
<pre><code class="javascript">// Examples
validator('has-friends', {
dependentKeys: ['friends.[]']
})
validator('has-valid-friends', {
dependentKeys: ['friends.@each.username']
})
validator('x-validator', {
dependentKeys: ['username', 'email', 'meta.foo.bar']
})
</code></pre>

<h2 id="message">message</h2>
<p>This option can take two forms. It can either be a <code>string</code> or a <code>function</code>. If a string is used, then it will overwrite all error message types for the specified validator. Some messages are passed values such as the <code>confirmation</code> validator and can be accessed via <code>%@</code>. To overwrite this, we can simply do</p>
<pre><code class="javascript">// Example: String
Expand Down

0 comments on commit 545c3d2

Please sign in to comment.