schema-markdown 1.3.2 → 1.3.3

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 (2) hide show
  1. package/lib/schema.js +21 -11
  2. package/package.json +4 -4
package/lib/schema.js CHANGED
@@ -159,8 +159,17 @@ export function validateType(types, typeName, value, memberFqn = null) {
159
159
  }
160
160
 
161
161
 
162
+ // Helper to create a local date (midnight) - unlike new Date, years 0 through 99 aren't 1900 through 1999
163
+ function newLocalDate(year, month, day) {
164
+ const date = new Date(0);
165
+ date.setFullYear(year, month - 1, day);
166
+ date.setHours(0, 0, 0, 0);
167
+ return date;
168
+ }
169
+
170
+
162
171
  // Regular expressions used by validateTypeHelper
163
- const rDate = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/;
172
+ const rDate = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})(?:T00:00(?::00(?:\.0+)?)?(?:Z|(?<tzhour>[+-]\d{2}):(?<tzminute>\d{2})))?$/;
164
173
  const rDatetime = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/;
165
174
  const rUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
166
175
 
@@ -221,26 +230,27 @@ function validateTypeHelper(types, type, value, memberFqn) {
221
230
 
222
231
  // date?
223
232
  } else if (builtin === 'date') {
224
- // Convert string?
233
+ // Convert string? A date, or a datetime at midnight in its own offset - the date is the string's date.
225
234
  if (typeof value === 'string') {
226
- // Date-only?
227
235
  const mDate = value.match(rDate);
228
236
  if (mDate !== null) {
229
237
  const year = Number.parseInt(mDate.groups.year, 10);
230
238
  const month = Number.parseInt(mDate.groups.month, 10);
231
239
  const day = Number.parseInt(mDate.groups.day, 10);
232
- valueNew = new Date(year, month - 1, day);
233
- // new Date rolls an out-of-range month/day over (e.g. Feb 30 -> Mar 1); reject that
234
- if (valueNew.getMonth() !== month - 1) {
240
+ const {tzhour = null, tzminute = null} = mDate.groups;
241
+ valueNew = newLocalDate(year, month, day);
242
+ // A date rolls an out-of-range month/day over (e.g. Feb 30 -> Mar 1); reject that, and an invalid offset
243
+ if (valueNew.getMonth() !== month - 1 ||
244
+ (tzhour !== null && (Math.abs(Number.parseInt(tzhour, 10)) > 23 || Number.parseInt(tzminute, 10) > 59))) {
235
245
  throwMemberError(type, value, memberFqn);
236
246
  }
237
247
  } else {
238
248
  throwMemberError(type, value, memberFqn);
239
249
  }
240
250
 
241
- // Not a date, or a date with a time-of-day?
242
- } else if (!(value instanceof Date) ||
243
- value.getHours() + value.getMinutes() + value.getSeconds() + value.getMilliseconds() !== 0) {
251
+ // Not a date - a datetime at the start of its day? A day's start is its local midnight, or its first time when a
252
+ // timezone change skips midnight.
253
+ } else if (!(value instanceof Date) || new Date(value).setHours(0, 0, 0, 0) !== value.getTime()) {
244
254
  throwMemberError(type, value, memberFqn);
245
255
  }
246
256
 
@@ -254,8 +264,8 @@ function validateTypeHelper(types, type, value, memberFqn) {
254
264
  const year = Number.parseInt(mDatetime.groups.year, 10);
255
265
  const month = Number.parseInt(mDatetime.groups.month, 10);
256
266
  const day = Number.parseInt(mDatetime.groups.day, 10);
257
- // new Date rolls an out-of-range day over (e.g. Feb 30 -> Mar 1); reject that
258
- if (Number.isNaN(valueNew.getTime()) || new Date(year, month - 1, day).getMonth() !== month - 1) {
267
+ // A date rolls an out-of-range day over (e.g. Feb 30 -> Mar 1); reject that
268
+ if (Number.isNaN(valueNew.getTime()) || newLocalDate(year, month, day).getMonth() !== month - 1) {
259
269
  throwMemberError(type, value, memberFqn);
260
270
  }
261
271
  } else {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "schema-markdown",
4
- "version": "1.3.2",
4
+ "version": "1.3.3",
5
5
  "description": "A schema definition and validation library",
6
6
  "keywords": [
7
7
  "schema",
@@ -25,9 +25,9 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "@eslint/js": "~10.0",
28
- "c8": "~11.0",
29
- "eslint": "~10.6",
30
- "globals": "~17.7",
28
+ "c8": "~12.0",
29
+ "eslint": "~10.12",
30
+ "globals": "~17.13",
31
31
  "jsdoc": "~4.0"
32
32
  }
33
33
  }