# Default Configuration

Once you’ve configured globally for all the fields, every validation in your project will by default follow that configuration if not provided explicitly.

Here's how you can achieve that,\
In any of your file in the global context:

{% tabs %}
{% tab title="config.ts" %}

```typescript
import valid8r from '@c4code/valid8r';
```

{% endtab %}

{% tab title="config.js" %}

```javascript
import valid8r from '@c4code/valid8r';
```

{% endtab %}
{% endtabs %}

Call `defaults` function in the valid8r and pass an object of all the fields as the first argument that you want to configure:

{% tabs %}
{% tab title="config.ts" %}

<pre class="language-javascript"><code class="lang-javascript"><strong>valid8r.defaults({
</strong>    name: {
        fullNameWithMiddle: true,
        allowNumbers: true,
    },
    email: {
        allowedDomains: ['gmail.com', 'outlook.com'],
        allowDisposables: false,
        customDisposables: ['xyz.com', 'dispmail.com'],
    }
}); // defaults to all the validation in the project
</code></pre>

{% endtab %}

{% tab title="config.js" %}

```javascript
valid8r.defaults({
    name: {
        fullNameWithMiddle: true,
        allowNumbers: true,
    },
    email: {
        allowedDomains: ['gmail.com', 'outlook.com'],
        allowDisposables: false,
        customDisposables: ['xyz.com', 'dispmail.com'],
    }
}); // defaults to all the validation in the project
```

{% endtab %}
{% endtabs %}

Then import the config file to any of the main files for this to work.

```typescript
import './config.ts';
```

That's it! Now all the validation functions used in the project will use this as their default configuration, until they are specifically provided with their own configuration. Such as:

{% tabs %}
{% tab title="Typescript" %}

```typescript
import valid8r from '@c4code/valid8r';

// Your input's value
const input: string = "Jonathan Wilson2";
const [isValid, errors] = valid8r.name(input);

console.log(isValid); // true, as allowNumbers is set to true.

const input2: string = "D4nny Wils0n";
valid8r.name(input, { allowNumbers: false }); 
// throws NameValidationFailed error
```

{% endtab %}

{% tab title="Javascript" %}

```javascript
import valid8r from '@c4code/valid8r';

// Your input's value
const input = "Jonathan Wilson2";
const [isValid, errors] = valid8r.name(input);

console.log(isValid); // true, as allowNumbers is set to true.

const input2 = "D4nny Wils0n";
valid8r.name(input, { allowNumbers: false }); 
// throws NameValidationFailed error
```

{% endtab %}
{% endtabs %}

The configuration is flexible and can be customized according to the projects need.

### Error Messages

If you don't like the default error messages, and want to configure the error messages according to you valid8r provides a simple way to configure every error message efficiently.

{% tabs %}
{% tab title="Typescript" %}

```typescript
import valid8r from '@c4code/valid8r';
```

{% endtab %}

{% tab title="Javascript" %}

```javascript
import valid8r from '@bitbybit/valid8r';
```

{% endtab %}
{% endtabs %}

Call `defaults` function in the valid8r and pass an object that of all the field's error messages your want to configure as the second argument.

{% tabs %}
{% tab title="config.ts" %}

```typescript
valid8r.defaults({
    /* Field configurations */
}, {
    name: {
        onlyLast: "Only last name is allowed!",
        noSpChars: "Please dont input any special characters!",
    },
}); // defaults to all the validation in the project
```

{% endtab %}

{% tab title="config.js" %}

```javascript
valid8r.defaults({
    /* Field configurations */
}, {
    name: {
        onlyLast: "Only last name is allowed!",
        noSpChars: "Please dont input any special characters!",
    },
}); // defaults to all the validation in the project
```

{% endtab %}
{% endtabs %}

However, some fields accept dynamic values that get converts to their relative values when the error is thrown,

{% tabs %}
{% tab title="Typescript" %}

<pre class="language-javascript"><code class="lang-javascript">import valid8r from '@bitbybit/valid8r';

valid8r.defaults({
    /* Field configurations */
}, {
    name: {
        minLen: "Name should atleast be {minLen} char long!"
    },
});

// Your input's value
const input: string = "J";
const [isValid, errors] = valid8r.name(input, {
    minLen: 4,
<strong>    safe: true
</strong>});

if (!isValid) {
    console.log(errors);
    // [{ minLen: "Name should atleast be 4 char long!"  }]
}
</code></pre>

{% endtab %}

{% tab title="Javascript" %}

```javascript
import valid8r from '@bitbybit/valid8r';

// Your input's value
const input = "J";
const [isValid, errors] = valid8r.name(input, { safe: true }, {
    minLen: "Name should atleast be {minLen} char long!" // minLen => 3
});

if (!isValid) {
    console.log(errors);
    // [{ minLen: "Name should atleast be 3 char long!"  }]
}
```

{% endtab %}
{% endtabs %}

You can configure all the field's error message to be used as default in any of the validator

If any validation function requires a specific error you can easily set the error message as the third parameter in any validation function

{% tabs %}
{% tab title="Typescript" %}

```typescript
import valid8r from '@bitbybit/valid8r';

// Your input's value
const input: string = "Jay Carlo$";
const [isValid, errors] = valid8r.name(input, { safe: true }, {
    noSpChar: "Special character cannot be included!"
});

if (!isValid) {
    console.log(errors);
    // [{ noSpChar: "Special character cannot be included!"  }]
}
```

{% endtab %}

{% tab title="Javascript" %}

```javascript
import valid8r from '@bitbybit/valid8r';

// Your input's value
const input = "Jay Carlo$";
const [isValid, errors] = valid8r.name(input, { safe: true }, {
    noSpChar: "Special character cannot be included!"
});

if (!isValid) {
    console.log(errors);
    // [{ noSpChar: "Special character cannot be included!"  }]
}
```

{% endtab %}
{% endtabs %}
