Skip to content

Commit

Permalink
feat(date-time): Added the day of the month validation to date classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Oct 20, 2024
1 parent bad6d0d commit 734d0c8
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 63 deletions.
2 changes: 1 addition & 1 deletion packages/date-time/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This package is part of the ⚡<b>Storm Stack</b> monorepo. Storm Stack packages

<h3 align="center">💻 Visit <a href="https://stormsoftware.com" target="_blank">stormsoftware.com</a> to stay up to date with this developer</h3><br />

[![Version](https://img.shields.io/badge/version-1.26.0-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)
[![Version](https://img.shields.io/badge/version-1.27.0-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)


> [!IMPORTANT]
Expand Down
21 changes: 14 additions & 7 deletions packages/date-time/src/storm-date-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import { RFC_3339_DATETIME_REGEX } from "./constants";
import { DateTimeErrorCode } from "./errors";
import { isInstant } from "./utilities/is-instant";
import { validateDayOfMonth } from "./utilities/validate-day-of-month";

/**
* The options to use when creating a new DateTime object
Expand All @@ -50,7 +51,7 @@ export interface DateTimeOptions {
*
* @defaultValue false
*/
skipDefaulting?: boolean;
defaultToNow?: boolean;
}

/**
Expand Down Expand Up @@ -195,7 +196,7 @@ export class StormDateTime extends Date {
*/
public static validate(
value?: DateTimeInput,
_options?: DateTimeOptions
options?: DateTimeOptions
): ValidationDetails | null {
if (StormDateTime.isDateTime(value)) {
return value.validate();
Expand Down Expand Up @@ -252,8 +253,7 @@ export class StormDateTime extends Date {
};
}

// Success - Valid
return null;
return validateDayOfMonth(StormDateTime.create(value, options));
}

/**
Expand Down Expand Up @@ -286,7 +286,7 @@ export class StormDateTime extends Date {
options.calendar ??= new Intl.DateTimeFormat().resolvedOptions().calendar;

const input = dateTime;
if (!_dateTime && !options?.skipDefaulting) {
if (!_dateTime && options?.defaultToNow) {
_dateTime = Temporal.Now.instant();
}

Expand Down Expand Up @@ -370,12 +370,19 @@ export class StormDateTime extends Date {
}

/**
* An accessor that returns the `isValid` boolean of the DateTime object
* An accessor that returns the `valid` boolean of the DateTime object
*/
public get isValid(): boolean {
public get valid(): boolean {
return this.validate() === null;
}

/**
* An accessor that returns the `invalid` boolean of the DateTime object
*/
public get invalid(): boolean {
return !this.valid;
}

/**
* Returns the input value used to create the DateTime object
*/
Expand Down
56 changes: 2 additions & 54 deletions packages/date-time/src/storm-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { DateTimeErrorCode } from "./errors";
import type { DateTimeInput, DateTimeOptions } from "./storm-date-time";
import { StormDateTime } from "./storm-date-time";
import { isInstant } from "./utilities/is-instant";
import { validateDayOfMonth } from "./utilities/validate-day-of-month";

/**
* Serializes a StormDate into a string
Expand Down Expand Up @@ -153,60 +154,7 @@ export class StormDate extends StormDateTime {
};
}

const createdDateTime = StormDateTime.create(value, options);
switch (createdDateTime.zonedDateTime.month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: {
if (createdDateTime.zonedDateTime.day > 31) {
return {
code: DateTimeErrorCode.invalid_day_of_month,
type: MessageType.ERROR
};
}

break;
}

case 2: {
if (
createdDateTime.zonedDateTime.day >
(createdDateTime.zonedDateTime.inLeapYear ? 29 : 28)
) {
return {
code: DateTimeErrorCode.invalid_day_of_month,
type: MessageType.ERROR
};
}

break;
}

case 4:
case 6:
case 9:
case 11: {
if (createdDateTime.zonedDateTime.day > 30) {
return {
code: DateTimeErrorCode.invalid_day_of_month,
type: MessageType.ERROR
};
}

break;
}

default: {
break;
}
}

// Success - Valid
return null;
return validateDayOfMonth(StormDateTime.create(value, options));
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/date-time/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export * from "./is-date-time";
export * from "./is-instant";
export * from "./validate-date";
export * from "./validate-date-time";
export * from "./validate-day-of-month";
export * from "./validate-time";
84 changes: 84 additions & 0 deletions packages/date-time/src/utilities/validate-day-of-month.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*-------------------------------------------------------------------
⚡ Storm Software - Storm Stack
This code was released as part of the Storm Stack project. Storm Stack
is maintained by Storm Software under the Apache-2.0 License, and is
free for commercial and private use. For more information, please visit
our licensing page.
Website: https://stormsoftware.com
Repository: https://github.com/storm-software/storm-stack
Documentation: https://stormsoftware.com/projects/storm-stack/docs
Contact: https://stormsoftware.com/contact
License: https://stormsoftware.com/projects/storm-stack/license
-------------------------------------------------------------------*/

import { ValidationDetails } from "@storm-stack/types";
import { MessageType } from "@storm-stack/types/utility-types/messages";
import { DateTimeErrorCode } from "../errors";
import { StormDateTime } from "../storm-date-time";

/**
* Validates the day of the month
*
* @param value - The StormDateTime value to validate
* @returns A ValidationDetails object if invalid, null if valid
*/
export const validateDayOfMonth = (
value: StormDateTime
): ValidationDetails | null => {
switch (value.zonedDateTime.month) {
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
case 11: {
if (value.zonedDateTime.day > 31) {
return {
code: DateTimeErrorCode.invalid_day_of_month,
type: MessageType.ERROR
};
}

break;
}

case 1: {
if (
value.zonedDateTime.day > (value.zonedDateTime.inLeapYear ? 29 : 28)
) {
return {
code: DateTimeErrorCode.invalid_day_of_month,
type: MessageType.ERROR
};
}

break;
}

case 3:
case 5:
case 8:
case 10: {
if (value.zonedDateTime.day > 30) {
return {
code: DateTimeErrorCode.invalid_day_of_month,
type: MessageType.ERROR
};
}

break;
}

default: {
break;
}
}

// Success - Valid
return null;
};
2 changes: 1 addition & 1 deletion packages/types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This package is part of the ⚡<b>Storm Stack</b> monorepo. Storm Stack packages

<h3 align="center">💻 Visit <a href="https://stormsoftware.com" target="_blank">stormsoftware.com</a> to stay up to date with this developer</h3><br />

[![Version](https://img.shields.io/badge/version-0.12.0-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)
[![Version](https://img.shields.io/badge/version-0.13.0-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with Fumadocs](https://img.shields.io/badge/documented_with-fumadocs-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://fumadocs.vercel.app/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-stack/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)


> [!IMPORTANT]
Expand Down

0 comments on commit 734d0c8

Please sign in to comment.