@naturalcycles/nodejs-lib 15.89.0 → 15.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/dist/validation/ajv/jsonSchemaBuilder.d.ts +1 -1
  2. package/dist/validation/ajv/jsonSchemaBuilder.js +2 -4
  3. package/package.json +1 -3
  4. package/src/validation/ajv/jsonSchemaBuilder.ts +4 -5
  5. package/dist/validation/joi/index.d.ts +0 -9
  6. package/dist/validation/joi/index.js +0 -5
  7. package/dist/validation/joi/joi.extensions.d.ts +0 -11
  8. package/dist/validation/joi/joi.extensions.js +0 -25
  9. package/dist/validation/joi/joi.model.d.ts +0 -7
  10. package/dist/validation/joi/joi.model.js +0 -5
  11. package/dist/validation/joi/joi.shared.schemas.d.ts +0 -91
  12. package/dist/validation/joi/joi.shared.schemas.js +0 -135
  13. package/dist/validation/joi/joi.validation.error.d.ts +0 -29
  14. package/dist/validation/joi/joi.validation.error.js +0 -8
  15. package/dist/validation/joi/joi.validation.util.d.ts +0 -33
  16. package/dist/validation/joi/joi.validation.util.js +0 -139
  17. package/dist/validation/joi/number.extensions.d.ts +0 -6
  18. package/dist/validation/joi/number.extensions.js +0 -38
  19. package/dist/validation/joi/string.extensions.d.ts +0 -11
  20. package/dist/validation/joi/string.extensions.js +0 -104
  21. package/src/validation/joi/index.ts +0 -32
  22. package/src/validation/joi/joi.extensions.ts +0 -38
  23. package/src/validation/joi/joi.model.ts +0 -12
  24. package/src/validation/joi/joi.shared.schemas.ts +0 -207
  25. package/src/validation/joi/joi.validation.error.ts +0 -35
  26. package/src/validation/joi/joi.validation.util.ts +0 -183
  27. package/src/validation/joi/number.extensions.ts +0 -46
  28. package/src/validation/joi/string.extensions.ts +0 -129
@@ -1,129 +0,0 @@
1
- import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
2
- import type { IsoDate } from '@naturalcycles/js-lib/types'
3
- import type Joi from 'joi'
4
- import type { Extension, StringSchema as JoiStringSchema } from 'joi'
5
-
6
- export interface StringSchema<TSchema = string> extends JoiStringSchema<TSchema> {
7
- dateString: (min?: IsoDate | 'today', max?: IsoDate | 'today') => StringSchema<IsoDate>
8
- }
9
-
10
- export interface JoiDateStringOptions {
11
- min?: IsoDate | 'today'
12
- max?: IsoDate | 'today'
13
- }
14
-
15
- export function stringExtensions(joi: typeof Joi): Extension {
16
- return {
17
- type: 'string',
18
- base: joi.string(),
19
- messages: {
20
- 'string.dateString': '"{{#label}}" must be an ISO8601 date (yyyy-mm-dd)',
21
- 'string.dateStringMin': '"{{#label}}" must be not earlier than {{#min}}',
22
- 'string.dateStringMax': '"{{#label}}" must be not later than {{#max}}',
23
- 'string.dateStringCalendarAccuracy': '"{{#label}}" must be a VALID calendar date',
24
- 'string.stripHTML': '"{{#label}}" must NOT contain any HTML tags',
25
- },
26
- rules: {
27
- dateString: {
28
- method(min?: IsoDate, max?: IsoDate) {
29
- return this.$_addRule({
30
- name: 'dateString',
31
- args: { min, max } satisfies JoiDateStringOptions,
32
- })
33
- },
34
- args: [
35
- {
36
- name: 'min',
37
- // ref: true, // check false
38
- assert: v => v === undefined || typeof v === 'string',
39
- message: 'must be a string',
40
- },
41
- {
42
- name: 'max',
43
- // ref: true,
44
- assert: v => v === undefined || typeof v === 'string',
45
- message: 'must be a string',
46
- },
47
- ],
48
- validate(v: string, helpers, args: JoiDateStringOptions) {
49
- // console.log('dateString validate called', {v, args})
50
-
51
- let err: string | undefined
52
- let { min, max } = args
53
-
54
- // Today allows +-14 hours gap to account for different timezones
55
- if (max === 'today') {
56
- max = getTodayStrPlus15()
57
- }
58
- if (min === 'today') {
59
- min = getTodayStrMinus15()
60
- }
61
- // console.log('min/max', min, max)
62
-
63
- const parts = /^(\d{4})-(\d{2})-(\d{2})$/.exec(v)
64
- if (!parts || parts.length < 4) {
65
- err = 'string.dateString'
66
- } else if (min && v < min) {
67
- err = 'string.dateStringMin'
68
- } else if (max && v > max) {
69
- err = 'string.dateStringMax'
70
- } else if (!isValidDate(parts)) {
71
- err = 'string.dateStringCalendarAccuracy'
72
- }
73
-
74
- if (err) {
75
- return helpers.error(err, args)
76
- }
77
-
78
- return v // validation passed
79
- },
80
- },
81
- },
82
- }
83
- }
84
-
85
- const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
86
-
87
- // Based on: https://github.com/ajv-validator
88
- function isValidDate(parts: string[]): boolean {
89
- const year = Number(parts[1])
90
- const month = Number(parts[2])
91
- const day = Number(parts[3])
92
- return (
93
- month >= 1 &&
94
- month <= 12 &&
95
- day >= 1 &&
96
- day <= (month === 2 && isLeapYear(year) ? 29 : DAYS[month]!)
97
- )
98
- }
99
-
100
- function isLeapYear(year: number): boolean {
101
- return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)
102
- }
103
-
104
- let lastCheckedPlus = 0
105
- let todayStrPlusCached: IsoDate
106
- let lastCheckedMinus = 0
107
- let todayStrMinusCached: IsoDate
108
-
109
- function getTodayStrPlus15(): IsoDate {
110
- const now = Date.now()
111
- if (now - lastCheckedPlus < 3_600_000) {
112
- // cached for 1 hour
113
- return todayStrPlusCached
114
- }
115
-
116
- lastCheckedPlus = now
117
- return (todayStrPlusCached = localTime.now().plus(15, 'hour').toISODate())
118
- }
119
-
120
- function getTodayStrMinus15(): IsoDate {
121
- const now = Date.now()
122
- if (now - lastCheckedMinus < 3_600_000) {
123
- // cached for 1 hour
124
- return todayStrMinusCached
125
- }
126
-
127
- lastCheckedMinus = now
128
- return (todayStrMinusCached = localTime.now().plus(-15, 'hour').toISODate())
129
- }