<commit_before><div class="ui masthead minimum vertical segment">
  <div class="ui container">
    <div class="introduction">
      <h1 class="ui header">Ember Model Validator Usage</h1>
    </div>
  </div>
</div>

<div class="ui main container">
  <h2 class="ui header">One Mixin to <i> Validate Them All</i></h2>

  <div class="no example">
    <p>
      <strong>Ember-model-validator</strong> provides a mixin to be included in your models for adding validation support. This mixin can be imported from your app's namespace (e.g. ../mixins/model-validator in your models).
    </p>

    <p>
      By including Ember-model-validator's mixin into your model, you will get a <code class="ui label">validate</code> function available. it is a synchronous function which returns either <i>true</i> or <i>false</i>.
    </p>
    {{! template-lint-disable block-indentation }}
    {{#code-block language="javascript"}}
if (myModel.validate()) {
  // ...
} else {
  console.log(myModel.get("errors"));
}
    {{/code-block}}

    <p>You will also have a <code class="ui label">clearErrors</code> function available. Which will clear the errors of your model.</p>

    <h2 class="ui header">Customising</h2>

    <h4>Validate Options</h4>
    <ul>
      <li><code class="ui label">only</code> Specify the attributes you want to validate.</li>
      <li><code class="ui label">except</code> Prevent certain attribute to be validated.</li>
      <li><code class="ui label">addErrors</code> (Default <strong>true</strong>). Only return <i>true</i> or <i>false</i> but do not add any errors to the model.</li>
    </ul>

    <p>
      You can pass an option hash for excluding or forcing certain attributes to be validated, and to prevent errors to be added.
    </p>
    {{#code-block language="javascript"}}
// Using `except`
myModel.validate({ except: ["name", "cellphone"] });

// Using `only`
myModel.validate({ only: ["favoriteColor", "mainstreamCode"] });

// Using `addErrors`
myModel.validate({ addErrors: false });
// This will validate the model but won't add any errors.
    {{/code-block}}

  </div>

  <h3 class="ui header">Conditional Validators</h3>
  <p>
    You can pass an<code class="ui label">if</code> option to any validator. This will make the validation to <strong>only</strong> run when the function passed returns true. <code class="ui label">function(key,value, _this){...}</code>
  </p>
  {{! template-lint-disable block-indentation }}
  {{#code-block language="javascript"}}
import Model from "ember-data/model";
import attr from "ember-data/attr";
import Validator from "../mixins/model-validator";

export default Model.extend(Validator, {
  images: attr(""),
  type: attr("string"),

  validations: {
    images: {
      presence: {
        if: function(key, value, _this) {
          return "gallery" === _this.get("type");
        }
      }
    }
  }
});
  {{/code-block}}
  <h3 class="ui header">Using function to generate custom message</h3><commit_msg>add spefici validation to dummy<commit_after><div class="ui masthead minimum vertical segment">
  <div class="ui container">
    <div class="introduction">
      <h1 class="ui header">Ember Model Validator Usage</h1>
    </div>
  </div>
</div>

<div class="ui main container">
  <h2 class="ui header">One Mixin to <i> Validate Them All</i></h2>

  <div class="no example">
    <p>
      <strong>Ember-model-validator</strong> provides a mixin to be included in your models for adding validation support. This mixin can be imported from your app's namespace (e.g. ../mixins/model-validator in your models).
    </p>

    <p>
      By including Ember-model-validator's mixin into your model, you will get a <code class="ui label">validate</code> function available. it is a synchronous function which returns either <i>true</i> or <i>false</i>.
    </p>
    {{! template-lint-disable block-indentation }}
    {{#code-block language="javascript"}}
if (myModel.validate()) {
  // ...
} else {
  console.log(myModel.get("errors"));
}
    {{/code-block}}

    <p>You will also have a <code class="ui label">clearErrors</code> function available. Which will clear the errors of your model.</p>

    <h2 class="ui header">Customising</h2>

    <h4>Validate Options</h4>
    <ul>
      <li><code class="ui label">only</code> Specify the attributes you want to validate.</li>
      <li><code class="ui label">except</code> Prevent certain attribute to be validated.</li>
      <li><code class="ui label">addErrors</code> (Default <strong>true</strong>). Only return <i>true</i> or <i>false</i> but do not add any errors to the model.</li>
    </ul>

    <p>
      You can pass an option hash for excluding or forcing certain attributes to be validated, and to prevent errors to be added.
    </p>
    {{#code-block language="javascript"}}
// Using `except`
myModel.validate({ except: ["name", "cellphone"] });

// Using `only`
myModel.validate({ only: ["favoriteColor", "mainstreamCode"] });

// Using `addErrors`
myModel.validate({ addErrors: false });
// This will validate the model but won't add any errors.
    {{/code-block}}

    <p>
      To target specific validations when using <code class="ui label">except / only</code>, pass the validations' names along the attribute's name:
    </p>
    {{#code-block language="javascript"}}
// This runs all validations, except name's presence and length validations and
// any email validations.
// Other name validations are still run.
myModel.validate({ except: ['name:presence,length', 'email'] });
    {{/code-block}}
  </div>

  <h3 class="ui header">Conditional Validators</h3>
  <p>
    You can pass an<code class="ui label">if</code> option to any validator. This will make the validation to <strong>only</strong> run when the function passed returns true. <code class="ui label">function(key,value, _this){...}</code>
  </p>
  {{! template-lint-disable block-indentation }}
  {{#code-block language="javascript"}}
import Model from "ember-data/model";
import attr from "ember-data/attr";
import Validator from "../mixins/model-validator";

export default Model.extend(Validator, {
  images: attr(""),
  type: attr("string"),

  validations: {
    images: {
      presence: {
        if: function(key, value, _this) {
          return "gallery" === _this.get("type");
        }
      }
    }
  }
});
  {{/code-block}}
  <h3 class="ui header">Using function to generate custom message</h3>
{'fraction_non_alphanumeric': 0.1544576059850374, 'fraction_numerical': 0.003740648379052369, 'mean_word_length': 3.6365606936416186, 'pattern_counts': {'":': 0, '<': 145, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7132261', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1937, 'n_words': 646}