Skip to content

Commit

Permalink
5.0.0 - Modernise dependencies (#25)
Browse files Browse the repository at this point in the history
* Modernise dependencies

* Switch to CircleCI

* Update readme badges

* Simplify coverage scripts

* Explicitly install Joi for coverage
  • Loading branch information
tjdavey committed Mar 27, 2024
1 parent 16e7704 commit a0ea7d9
Show file tree
Hide file tree
Showing 12 changed files with 8,339 additions and 8,021 deletions.
37 changes: 37 additions & 0 deletions .circleci/config.yml
@@ -0,0 +1,37 @@
version: 2.1

orbs:
node: circleci/node@5.2
coveralls: coveralls/coveralls@2.2.1

jobs:
coverage:
executor:
name: node/default
tag: 'lts'
steps:
- checkout
- node/install-packages:
check-cache: always
pkg-manager: yarn-berry
with-cache: false
- run:
command: npm install joi
name: Install Joi
- run:
command: npm run coverage
name: Run Coverage
- coveralls/upload:
coverage_file: ./coverage/lcov.info

workflows:
test:
jobs:
- node/test:
version: 'lts'
pkg-manager: npm
- node/run:
npm-run: lint
version: 'lts'
pkg-manager: npm
- coverage
11 changes: 0 additions & 11 deletions .eslintrc

This file was deleted.

11 changes: 11 additions & 0 deletions .eslintrc.yml
@@ -0,0 +1,11 @@
env:
jest: true
browser: true
commonjs: true
es2021: true
node: true
extends:
- standard
parserOptions:
ecmaVersion: latest
rules: {}
67 changes: 0 additions & 67 deletions .npmignore

This file was deleted.

2 changes: 1 addition & 1 deletion .tav.yml
@@ -1,3 +1,3 @@
joi:
versions: ">=16.1.8"
commands: NODE_ENV=test mocha --recursive
commands: jest
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

29 changes: 5 additions & 24 deletions README.md
@@ -1,12 +1,13 @@
# joi-tz - Joi Timezone Validation

[![Build Status](https://travis-ci.org/tjdavey/joi-tz.svg?branch=master)](https://travis-ci.org/tjdavey/joi-tz)
[![npm version](https://badge.fury.io/js/joi-tz.svg)](https://badge.fury.io/js/joi-tz)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![CircleCI](https://dl.circleci.com/status-badge/img/gh/tjdavey/joi-tz/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/tjdavey/joi-tz/tree/main)
[![Coverage Status](https://coveralls.io/repos/github/tjdavey/joi-tz/badge.svg)](https://coveralls.io/github/tjdavey/joi-tz)
[![Known Vulnerabilities](https://snyk.io/test/github/tjdavey/joi-tz/badge.svg?targetFile=package.json)](https://snyk.io/test/github/tjdavey/joi-tz?targetFile=package.json)
[![npm version](https://badge.fury.io/js/joi-tz.svg)](https://badge.fury.io/js/joi-tz)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Ftjdavey%2Fjoi-tz.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Ftjdavey%2Fjoi-tz?ref=badge_shield)

Provides a Joi rule to validate IANA timezone strings (eg. `Europe/London`, `America/New_York`, `Etc/GMT-8`) using [moment-timezone](https://momentjs.com/timezone/).
Provides a Joi rule to validate IANA timezone strings (eg. `Europe/London`, `America/New_York`, `Etc/GMT-8`) using [luxon](https://moment.github.io/luxon).

Joi-tz supports and is tested against Joi 16.x or higher.

Expand All @@ -30,34 +31,14 @@ Joi.timezone().validate('Melbourne/Australia');
// returns {error: null, value: 'Melbourne/Australia'}
```

### Rules

#### `returnMoment` - Convert to a moment zone object

When added to the validation chain returns a [moment zone object](https://momentjs.com/timezone/docs/#/zone-object/) which contains useful metadata about the timezone.

```js
Joi.timezone().returnMoment().validate('America/Los_Angeles');
/*
returns {
error: null,
value: {
name : 'America/Los_Angeles',
abbrs : ['PDT', 'PST'],
untils : [1414918800000, 1425808800000],
offsets : [420, 480]
}
}
*/
```

## Compatibility

This library is tested for compatibility, and contains peer dependencies with the following versions.


| Version | @hapi/joi 16.x | joi 16.x | joi 17.x |
|----------------------------------------------------------------|----------------|----------|----------|
| [5.0.0](https://github.com/tjdavey/joi-tz/releases/tag/v5.0.0) | |||
| [4.1.1](https://github.com/tjdavey/joi-tz/releases/tag/v4.1.1) | |||
| [4.1.0](https://github.com/tjdavey/joi-tz/releases/tag/v4.1.0) | |||
| [4.0.2](https://github.com/tjdavey/joi-tz/releases/tag/v4.0.2) || | |
Expand Down
25 changes: 7 additions & 18 deletions lib/index.js
@@ -1,5 +1,5 @@
const Joi = require('joi');
const moment = require('moment-timezone');
const Joi = require('joi')
const { IANAZone } = require('luxon')

module.exports = {
type: 'timezone',
Expand All @@ -8,23 +8,12 @@ module.exports = {
timezone: '"{{#label}}" must be at least a valid timezone'
},
rules: {
returnMoment: {
convert: true,
method() {
return this.$_setFlag('returnMoment', true);
}
}
},
validate(value, helpers) {
if (!moment.tz.zone(value)) {
return { value, errors: helpers.error('timezone')};
}

// The value must be a string for validation, so we can't use coerce to convert as this runs before validation
if (helpers.schema.$_getFlag('returnMoment') && helpers.prefs.convert) {
return {value: moment.tz(value)};
validate (value, helpers) {
if (!IANAZone.isValidZone(value)) {
return { value, errors: helpers.error('timezone') }
}

return {value};
return { value }
}
};
}

0 comments on commit a0ea7d9

Please sign in to comment.