@sprucelabs/schema 25.6.123 → 26.0.1

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 (43) hide show
  1. package/build/StaticSchemaEntityImplementation.js +7 -6
  2. package/build/esm/StaticSchemaEntityImplementation.js +7 -6
  3. package/build/esm/fields/AbstractField.d.ts +1 -4
  4. package/build/esm/fields/AbstractField.js +2 -2
  5. package/build/esm/fields/DateField.d.ts +9 -0
  6. package/build/esm/fields/DateField.js +33 -0
  7. package/build/esm/fields/DateField.types.d.ts +1 -5
  8. package/build/esm/fields/DateTimeField.d.ts +4 -0
  9. package/build/esm/fields/DateTimeField.js +19 -1
  10. package/build/esm/fields/DateTimeField.types.d.ts +1 -3
  11. package/build/esm/index.d.ts +1 -1
  12. package/build/esm/index.js +1 -1
  13. package/build/esm/tests/AbstractDateFieldTest.d.ts +10 -0
  14. package/build/esm/tests/AbstractDateFieldTest.js +39 -0
  15. package/build/esm/tests/validationErrorAssert.utility.d.ts +2 -2
  16. package/build/esm/tests/validationErrorAssert.utility.js +2 -2
  17. package/build/esm/utilities/assertOptions.d.ts +1 -1
  18. package/build/esm/utilities/assertOptions.js +1 -1
  19. package/build/esm/utilities/getStartOfDay.d.ts +1 -0
  20. package/build/esm/utilities/getStartOfDay.js +8 -0
  21. package/build/esm/utilities/isUndefinedOrNull.d.ts +1 -0
  22. package/build/esm/utilities/isUndefinedOrNull.js +3 -0
  23. package/build/fields/AbstractField.d.ts +1 -4
  24. package/build/fields/AbstractField.js +2 -2
  25. package/build/fields/DateField.d.ts +9 -0
  26. package/build/fields/DateField.js +35 -0
  27. package/build/fields/DateField.types.d.ts +1 -5
  28. package/build/fields/DateTimeField.d.ts +4 -0
  29. package/build/fields/DateTimeField.js +19 -1
  30. package/build/fields/DateTimeField.types.d.ts +1 -3
  31. package/build/index.d.ts +1 -1
  32. package/build/index.js +2 -2
  33. package/build/tests/AbstractDateFieldTest.d.ts +10 -0
  34. package/build/tests/AbstractDateFieldTest.js +31 -0
  35. package/build/tests/validationErrorAssert.utility.d.ts +2 -2
  36. package/build/tests/validationErrorAssert.utility.js +2 -2
  37. package/build/utilities/assertOptions.d.ts +1 -1
  38. package/build/utilities/assertOptions.js +1 -1
  39. package/build/utilities/getStartOfDay.d.ts +1 -0
  40. package/build/utilities/getStartOfDay.js +11 -0
  41. package/build/utilities/isUndefinedOrNull.d.ts +1 -0
  42. package/build/utilities/isUndefinedOrNull.js +6 -0
  43. package/package.json +2 -2
@@ -101,24 +101,25 @@ class StaticSchemaEntityImplementation extends AbstractEntity_1.default {
101
101
  this.getNamedFields(options).forEach((namedField) => {
102
102
  var _a, _b, _c, _d;
103
103
  const { name, field } = namedField;
104
- let valueArray = (0, normalizeFieldValue_1.normalizeValueToArray)(this.values[name]);
104
+ let valueAsArray = (0, normalizeFieldValue_1.normalizeValueToArray)(this.values[name]);
105
105
  if (field.isRequired &&
106
106
  field.isArray &&
107
- (!this.values[name] || valueArray.length < ((_a = field.minArrayLength) !== null && _a !== void 0 ? _a : 1))) {
107
+ (!this.values[name] ||
108
+ valueAsArray.length < ((_a = field.minArrayLength) !== null && _a !== void 0 ? _a : 1))) {
108
109
  errors.push({
109
110
  code: !this.values[name] ? 'MISSING_PARAMETER' : 'INVALID_PARAMETER',
110
111
  name,
111
112
  friendlyMessage: !this.values[name]
112
113
  ? `'${(_b = field.label) !== null && _b !== void 0 ? _b : field.name}' is required!`
113
- : `'${(_c = field.label) !== null && _c !== void 0 ? _c : field.name}' must have at least ${field.minArrayLength} value${field.minArrayLength === 1 ? '' : 's'}. I found ${valueArray.length}!`,
114
+ : `'${(_c = field.label) !== null && _c !== void 0 ? _c : field.name}' must have at least ${field.minArrayLength} value${field.minArrayLength === 1 ? '' : 's'}. I found ${valueAsArray.length}!`,
114
115
  });
115
116
  }
116
117
  else {
117
118
  if ((!field.isArray || ((_d = field.minArrayLength) !== null && _d !== void 0 ? _d : 0) > 0) &&
118
- valueArray.length === 0) {
119
- valueArray = [undefined];
119
+ valueAsArray.length === 0) {
120
+ valueAsArray = [undefined];
120
121
  }
121
- for (const value of valueArray) {
122
+ for (const value of valueAsArray) {
122
123
  const fieldErrors = field.validate(value, {
123
124
  schemasById: {},
124
125
  });
@@ -77,24 +77,25 @@ export default class StaticSchemaEntityImplementation extends AbstractEntity {
77
77
  this.getNamedFields(options).forEach((namedField) => {
78
78
  var _a, _b, _c, _d;
79
79
  const { name, field } = namedField;
80
- let valueArray = normalizeValueToArray(this.values[name]);
80
+ let valueAsArray = normalizeValueToArray(this.values[name]);
81
81
  if (field.isRequired &&
82
82
  field.isArray &&
83
- (!this.values[name] || valueArray.length < ((_a = field.minArrayLength) !== null && _a !== void 0 ? _a : 1))) {
83
+ (!this.values[name] ||
84
+ valueAsArray.length < ((_a = field.minArrayLength) !== null && _a !== void 0 ? _a : 1))) {
84
85
  errors.push({
85
86
  code: !this.values[name] ? 'MISSING_PARAMETER' : 'INVALID_PARAMETER',
86
87
  name,
87
88
  friendlyMessage: !this.values[name]
88
89
  ? `'${(_b = field.label) !== null && _b !== void 0 ? _b : field.name}' is required!`
89
- : `'${(_c = field.label) !== null && _c !== void 0 ? _c : field.name}' must have at least ${field.minArrayLength} value${field.minArrayLength === 1 ? '' : 's'}. I found ${valueArray.length}!`,
90
+ : `'${(_c = field.label) !== null && _c !== void 0 ? _c : field.name}' must have at least ${field.minArrayLength} value${field.minArrayLength === 1 ? '' : 's'}. I found ${valueAsArray.length}!`,
90
91
  });
91
92
  }
92
93
  else {
93
94
  if ((!field.isArray || ((_d = field.minArrayLength) !== null && _d !== void 0 ? _d : 0) > 0) &&
94
- valueArray.length === 0) {
95
- valueArray = [undefined];
95
+ valueAsArray.length === 0) {
96
+ valueAsArray = [undefined];
96
97
  }
97
- for (const value of valueArray) {
98
+ for (const value of valueAsArray) {
98
99
  const fieldErrors = field.validate(value, {
99
100
  schemasById: {},
100
101
  });
@@ -18,7 +18,7 @@ export default abstract class AbstractField<F extends FieldDefinitions> implemen
18
18
  /** Details needed for generating templates */
19
19
  static generateTemplateDetails(_options: FieldTemplateDetailOptions<any>): FieldTemplateDetails;
20
20
  get options(): {} | import("./SelectField.types").SelectFieldOptions | {
21
- durationFormat?: string | undefined; /** The definition for this field */
21
+ durationFormat?: string | undefined;
22
22
  minDuration?: import("./DurationField.types").DurationFieldValue | undefined;
23
23
  maxDuration?: import("./DurationField.types").DurationFieldValue | undefined;
24
24
  } | {
@@ -35,8 +35,6 @@ export default abstract class AbstractField<F extends FieldDefinitions> implemen
35
35
  acceptableTypes?: string[] | undefined;
36
36
  maxSize?: string | undefined;
37
37
  relativeTo?: string | undefined;
38
- } | {
39
- DateFormat?: string | undefined;
40
38
  } | {
41
39
  relativeTo?: string | undefined;
42
40
  } | undefined;
@@ -50,4 +48,3 @@ export default abstract class AbstractField<F extends FieldDefinitions> implemen
50
48
  /** Transform any value to the value type of this field */
51
49
  toValueType(value: any, _: any): any;
52
50
  }
53
- /** A field that comprises a schema */
@@ -1,4 +1,5 @@
1
1
  import SpruceError from '../errors/SpruceError.js';
2
+ import isUndefinedOrNull from '../utilities/isUndefinedOrNull.js';
2
3
  export default class AbstractField {
3
4
  constructor(name, definition) {
4
5
  this.definition = definition;
@@ -64,7 +65,7 @@ public static generateTemplateDetails(
64
65
  }
65
66
  validate(value, _) {
66
67
  const errors = [];
67
- if ((typeof value === 'undefined' || value === null) && this.isRequired) {
68
+ if (isUndefinedOrNull(value) && this.isRequired) {
68
69
  errors.push({
69
70
  code: 'MISSING_PARAMETER',
70
71
  name: this.name,
@@ -78,4 +79,3 @@ public static generateTemplateDetails(
78
79
  return value;
79
80
  }
80
81
  }
81
- /** A field that comprises a schema */
@@ -1,7 +1,16 @@
1
+ import { FieldError } from '../errors/options.types';
1
2
  import { FieldTemplateDetailOptions, FieldTemplateDetails } from '../types/template.types';
2
3
  import AbstractField from './AbstractField';
3
4
  import { DateFieldDefinition } from './DateField.types';
5
+ import { ValidateOptions } from './field.static.types';
4
6
  export default class DateField extends AbstractField<DateFieldDefinition> {
5
7
  static get description(): string;
6
8
  static generateTemplateDetails(options: FieldTemplateDetailOptions<DateFieldDefinition>): FieldTemplateDetails;
9
+ validate(value: any, options?: ValidateOptions<DateFieldDefinition>): FieldError[];
10
+ toValueType(value: any): number;
7
11
  }
12
+ export declare function validateDateValue(options: {
13
+ value: any;
14
+ isRequired: boolean;
15
+ name: string;
16
+ }): FieldError[];
@@ -1,3 +1,5 @@
1
+ import getStartOfDay from '../utilities/getStartOfDay.js';
2
+ import isUndefinedOrNull from '../utilities/isUndefinedOrNull.js';
1
3
  import AbstractField from './AbstractField.js';
2
4
  export default class DateField extends AbstractField {
3
5
  static get description() {
@@ -8,4 +10,35 @@ export default class DateField extends AbstractField {
8
10
  valueType: `${options.importAs}.IDateFieldValue${options.definition.isArray ? '[]' : ''}`,
9
11
  };
10
12
  }
13
+ validate(value, options) {
14
+ const errors = super.validate(value, options);
15
+ if (errors.length > 0) {
16
+ return errors;
17
+ }
18
+ return validateDateValue({
19
+ value,
20
+ isRequired: this.isRequired,
21
+ name: this.name,
22
+ });
23
+ }
24
+ toValueType(value) {
25
+ return getStartOfDay(value);
26
+ }
27
+ }
28
+ export function validateDateValue(options) {
29
+ const { value, isRequired, name } = options;
30
+ if (isUndefinedOrNull(value) && !isRequired) {
31
+ return [];
32
+ }
33
+ if (typeof value === 'number' || value instanceof Date) {
34
+ return [];
35
+ }
36
+ else {
37
+ return [
38
+ {
39
+ name,
40
+ code: 'INVALID_PARAMETER',
41
+ },
42
+ ];
43
+ }
11
44
  }
@@ -1,10 +1,6 @@
1
1
  import { FieldDefinition } from './field.static.types';
2
- export interface DateFieldValue extends Date {
3
- }
2
+ export declare type DateFieldValue = number;
4
3
  export declare type DateFieldDefinition = FieldDefinition<DateFieldValue> & {
5
4
  /** * A date/time field that stores everything in GMT and handles all the timezone */
6
5
  type: 'date';
7
- options?: {
8
- DateFormat?: string;
9
- };
10
6
  };
@@ -1,7 +1,11 @@
1
+ import { FieldError } from '../errors/options.types';
1
2
  import { FieldTemplateDetailOptions, FieldTemplateDetails } from '../types/template.types';
2
3
  import AbstractField from './AbstractField';
3
4
  import { DateTimeFieldDefinition } from './DateTimeField.types';
5
+ import { ValidateOptions } from './field.static.types';
4
6
  export default class DateTimeField extends AbstractField<DateTimeFieldDefinition> {
5
7
  static get description(): string;
6
8
  static generateTemplateDetails(options: FieldTemplateDetailOptions<DateTimeFieldDefinition>): FieldTemplateDetails;
9
+ validate(value: any, options?: ValidateOptions<DateTimeFieldDefinition>): FieldError[];
10
+ toValueType(value: any): any;
7
11
  }
@@ -1,11 +1,29 @@
1
1
  import AbstractField from './AbstractField.js';
2
+ import { validateDateValue } from './DateField.js';
2
3
  export default class DateTimeField extends AbstractField {
3
4
  static get description() {
4
5
  return 'Date and time support.';
5
6
  }
6
7
  static generateTemplateDetails(options) {
7
8
  return {
8
- valueType: `${options.importAs}.IDateTimeFieldValue${options.definition.isArray ? '[]' : ''}`,
9
+ valueType: `${options.importAs}.DateTimeFieldValue${options.definition.isArray ? '[]' : ''}`,
9
10
  };
10
11
  }
12
+ validate(value, options) {
13
+ const errors = super.validate(value, options);
14
+ if (errors.length > 0) {
15
+ return errors;
16
+ }
17
+ return validateDateValue({
18
+ value,
19
+ isRequired: this.isRequired,
20
+ name: this.name,
21
+ });
22
+ }
23
+ toValueType(value) {
24
+ if (value instanceof Date) {
25
+ return value.getTime();
26
+ }
27
+ return value;
28
+ }
11
29
  }
@@ -1,7 +1,5 @@
1
1
  import { FieldDefinition } from './field.static.types';
2
- export interface DateTimeFieldValue {
3
- gmt: string;
4
- }
2
+ export declare type DateTimeFieldValue = number;
5
3
  export declare type DateTimeFieldDefinition = FieldDefinition<DateTimeFieldValue> & {
6
4
  /** * A date/time field that stores everything in GMT and handles all the timezone */
7
5
  type: 'dateTime';
@@ -2,7 +2,7 @@ export * from './StaticSchemaEntityImplementation';
2
2
  import StaticSchemaEntityImplementation from './StaticSchemaEntityImplementation';
3
3
  import { FieldRegistration } from './utilities/registerFieldType';
4
4
  export default StaticSchemaEntityImplementation;
5
- export { default as validationErrorAssertUtil } from './tests/validationErrorAssert.utility';
5
+ export { default as validationErrorAssert } from './tests/validationErrorAssert.utility';
6
6
  export { default as selectAssertUtil } from './tests/selectAssert.utility';
7
7
  export * from './schemas.static.types';
8
8
  export * from './fields';
@@ -16,7 +16,7 @@ import selectRegistration from './addons/selectField.addon.js';
16
16
  import textRegistration from './addons/textField.addon.js';
17
17
  import StaticSchemaEntityImplementation from './StaticSchemaEntityImplementation.js';
18
18
  export default StaticSchemaEntityImplementation;
19
- export { default as validationErrorAssertUtil } from './tests/validationErrorAssert.utility.js';
19
+ export { default as validationErrorAssert } from './tests/validationErrorAssert.utility.js';
20
20
  export { default as selectAssertUtil } from './tests/selectAssert.utility.js';
21
21
  export * from './schemas.static.types.js';
22
22
  export * from './fields/index.js';
@@ -0,0 +1,10 @@
1
+ import StaticSchemaEntityImplementation from '..';
2
+ import AbstractSchemaTest from '../AbstractSchemaTest';
3
+ export default abstract class AbstractDateFieldTest extends AbstractSchemaTest {
4
+ protected static schema: any;
5
+ protected static entity: StaticSchemaEntityImplementation<any>;
6
+ protected static beforeEach(): Promise<void>;
7
+ protected static canSetDateAsDateObject(): void;
8
+ protected static failsValidationIfPassedString(fieldName: any, value: any): void;
9
+ protected static honorsRequired(): void;
10
+ }
@@ -0,0 +1,39 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { assert } from '@sprucelabs/test';
11
+ import { validationErrorAssert } from '../index.js';
12
+ import AbstractSchemaTest from '../AbstractSchemaTest.js';
13
+ import SchemaEntityFactory from '../factories/SchemaEntityFactory.js';
14
+ export default class AbstractDateFieldTest extends AbstractSchemaTest {
15
+ static beforeEach() {
16
+ const _super = Object.create(null, {
17
+ beforeEach: { get: () => super.beforeEach }
18
+ });
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ yield _super.beforeEach.call(this);
21
+ this.entity = SchemaEntityFactory.Entity(this.schema);
22
+ });
23
+ }
24
+ static canSetDateAsDateObject() {
25
+ //@ts-ignore
26
+ this.entity.set('optionalBirthday', new Date());
27
+ }
28
+ static failsValidationIfPassedString(fieldName, value) {
29
+ const err = assert.doesThrow(() =>
30
+ //@ts-ignore
31
+ this.entity.set(fieldName, value));
32
+ validationErrorAssert.assertError(err, { invalid: [fieldName] });
33
+ }
34
+ static honorsRequired() {
35
+ assert.isFalse(this.entity.isValid());
36
+ this.entity.set('requiredBirthday', new Date().getTime());
37
+ assert.isTrue(this.entity.isValid());
38
+ }
39
+ }
@@ -4,7 +4,7 @@ declare type ValidationErrorAssertOptions = {
4
4
  unexpected?: string[];
5
5
  };
6
6
  export declare type ValidationErrorAssertOptionsKey = keyof ValidationErrorAssertOptions;
7
- declare const validationErrorAssertUtil: {
7
+ declare const validationErrorAssert: {
8
8
  assertError(error: any, options: ValidationErrorAssertOptions): void;
9
9
  };
10
- export default validationErrorAssertUtil;
10
+ export default validationErrorAssert;
@@ -17,7 +17,7 @@ function flattenFields(fieldErrors, flattened, namePrefix = '') {
17
17
  }
18
18
  });
19
19
  }
20
- const validationErrorAssertUtil = {
20
+ const validationErrorAssert = {
21
21
  assertError(error, options) {
22
22
  var _a, _b;
23
23
  const missing = [];
@@ -66,4 +66,4 @@ const validationErrorAssertUtil = {
66
66
  }
67
67
  },
68
68
  };
69
- export default validationErrorAssertUtil;
69
+ export default validationErrorAssert;
@@ -1,2 +1,2 @@
1
- declare const assertOptions: <Options extends Record<string, any>>(options: Options, toCheck: string[]) => Options;
1
+ declare const assertOptions: <Options extends Record<string, any>>(options: Options, toCheck: (keyof Options)[]) => Options;
2
2
  export default assertOptions;
@@ -3,7 +3,7 @@ const assertOptions = function (options, toCheck) {
3
3
  const missing = [];
4
4
  for (const check of toCheck) {
5
5
  //@ts-ignore
6
- if (!(options === null || options === void 0 ? void 0 : options[check])) {
6
+ if (typeof (options === null || options === void 0 ? void 0 : options[check]) === 'undefined') {
7
7
  missing.push(check);
8
8
  }
9
9
  }
@@ -0,0 +1 @@
1
+ export default function getStartOfDay(timestamp?: number): number;
@@ -0,0 +1,8 @@
1
+ export default function getStartOfDay(timestamp) {
2
+ if (!timestamp) {
3
+ timestamp = new Date().getTime();
4
+ }
5
+ const date = new Date(timestamp);
6
+ date.setUTCHours(0, 0, 0, 0);
7
+ return date.getTime();
8
+ }
@@ -0,0 +1 @@
1
+ export default function isUndefinedOrNull(value: any): boolean;
@@ -0,0 +1,3 @@
1
+ export default function isUndefinedOrNull(value) {
2
+ return typeof value === 'undefined' || value === null;
3
+ }
@@ -18,7 +18,7 @@ export default abstract class AbstractField<F extends FieldDefinitions> implemen
18
18
  /** Details needed for generating templates */
19
19
  static generateTemplateDetails(_options: FieldTemplateDetailOptions<any>): FieldTemplateDetails;
20
20
  get options(): {} | import("./SelectField.types").SelectFieldOptions | {
21
- durationFormat?: string | undefined; /** The definition for this field */
21
+ durationFormat?: string | undefined;
22
22
  minDuration?: import("./DurationField.types").DurationFieldValue | undefined;
23
23
  maxDuration?: import("./DurationField.types").DurationFieldValue | undefined;
24
24
  } | {
@@ -35,8 +35,6 @@ export default abstract class AbstractField<F extends FieldDefinitions> implemen
35
35
  acceptableTypes?: string[] | undefined;
36
36
  maxSize?: string | undefined;
37
37
  relativeTo?: string | undefined;
38
- } | {
39
- DateFormat?: string | undefined;
40
38
  } | {
41
39
  relativeTo?: string | undefined;
42
40
  } | undefined;
@@ -50,4 +48,3 @@ export default abstract class AbstractField<F extends FieldDefinitions> implemen
50
48
  /** Transform any value to the value type of this field */
51
49
  toValueType(value: any, _: any): any;
52
50
  }
53
- /** A field that comprises a schema */
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
7
+ const isUndefinedOrNull_1 = __importDefault(require("../utilities/isUndefinedOrNull"));
7
8
  class AbstractField {
8
9
  constructor(name, definition) {
9
10
  this.definition = definition;
@@ -69,7 +70,7 @@ public static generateTemplateDetails(
69
70
  }
70
71
  validate(value, _) {
71
72
  const errors = [];
72
- if ((typeof value === 'undefined' || value === null) && this.isRequired) {
73
+ if ((0, isUndefinedOrNull_1.default)(value) && this.isRequired) {
73
74
  errors.push({
74
75
  code: 'MISSING_PARAMETER',
75
76
  name: this.name,
@@ -84,4 +85,3 @@ public static generateTemplateDetails(
84
85
  }
85
86
  }
86
87
  exports.default = AbstractField;
87
- /** A field that comprises a schema */
@@ -1,7 +1,16 @@
1
+ import { FieldError } from '../errors/options.types';
1
2
  import { FieldTemplateDetailOptions, FieldTemplateDetails } from '../types/template.types';
2
3
  import AbstractField from './AbstractField';
3
4
  import { DateFieldDefinition } from './DateField.types';
5
+ import { ValidateOptions } from './field.static.types';
4
6
  export default class DateField extends AbstractField<DateFieldDefinition> {
5
7
  static get description(): string;
6
8
  static generateTemplateDetails(options: FieldTemplateDetailOptions<DateFieldDefinition>): FieldTemplateDetails;
9
+ validate(value: any, options?: ValidateOptions<DateFieldDefinition>): FieldError[];
10
+ toValueType(value: any): number;
7
11
  }
12
+ export declare function validateDateValue(options: {
13
+ value: any;
14
+ isRequired: boolean;
15
+ name: string;
16
+ }): FieldError[];
@@ -3,6 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.validateDateValue = void 0;
7
+ const getStartOfDay_1 = __importDefault(require("../utilities/getStartOfDay"));
8
+ const isUndefinedOrNull_1 = __importDefault(require("../utilities/isUndefinedOrNull"));
6
9
  const AbstractField_1 = __importDefault(require("./AbstractField"));
7
10
  class DateField extends AbstractField_1.default {
8
11
  static get description() {
@@ -13,5 +16,37 @@ class DateField extends AbstractField_1.default {
13
16
  valueType: `${options.importAs}.IDateFieldValue${options.definition.isArray ? '[]' : ''}`,
14
17
  };
15
18
  }
19
+ validate(value, options) {
20
+ const errors = super.validate(value, options);
21
+ if (errors.length > 0) {
22
+ return errors;
23
+ }
24
+ return validateDateValue({
25
+ value,
26
+ isRequired: this.isRequired,
27
+ name: this.name,
28
+ });
29
+ }
30
+ toValueType(value) {
31
+ return (0, getStartOfDay_1.default)(value);
32
+ }
16
33
  }
17
34
  exports.default = DateField;
35
+ function validateDateValue(options) {
36
+ const { value, isRequired, name } = options;
37
+ if ((0, isUndefinedOrNull_1.default)(value) && !isRequired) {
38
+ return [];
39
+ }
40
+ if (typeof value === 'number' || value instanceof Date) {
41
+ return [];
42
+ }
43
+ else {
44
+ return [
45
+ {
46
+ name,
47
+ code: 'INVALID_PARAMETER',
48
+ },
49
+ ];
50
+ }
51
+ }
52
+ exports.validateDateValue = validateDateValue;
@@ -1,10 +1,6 @@
1
1
  import { FieldDefinition } from './field.static.types';
2
- export interface DateFieldValue extends Date {
3
- }
2
+ export declare type DateFieldValue = number;
4
3
  export declare type DateFieldDefinition = FieldDefinition<DateFieldValue> & {
5
4
  /** * A date/time field that stores everything in GMT and handles all the timezone */
6
5
  type: 'date';
7
- options?: {
8
- DateFormat?: string;
9
- };
10
6
  };
@@ -1,7 +1,11 @@
1
+ import { FieldError } from '../errors/options.types';
1
2
  import { FieldTemplateDetailOptions, FieldTemplateDetails } from '../types/template.types';
2
3
  import AbstractField from './AbstractField';
3
4
  import { DateTimeFieldDefinition } from './DateTimeField.types';
5
+ import { ValidateOptions } from './field.static.types';
4
6
  export default class DateTimeField extends AbstractField<DateTimeFieldDefinition> {
5
7
  static get description(): string;
6
8
  static generateTemplateDetails(options: FieldTemplateDetailOptions<DateTimeFieldDefinition>): FieldTemplateDetails;
9
+ validate(value: any, options?: ValidateOptions<DateTimeFieldDefinition>): FieldError[];
10
+ toValueType(value: any): any;
7
11
  }
@@ -4,14 +4,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const AbstractField_1 = __importDefault(require("./AbstractField"));
7
+ const DateField_1 = require("./DateField");
7
8
  class DateTimeField extends AbstractField_1.default {
8
9
  static get description() {
9
10
  return 'Date and time support.';
10
11
  }
11
12
  static generateTemplateDetails(options) {
12
13
  return {
13
- valueType: `${options.importAs}.IDateTimeFieldValue${options.definition.isArray ? '[]' : ''}`,
14
+ valueType: `${options.importAs}.DateTimeFieldValue${options.definition.isArray ? '[]' : ''}`,
14
15
  };
15
16
  }
17
+ validate(value, options) {
18
+ const errors = super.validate(value, options);
19
+ if (errors.length > 0) {
20
+ return errors;
21
+ }
22
+ return (0, DateField_1.validateDateValue)({
23
+ value,
24
+ isRequired: this.isRequired,
25
+ name: this.name,
26
+ });
27
+ }
28
+ toValueType(value) {
29
+ if (value instanceof Date) {
30
+ return value.getTime();
31
+ }
32
+ return value;
33
+ }
16
34
  }
17
35
  exports.default = DateTimeField;
@@ -1,7 +1,5 @@
1
1
  import { FieldDefinition } from './field.static.types';
2
- export interface DateTimeFieldValue {
3
- gmt: string;
4
- }
2
+ export declare type DateTimeFieldValue = number;
5
3
  export declare type DateTimeFieldDefinition = FieldDefinition<DateTimeFieldValue> & {
6
4
  /** * A date/time field that stores everything in GMT and handles all the timezone */
7
5
  type: 'dateTime';
package/build/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export * from './StaticSchemaEntityImplementation';
2
2
  import StaticSchemaEntityImplementation from './StaticSchemaEntityImplementation';
3
3
  import { FieldRegistration } from './utilities/registerFieldType';
4
4
  export default StaticSchemaEntityImplementation;
5
- export { default as validationErrorAssertUtil } from './tests/validationErrorAssert.utility';
5
+ export { default as validationErrorAssert } from './tests/validationErrorAssert.utility';
6
6
  export { default as selectAssertUtil } from './tests/selectAssert.utility';
7
7
  export * from './schemas.static.types';
8
8
  export * from './fields';
package/build/index.js CHANGED
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  return (mod && mod.__esModule) ? mod : { "default": mod };
14
14
  };
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.mapFieldErrorsToParameterErrors = exports.assertOptions = exports.fieldRegistrations = exports.SchemaRegistry = exports.SchemaEntityFactory = exports.FieldFactory = exports.normalizeSchemaToIdWithVersion = exports.isIdWithVersion = exports.getFields = exports.formatPhoneNumber = exports.areSchemasTheSame = exports.isSchemaValid = exports.validateSchema = exports.dropPrivateFields = exports.dropFields = exports.makeFieldsOptional = exports.areSchemaValuesValid = exports.normalizeSchemaValues = exports.validateSchemaValues = exports.defaultSchemaValues = exports.registerFieldType = exports.buildSchema = exports.buildErrorSchema = exports.SchemaError = exports.selectAssertUtil = exports.validationErrorAssertUtil = void 0;
16
+ exports.mapFieldErrorsToParameterErrors = exports.assertOptions = exports.fieldRegistrations = exports.SchemaRegistry = exports.SchemaEntityFactory = exports.FieldFactory = exports.normalizeSchemaToIdWithVersion = exports.isIdWithVersion = exports.getFields = exports.formatPhoneNumber = exports.areSchemasTheSame = exports.isSchemaValid = exports.validateSchema = exports.dropPrivateFields = exports.dropFields = exports.makeFieldsOptional = exports.areSchemaValuesValid = exports.normalizeSchemaValues = exports.validateSchemaValues = exports.defaultSchemaValues = exports.registerFieldType = exports.buildSchema = exports.buildErrorSchema = exports.SchemaError = exports.selectAssertUtil = exports.validationErrorAssert = void 0;
17
17
  __exportStar(require("./StaticSchemaEntityImplementation"), exports);
18
18
  const addressField_addon_1 = __importDefault(require("./addons/addressField.addon"));
19
19
  const booleanField_addon_1 = __importDefault(require("./addons/booleanField.addon"));
@@ -33,7 +33,7 @@ const textField_addon_1 = __importDefault(require("./addons/textField.addon"));
33
33
  const StaticSchemaEntityImplementation_1 = __importDefault(require("./StaticSchemaEntityImplementation"));
34
34
  exports.default = StaticSchemaEntityImplementation_1.default;
35
35
  var validationErrorAssert_utility_1 = require("./tests/validationErrorAssert.utility");
36
- Object.defineProperty(exports, "validationErrorAssertUtil", { enumerable: true, get: function () { return __importDefault(validationErrorAssert_utility_1).default; } });
36
+ Object.defineProperty(exports, "validationErrorAssert", { enumerable: true, get: function () { return __importDefault(validationErrorAssert_utility_1).default; } });
37
37
  var selectAssert_utility_1 = require("./tests/selectAssert.utility");
38
38
  Object.defineProperty(exports, "selectAssertUtil", { enumerable: true, get: function () { return __importDefault(selectAssert_utility_1).default; } });
39
39
  __exportStar(require("./schemas.static.types"), exports);
@@ -0,0 +1,10 @@
1
+ import StaticSchemaEntityImplementation from '..';
2
+ import AbstractSchemaTest from '../AbstractSchemaTest';
3
+ export default abstract class AbstractDateFieldTest extends AbstractSchemaTest {
4
+ protected static schema: any;
5
+ protected static entity: StaticSchemaEntityImplementation<any>;
6
+ protected static beforeEach(): Promise<void>;
7
+ protected static canSetDateAsDateObject(): void;
8
+ protected static failsValidationIfPassedString(fieldName: any, value: any): void;
9
+ protected static honorsRequired(): void;
10
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const test_1 = require("@sprucelabs/test");
7
+ const __1 = require("..");
8
+ const AbstractSchemaTest_1 = __importDefault(require("../AbstractSchemaTest"));
9
+ const SchemaEntityFactory_1 = __importDefault(require("../factories/SchemaEntityFactory"));
10
+ class AbstractDateFieldTest extends AbstractSchemaTest_1.default {
11
+ static async beforeEach() {
12
+ await super.beforeEach();
13
+ this.entity = SchemaEntityFactory_1.default.Entity(this.schema);
14
+ }
15
+ static canSetDateAsDateObject() {
16
+ //@ts-ignore
17
+ this.entity.set('optionalBirthday', new Date());
18
+ }
19
+ static failsValidationIfPassedString(fieldName, value) {
20
+ const err = test_1.assert.doesThrow(() =>
21
+ //@ts-ignore
22
+ this.entity.set(fieldName, value));
23
+ __1.validationErrorAssert.assertError(err, { invalid: [fieldName] });
24
+ }
25
+ static honorsRequired() {
26
+ test_1.assert.isFalse(this.entity.isValid());
27
+ this.entity.set('requiredBirthday', new Date().getTime());
28
+ test_1.assert.isTrue(this.entity.isValid());
29
+ }
30
+ }
31
+ exports.default = AbstractDateFieldTest;
@@ -4,7 +4,7 @@ declare type ValidationErrorAssertOptions = {
4
4
  unexpected?: string[];
5
5
  };
6
6
  export declare type ValidationErrorAssertOptionsKey = keyof ValidationErrorAssertOptions;
7
- declare const validationErrorAssertUtil: {
7
+ declare const validationErrorAssert: {
8
8
  assertError(error: any, options: ValidationErrorAssertOptions): void;
9
9
  };
10
- export default validationErrorAssertUtil;
10
+ export default validationErrorAssert;
@@ -22,7 +22,7 @@ function flattenFields(fieldErrors, flattened, namePrefix = '') {
22
22
  }
23
23
  });
24
24
  }
25
- const validationErrorAssertUtil = {
25
+ const validationErrorAssert = {
26
26
  assertError(error, options) {
27
27
  var _a, _b;
28
28
  const missing = [];
@@ -71,4 +71,4 @@ const validationErrorAssertUtil = {
71
71
  }
72
72
  },
73
73
  };
74
- exports.default = validationErrorAssertUtil;
74
+ exports.default = validationErrorAssert;
@@ -1,2 +1,2 @@
1
- declare const assertOptions: <Options extends Record<string, any>>(options: Options, toCheck: string[]) => Options;
1
+ declare const assertOptions: <Options extends Record<string, any>>(options: Options, toCheck: (keyof Options)[]) => Options;
2
2
  export default assertOptions;
@@ -8,7 +8,7 @@ const assertOptions = function (options, toCheck) {
8
8
  const missing = [];
9
9
  for (const check of toCheck) {
10
10
  //@ts-ignore
11
- if (!(options === null || options === void 0 ? void 0 : options[check])) {
11
+ if (typeof (options === null || options === void 0 ? void 0 : options[check]) === 'undefined') {
12
12
  missing.push(check);
13
13
  }
14
14
  }
@@ -0,0 +1 @@
1
+ export default function getStartOfDay(timestamp?: number): number;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function getStartOfDay(timestamp) {
4
+ if (!timestamp) {
5
+ timestamp = new Date().getTime();
6
+ }
7
+ const date = new Date(timestamp);
8
+ date.setUTCHours(0, 0, 0, 0);
9
+ return date.getTime();
10
+ }
11
+ exports.default = getStartOfDay;
@@ -0,0 +1 @@
1
+ export default function isUndefinedOrNull(value: any): boolean;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function isUndefinedOrNull(value) {
4
+ return typeof value === 'undefined' || value === null;
5
+ }
6
+ exports.default = isUndefinedOrNull;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "!build/__tests__",
9
9
  "esm"
10
10
  ],
11
- "version": "25.6.123",
11
+ "version": "26.0.1",
12
12
  "main": "./build/index.js",
13
13
  "types": "./build/index.d.ts",
14
14
  "module": "./build/esm/index.js",
@@ -74,7 +74,7 @@
74
74
  "@sprucelabs/jest-sheets-reporter": "^1.2.318",
75
75
  "@sprucelabs/resolve-path-aliases": "^1.0.256",
76
76
  "@sprucelabs/semantic-release": "^4.0.8",
77
- "@sprucelabs/spruce-test-fixtures": "^23.0.42",
77
+ "@sprucelabs/spruce-test-fixtures": "^23.0.43",
78
78
  "@sprucelabs/test-utils": "^3.0.393",
79
79
  "@types/lodash": "^4.14.178",
80
80
  "@types/mime-db": "^1.43.1",