Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

16 changes: 8 additions & 8 deletions .github/workflows/ci-module.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: ci

on:
push:
branches:
- master
pull_request:
workflow_dispatch:
push:
branches:
- master
pull_request:
workflow_dispatch:

permissions:
contents: read # for actions/checkout
contents: read # for actions/checkout

jobs:
test:
uses: hapijs/.github/.github/workflows/ci-module.yml@master
test:
uses: hapijs/.github/.github/workflows/ci-module.yml@min-node-22-hapi-21
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
**/node_modules
**/package-lock.json

coverage.*
coverage/
dist/

**/.DS_Store
**/._*
Expand Down
3 changes: 0 additions & 3 deletions .npmignore

This file was deleted.

62 changes: 53 additions & 9 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,78 @@
## Compatibility

This version requires **joi** v17 or newer.
This version requires **joi** v18 or newer, and **node** v22 or newer.

## Usage

```js
const Joi = require('joi')
.extend(require('@joi/date'));
import BaseJoi from 'joi';
import { JoiDate } from '@joi/date';

const schema = Joi.date().format('YYYY-MM-DD').utc();
const Joi = BaseJoi.extend(JoiDate);

const schema = Joi.date().format('YYYY-MM-DD');
```

## Rules

### `date.format(format)`

Specifies the allowed date format:
- `format` - string or array of strings that follow the `moment.js` [format](http://momentjs.com/docs/#/parsing/string-format/).

- `format` - string or array of strings that follow the `dayjs` [format](https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens), or an object with:
- `format` - the format(s) above.
- `utc` - if `true`, the value is parsed as UTC instead of using the machine's local timezone. Defaults to `false`.

A single format:

```js
const schema = Joi.date().format('YYYY-MM-DD');

schema.validate('2021-10-29'); // 2021-10-29T00:00:00.000 in the local timezone
schema.validate('29/10/2021'); // "value" must be in YYYY-MM-DD format
```

Any dayjs token, including a time part:

```js
const schema = Joi.date().format('DD/MM/YYYY HH:mm');

schema.validate('29/10/2021 14:30');
```

Several formats, tried in order until one matches:

```js
const schema = Joi.date().format(['YYYY/MM/DD', 'DD-MM-YYYY']);

schema.validate('2021/10/29');
schema.validate('29-10-2021');
```

The object form to parse as UTC instead of the machine's local timezone:

```js
const schema = Joi.date().format('YYYY-MM-DD HH:mm');
const schema = Joi.date().format({ format: 'YYYY-MM-DD', utc: true });

schema.validate('2021-10-29'); // 2021-10-29T00:00:00.000Z
```

### `date.utc()`
```js
const schema = Joi.date().format({ format: ['YYYY/MM/DD', 'DD-MM-YYYY'], utc: true });
```

`utc` only applies to the formats above, so it cannot be combined with joi's own base formats. Those
stay joi's: use [`date.iso()`](https://joi.dev/api/#dateiso) or
[`date.timestamp()`](https://joi.dev/api/#datetimestamptype) rather than `format('iso')`.

Dates will be parsed as UTC instead of using the machine's local timezone.
`format()` only applies to string inputs. Any other value is left to joi's own date coercion, which
reads a number as an epoch timestamp in milliseconds:

```js
const schema = Joi.date().utc().format(['YYYY/MM/DD', 'DD-MM-YYYY']);
const schema = Joi.date().format('YYYYMMDD');

schema.validate('20211029'); // 2021-10-29
schema.validate(20211029); // 1970-01-01T05:36:51.029Z
```

Convert the value to a string yourself if it needs to be parsed with the format.
7 changes: 4 additions & 3 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Copyright (c) 2012-2020, Sideway Inc, and project contributors
Copyright (c) 2012-2014, Walmart.
Copyright (c) 2012-2026, Project contributors
Copyright (c) 2012-2020, Sideway Inc
Copyright (c) 2012-2014, Walmart.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 changes: 0 additions & 32 deletions lib/index.d.ts

This file was deleted.

65 changes: 0 additions & 65 deletions lib/index.js

This file was deleted.

8 changes: 8 additions & 0 deletions oxfmt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import DefaultOxfmtConfig from '@hapi/oxc-plugin/oxfmt';
import { defineConfig } from 'oxfmt';

import type { OxfmtConfig } from 'oxfmt';

export default defineConfig({
...DefaultOxfmtConfig,
}) as OxfmtConfig;
11 changes: 11 additions & 0 deletions oxlint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import HapiRecommended from '@hapi/oxc-plugin/oxlint';
import { defineConfig } from 'oxlint';

import type { OxlintConfig } from 'oxlint';

export default defineConfig({
extends: [HapiRecommended],
env: {
...HapiRecommended.env,
},
}) as OxlintConfig;
87 changes: 58 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
{
"name": "@joi/date",
"description": "Joi extension for dates",
"version": "2.1.1",
"repository": "git://github.com/sideway/joi-date",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"keywords": [
"schema",
"validation",
"date",
"moment",
"joi",
"extension"
],
"dependencies": {
"moment": "2.x.x"
},
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/lab": "24.x.x",
"@types/node": "^14.18.63",
"joi": "^17.2.0",
"typescript": "4.0.x"
},
"scripts": {
"test": "lab -t 100 -a @hapi/code -L -Y",
"test-cov-html": "lab -r html -o coverage.html -a @hapi/code"
},
"license": "BSD-3-Clause"
"name": "@joi/date",
"version": "2.1.1",
"description": "Joi extension for dates",
"keywords": [
"date",
"dayjs",
"extension",
"joi",
"schema",
"validation"
],
"license": "BSD-3-Clause",
"repository": {
"type": "git",
"url": "git://github.com/hapijs/joi-date.git"
},
"files": [
"dist",
"API.md"
],
"type": "module",
"types": "./dist/index.d.mts",
"exports": {
".": "./dist/index.mjs",
"./package.json": "./package.json"
},
"scripts": {
"prepare": "node --run build",
"test": "vitest run --coverage",
"typecheck": "tsc --noEmit",
"lint": "oxlint",
"lint:fix": "oxlint --fix",
"fmt": "oxfmt --check",
"fmt:fix": "oxfmt",
"check": "npm run lint && npm run fmt && npm run typecheck && npm test",
"build": "tsdown"
},
"dependencies": {
"dayjs": "^1.11.21"
},
"devDependencies": {
"@hapi/hoek": "^11.0.7",
"@hapi/oxc-plugin": "^1.0.2",
"@types/node": "^22.20.1",
"@vitest/coverage-v8": "^4.1.10",
"joi": "^18.2.3",
"oxfmt": "^0.61.0",
"oxlint": "^1.76.0",
"tsdown": "^0.22.14",
"typescript": "^6.0.3",
"vitest": "^4.1.10"
},
"peerDependencies": {
"joi": ">=18"
},
"engines": {
"node": ">=22"
}
}
Loading