# mongoose-type-phone

> A phone field-type for Mongoose schemas

Latest version **1.0.1** (published 2019-08-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-type-phone
pnpm add mongoose-type-phone
yarn add mongoose-type-phone
bun add mongoose-type-phone
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-08-19 |
| First published | 2019-08-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 9.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | PROger4ever |
| Maintainers | proger4ever |
| Keywords | mongoose, types, phone, schema |

## Links

- npm: https://www.npmjs.com/package/mongoose-type-phone
- Repository: https://github.com/PROger4ever-NodeJS/mongoose-type-phone
- Issues: https://github.com/PROger4ever-NodeJS/mongoose-type-phone/issues
- npm.io page: https://npm.io/package/mongoose-type-phone

## Dependencies (2)

- [cryptiles](https://npm.io/package/cryptiles.md) ^4.1.2
- [google-libphonenumber](https://npm.io/package/google-libphonenumber.md) ^3.2.3

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-08-19

## README

# mongoose-type-phone

A phone field-type for Mongoose schemas based on [mongoose-type-email](https://www.npmjs.com/package/mongoose-type-email), [mongoose-intl-phone-number](https://www.npmjs.com/package/mongoose-intl-phone-number) and [google-libphonenumber](https://www.npmjs.com/package/google-libphonenumber).

[![npm](https://nodei.co/npm/mongoose-type-phone.png)](https://www.npmjs.com/package/mongoose-type-phone)

[![Greenkeeper badge](https://badges.greenkeeper.io/konsumer/mongoose-type-phone.svg)](https://greenkeeper.io/)
[![Build Status](https://travis-ci.org/konsumer/mongoose-type-phone.svg?branch=master)](https://travis-ci.org/konsumer/mongoose-type-phone)
[![Code Climate](https://codeclimate.com/github/konsumer/mongoose-type-phone/badges/gpa.svg)](https://codeclimate.com/github/konsumer/mongoose-type-phone)

## usage

This will validate phone, correctly:

```js
var mongoose = require('mongoose');
var mongooseTypePhone = require('mongoose-type-phone');

var UserSchema = new mongoose.Schema({
    phone: {
        work: mongoose.SchemaTypes.Phone,
        home: mongoose.SchemaTypes.Phone
    }
});
```

You can also use the stuff in `String` type:

```js
var UserSchema = new mongoose.Schema({
    phone: {
        work: {type: mongoose.SchemaTypes.Phone, required: true},
        home: {type: mongoose.SchemaTypes.Phone, required: true},
    }
});
```

You can also use it as an array:


```js
var UserSchema = new mongoose.Schema({
    phones: [{type: mongoose.SchemaTypes.Phone}]
});
```

You can add 'allowBlank: true' in order to allow empty string ('') when the field is not required

```js
var mongoose = require('mongoose');
var mongooseTypePhone = require('mongoose-type-phone');

var UserSchema = new mongoose.Schema({
    phone: {
        work: { type: mongoose.SchemaTypes.Phone, allowBlank: true }, // allows '' as a value
        home: mongoose.SchemaTypes.Phone // throws when the value is ''
    }
});
```

Here is the example with full set of available options:
```js
var mongoose = require('mongoose');
var mongooseTypePhone = require('mongoose-type-phone');

var UserSchema = new mongoose.Schema({
    phone: {
        type: mongoose.SchemaTypes.Phone,
        required: 'Phone number should be set correctly',
        allowBlank: false,
        allowedNumberTypes: [mongooseTypePhone.PhoneNumberType.MOBILE, mongooseTypePhone.PhoneNumberType.FIXED_LINE_OR_MOBILE],
        phoneNumberFormat: mongooseTypePhone.PhoneNumberFormat.INTERNATIONAL, // can be omitted to keep raw input
        defaultRegion: 'RU',
        parseOnGet: false
    }
});
```

## contribution
This project needs your contribution! :-)

## TODO
1) Add tests for all options (only tests for `required` and `allowBlank` are written now);
2) How can we avoid repeating of validation in `cast()` and `checkRequired()`?
3) Is it better to use custom class instead of prototype'ing String-type?
4) Improve examples in README.md?
5) Refactoring.

---
_Source: https://npm.io/package/mongoose-type-phone · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
