# Address

Valid8r covers all the basic as well as advanced address validations that covers 99% edge cases

Start by importing valid8r into your file:

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

### Configuration Options

Valid8r phone validator provides handful of options to best meet your requirements and validation needs, the options that can be configured in address validator are:

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

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

const address: string = "123 Main St., Apt #4B";

// The configuration can be defaulted globally
<strong>valid8r.address(input, {
</strong>  safe: false, // boolean
  minLen: 5, // number
  maxLen: 255, // number
  allowedSpChars: ["#", ",", "."], // string[]
  properCapitalization: true, // boolean
  noConsecutiveSpaces: true, // boolean
  throwErrorsAs: "throw-all", // "throw-first" | "throw-last" | "throw-all",
  /*
  "throw-first" => Throws or returns the first error encountered,
  "throw-last" => Throws or returns the last error encountered,
  "throw-all" => Throws or returns all errors in a Array of string
  */
}); // the second argument is optional
</code></pre>

{% endtab %}

{% tab title="Javascript" %}

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

const address = "123 Main St., Apt #4B";

// The configuration can be defaulted globally
valid8r.address(input, {
  safe: false, // boolean
  minLen: 5, // number
  maxLen: 255, // number
  allowedSpChars: ["#", ",", "."], // string[]
  properCapitalization: true, // boolean | Every letter of first word should be capitalized
  noConsecutiveSpaces: true, // boolean | No spaces one after another
  throwErrorsAs: "throw-all", // "throw-first" | "throw-last" | "throw-all",
  /*
  "throw-first" => Throws or returns the first error encountered,
  "throw-last" => Throws or returns the last error encountered,
  "throw-all" => Throws or returns all errors in a Array of string
  */
}); // the second argument is optional
```

{% endtab %}
{% endtabs %}

The `allowedSpChars` field accepts special characters which are allowed to be included in the address, it accepts a array of special characters like:

```typescript
allowedSpChars: [',', '#']; // Accepts these sp. characters only
// or
allowedSpChars: true // Allows all
```

If not provided explicitly will accept `,` `.` and `#`

> The Configuration can be globally set for all the validators in your project, [know more?](https://c4code.gitbook.io/valid8r/getting-started/publish-your-docs)

### Basic Usage

Here's how we can use the email validator in it's most basic manner:

Start by importing valid8r:

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

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

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

// Your input's value
const address: string = "123 Main St., Apt #4B";

valid8r.address(address, {
  minLen: 10,
  maxLen: 100,
  allowedSpChars: [",", "."],
}); // throws AddressValidationFailed error as '#' is not allowed
```

{% endtab %}

{% tab title="Javascript" %}

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

// Your input's value
const address = "123 Main St., Apt #4B";

valid8r.address(address, {
  minLen: 10,
  maxLen: 100,
  allowedSpChars: [",", "."],
}); // throws AddressValidationFailed error as '#' is not allowed
```

{% endtab %}
{% endtabs %}

By default the phone validator requires international proper capitalization, i.e all the first letter of every word should be capitalized, can be disabled by passing `properCapitalization` as false

### Safe handling

By default, all the validations in valid8r works on unsafe flag, meaning if any of the validation fails it will throw an Error.

However, if you don't want it to throw any error but return them. set the safe flag to true.

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

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

// Your input's value
const address: string = "123 Main St., Apt #4B";

const [isValid, errors] = valid8r.address(address, {
  minLen: 10,
  maxLen: 100,
  allowedSpChars: [",", "."],
  safe: true
});

if (!isValid) {
  console.log(errors);
    // [{ allowedSpChars: "Address contains invalid characters. Only alphanumeric characters and , or . are allowed." }]
}
```

{% endtab %}

{% tab title="Javascript" %}

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

// Your input's value
const address = "123 Main St., Apt #4B";

const [isValid, errors] = valid8r.address(address, {
  minLen: 10,
  maxLen: 100,
  allowedSpChars: [",", "."],
  safe: true
});

if (!isValid) {
  console.log(errors);
    // [{ allowedSpChars: "Address contains invalid characters. Only alphanumeric characters and , or . are allowed." }]
}
```

{% endtab %}
{% endtabs %}

### Error Messages

If you don't like the default error message and want to set a custom error message for any specific fields, pass an object as a third argument of the address function:

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

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

// Your input's value
const address: string = "123 Main St., Apt B";

const [isValid, errors] = valid8r.address(address, {
  maxLen: 10,
  safe: true
}, {
  maxLen: "Maximum length exceeded"
});

if (!isValid) {
  console.log(errors);
    // [{ maxLen: "Maximum length exceeded" }]
}
```

{% endtab %}

{% tab title="Javascript" %}

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

// Your input's value
const address = "123 Main St., Apt B";

const [isValid, errors] = valid8r.address(address, {
  maxLen: 10,
  safe: true
}, {
  maxLen: "Maximum length exceeded"
});

if (!isValid) {
  console.log(errors);
    // [{ maxLen: "Maximum length exceeded" }]
}
```

{% endtab %}
{% endtabs %}

### Error Message Options

Valid8r phone validator provides handful of options to configure error messages to best fit your project requirements

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

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

const address: string = "l3, Lb Street. New York";

// The configuration can be defaulted globally
valid8r.address(address, { /* Field Configurations */ }, {
  minLen: 
    "Address must be at least {minLen} characters long.",
  maxLen: 
    "Address must not exceed {maxLen} characters.",
  allowedSpChars:
    "Address contains invalid characters. Only alphanumeric characters and {allowedSpChars} are allowed.",
  properCapitalization:
    "Each word in the address must be properly capitalized.",
  noConsecutiveSpaces: 
    "Address must not contain consecutive spaces.",
}); // the second and third argument is optional
```

{% endtab %}

{% tab title="Javascript" %}

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

const address = "l3, Lb Street. New York";

// The configuration can be defaulted globally
valid8r.address(address, { /* Field Configurations */ }, {
  minLen: 
    "Address must be at least {minLen} characters long.",
  maxLen: 
    "Address must not exceed {maxLen} characters.",
  allowedSpChars:
    "Address contains invalid characters. Only alphanumeric characters and {allowedSpChars} are allowed.",
  properCapitalization:
    "Each word in the address must be properly capitalized.",
  noConsecutiveSpaces: 
    "Address must not contain consecutive spaces.",
}); // the second and third argument is optional
```

{% endtab %}
{% endtabs %}

All the options are optional, if passed will be used as the configuration for that specific validator

> The Error messages can be globally set for all the validators in your project, [know more?](https://c4code.gitbook.io/valid8r/getting-started/publish-your-docs#error-messages)
