@sinclair/typebox 0.25.16 → 0.25.18

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.
@@ -134,7 +134,7 @@ var TypeCompiler;
134
134
  yield* Visit(schema.returns, `${value}.prototype`);
135
135
  }
136
136
  function* Date(schema, value) {
137
- yield `(${value} instanceof Date)`;
137
+ yield `(${value} instanceof Date) && !isNaN(${value}.getTime())`;
138
138
  if (IsNumber(schema.exclusiveMinimumTimestamp))
139
139
  yield `(${value}.getTime() > ${schema.exclusiveMinimumTimestamp})`;
140
140
  if (IsNumber(schema.exclusiveMaximumTimestamp))
@@ -272,7 +272,7 @@ var TypeCompiler;
272
272
  if (IsNumber(schema.maxLength))
273
273
  yield `(${value}.length <= ${schema.maxLength})`;
274
274
  if (schema.pattern !== undefined) {
275
- const local = PushLocal(`new RegExp(/${schema.pattern}/);`);
275
+ const local = PushLocal(`${new RegExp(schema.pattern)};`);
276
276
  yield `(${local}.test(${value}))`;
277
277
  }
278
278
  if (schema.format !== undefined) {
package/errors/errors.js CHANGED
@@ -143,6 +143,9 @@ var ValueErrors;
143
143
  if (!(value instanceof globalThis.Date)) {
144
144
  return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
145
145
  }
146
+ if (isNaN(value.getTime())) {
147
+ return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
148
+ }
146
149
  if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
147
150
  yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
148
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.25.16",
3
+ "version": "0.25.18",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/value/cast.js CHANGED
@@ -127,6 +127,9 @@ var ValueCast;
127
127
  function IsArray(value) {
128
128
  return typeof value === 'object' && globalThis.Array.isArray(value);
129
129
  }
130
+ function IsDate(value) {
131
+ return typeof value === 'object' && value instanceof globalThis.Date;
132
+ }
130
133
  function IsString(value) {
131
134
  return typeof value === 'string';
132
135
  }
@@ -151,6 +154,21 @@ var ValueCast;
151
154
  function IsValueFalse(value) {
152
155
  return value === false || (IsNumber(value) && value === 0) || (IsBigInt(value) && value === globalThis.BigInt('0')) || (IsString(value) && (value.toLowerCase() === 'false' || value === '0'));
153
156
  }
157
+ function IsTimeStringWithTimeZone(value) {
158
+ return IsString(value) && /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i.test(value);
159
+ }
160
+ function IsTimeStringWithoutTimeZone(value) {
161
+ return IsString(value) && /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)?$/i.test(value);
162
+ }
163
+ function IsDateTimeStringWithTimeZone(value) {
164
+ return IsString(value) && /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i.test(value);
165
+ }
166
+ function IsDateTimeStringWithoutTimeZone(value) {
167
+ return IsString(value) && /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)?$/i.test(value);
168
+ }
169
+ function IsDateString(value) {
170
+ return IsString(value) && /^\d\d\d\d-[0-1]\d-[0-3]\d$/i.test(value);
171
+ }
154
172
  // ----------------------------------------------------------------------------------------------
155
173
  // Convert
156
174
  // ----------------------------------------------------------------------------------------------
@@ -166,6 +184,31 @@ var ValueCast;
166
184
  function TryConvertBoolean(value) {
167
185
  return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
168
186
  }
187
+ function TryConvertDate(value) {
188
+ // note: this function may return an invalid dates for the regex tests
189
+ // above. Invalid dates will however be checked during the casting
190
+ // function and will return a epoch date if invalid. Consider better
191
+ // string parsing for the iso dates in future revisions.
192
+ return IsDate(value)
193
+ ? value
194
+ : IsNumber(value)
195
+ ? new globalThis.Date(value)
196
+ : IsValueTrue(value)
197
+ ? new globalThis.Date(1)
198
+ : IsStringNumeric(value)
199
+ ? new globalThis.Date(parseInt(value))
200
+ : IsTimeStringWithoutTimeZone(value)
201
+ ? new globalThis.Date(`1970-01-01T${value}.000Z`)
202
+ : IsTimeStringWithTimeZone(value)
203
+ ? new globalThis.Date(`1970-01-01T${value}`)
204
+ : IsDateTimeStringWithoutTimeZone(value)
205
+ ? new globalThis.Date(`${value}.000Z`)
206
+ : IsDateTimeStringWithTimeZone(value)
207
+ ? new globalThis.Date(value)
208
+ : IsDateString(value)
209
+ ? new globalThis.Date(`${value}T00:00:00.000Z`)
210
+ : value;
211
+ }
169
212
  // ----------------------------------------------------------------------------------------------
170
213
  // Cast
171
214
  // ----------------------------------------------------------------------------------------------
@@ -203,7 +246,8 @@ var ValueCast;
203
246
  return result;
204
247
  }
205
248
  function Date(schema, references, value) {
206
- return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
249
+ const conversion = TryConvertDate(value);
250
+ return check_1.ValueCheck.Check(schema, references, conversion) ? clone_1.ValueClone.Clone(conversion) : create_1.ValueCreate.Create(schema, references);
207
251
  }
208
252
  function Function(schema, references, value) {
209
253
  return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
package/value/check.js CHANGED
@@ -82,6 +82,9 @@ var ValueCheck;
82
82
  if (!(value instanceof globalThis.Date)) {
83
83
  return false;
84
84
  }
85
+ if (isNaN(value.getTime())) {
86
+ return false;
87
+ }
85
88
  if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
86
89
  return false;
87
90
  }
package/value/value.d.ts CHANGED
@@ -3,9 +3,9 @@ import { ValueError } from '../errors/index';
3
3
  import { Edit } from './delta';
4
4
  /** Provides functions to perform structural updates to JavaScript values */
5
5
  export declare namespace Value {
6
- /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */
6
+ /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number, boolean and date values if a reasonable conversion is possible. */
7
7
  function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>;
8
- /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */
8
+ /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number, boolean and date values if a reasonable conversion is possible. */
9
9
  function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>;
10
10
  /** Creates a value from the given type */
11
11
  function Create<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R]): Types.Static<T>;