Skip to content

Commit 1af7816

Browse files
Merge pull request #17 from aniketbiswas21/Date
✨ feat(Parser): Added Date parser and corresponding tests
2 parents ef5a68f + 6037002 commit 1af7816

File tree

11 files changed

+367
-3
lines changed

11 files changed

+367
-3
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export { typeCheck as type } from "./typeCheck";
2+
export {
3+
greaterThanCheck as greaterThan,
4+
greaterThanOrEqualToCheck as greaterThanOrEqual,
5+
lessThanCheck as lessThan,
6+
lessThanOrEqualToCheck as lessThanOrEqual,
7+
equalToCheck as equalTo,
8+
} from "./valueCheck";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { DateSchemaConfig } from "../types/DateSchema";
2+
import {
3+
DateSchemaError,
4+
DateSchemaErrorEnum as msg,
5+
} from "../types/DateError";
6+
import * as check from "./allChecks";
7+
8+
const checkRunner = (
9+
x: any,
10+
config: DateSchemaConfig,
11+
key: string
12+
): { value: Date; errors: DateSchemaError[] } => {
13+
const {
14+
greaterThan,
15+
greaterThanOrEqualTo,
16+
lessThan,
17+
lessThanOrEqualTo,
18+
equalTo,
19+
} = config;
20+
const errors: DateSchemaError[] = [];
21+
22+
// type check
23+
if (!check.type(x)) {
24+
errors.push({
25+
error: `${key} must be a Date object`,
26+
errorType: msg.Type,
27+
});
28+
return { value: x, errors };
29+
}
30+
31+
// value check
32+
if (greaterThan && !check.greaterThan(x, greaterThan)) {
33+
errors.push({
34+
error: `${key} must be greater than ${greaterThan}`,
35+
errorType: msg.Value,
36+
});
37+
}
38+
if (
39+
greaterThanOrEqualTo &&
40+
!check.greaterThanOrEqual(x, greaterThanOrEqualTo)
41+
) {
42+
errors.push({
43+
error: `${key} must be greater than or equal to ${greaterThanOrEqualTo}`,
44+
errorType: msg.Value,
45+
});
46+
}
47+
if (lessThan && !check.lessThan(x, lessThan)) {
48+
errors.push({
49+
error: `${key} must be less than ${lessThan}`,
50+
errorType: msg.Value,
51+
});
52+
}
53+
if (lessThanOrEqualTo && !check.lessThanOrEqual(x, lessThanOrEqualTo)) {
54+
errors.push({
55+
error: `${key} must be less than or equal to ${lessThanOrEqualTo}`,
56+
errorType: msg.Value,
57+
});
58+
}
59+
if (equalTo && !check.equalTo(x, equalTo)) {
60+
errors.push({
61+
error: `${key} must be equal to ${equalTo}`,
62+
errorType: msg.Value,
63+
});
64+
}
65+
66+
return { value: x, errors };
67+
};
68+
69+
export default checkRunner;
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const typeCheck = (x: any): x is Date => {
2+
if (x instanceof Date) return true;
3+
4+
return false;
5+
};
6+
7+
export { typeCheck };
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const greaterThanCheck = (x: Date, threshold: Date): boolean => {
2+
if (x.getTime() > threshold.getTime()) return true;
3+
4+
return false;
5+
};
6+
7+
const greaterThanOrEqualToCheck = (x: Date, threshold: Date): boolean => {
8+
if (x.getTime() >= threshold.getTime()) return true;
9+
10+
return false;
11+
};
12+
13+
const lessThanCheck = (x: Date, threshold: Date): boolean => {
14+
if (x.getTime() < threshold.getTime()) return true;
15+
16+
return false;
17+
};
18+
19+
const lessThanOrEqualToCheck = (x: Date, threshold: Date): boolean => {
20+
if (x.getTime() <= threshold.getTime()) return true;
21+
22+
return false;
23+
};
24+
25+
const equalToCheck = (x: Date, threshold: Date): boolean => {
26+
if (x.getTime() === threshold.getTime()) return true;
27+
28+
return false;
29+
};
30+
31+
export {
32+
greaterThanCheck,
33+
greaterThanOrEqualToCheck,
34+
lessThanCheck,
35+
lessThanOrEqualToCheck,
36+
equalToCheck,
37+
};

src/SchemaTypes/Date/class.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import SchemaType from "../class";
2+
import checkRunner from "./checks/checksRunner";
3+
import { DateSchemaConfig } from "./types/DateSchema";
4+
import { DateSchemaError } from "./types/DateError";
5+
6+
class DateSchema extends SchemaType<Date> {
7+
protected override __config: DateSchemaConfig = {};
8+
9+
public greaterThan = (x: Date): this => {
10+
this.__config.greaterThan = x;
11+
return this;
12+
};
13+
14+
public greaterThanOrEqualTo = (x: Date): this => {
15+
this.__config.greaterThanOrEqualTo = x;
16+
return this;
17+
};
18+
19+
public lessThan = (x: Date): this => {
20+
this.__config.lessThan = x;
21+
return this;
22+
};
23+
24+
public lessThanOrEqualTo = (x: Date): this => {
25+
this.__config.lessThanOrEqualTo = x;
26+
return this;
27+
};
28+
29+
public equalTo = (x: Date): this => {
30+
this.__config.equalTo = x;
31+
return this;
32+
};
33+
34+
validate = (
35+
x: any,
36+
key: string = "value"
37+
): {
38+
value: Date;
39+
valid: boolean;
40+
errors: DateSchemaError[];
41+
} => {
42+
const { value, errors } = checkRunner(x, this.__config, key);
43+
44+
return {
45+
valid: Boolean(errors.length === 0),
46+
value,
47+
errors,
48+
};
49+
};
50+
}
51+
52+
export default DateSchema;
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import BaseError from "../../types/BaseError";
2+
3+
enum DateSchemaErrorEnum {
4+
None = "None",
5+
Type = "Date/Type",
6+
Value = "Date/InvalidValue",
7+
}
8+
9+
type DateSchemaError = BaseError<DateSchemaErrorEnum>;
10+
11+
export { DateSchemaError, DateSchemaErrorEnum };
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import BaseSchemaConfig from "../../types/BaseSchema";
2+
3+
interface DateSchemaConfig extends BaseSchemaConfig<Date> {
4+
greaterThan?: Date;
5+
greaterThanOrEqualTo?: Date;
6+
lessThan?: Date;
7+
lessThanOrEqualTo?: Date;
8+
equalTo?: Date;
9+
}
10+
11+
export { DateSchemaConfig };

src/SchemaTypes/class.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { firestore } from "firebase-admin";
12
import BaseError from "./types/BaseError";
23
import BaseSchemaConfig from "./types/BaseSchema";
34
import { KeyValueStore } from "./Object/types/KeyValue";
@@ -6,7 +7,7 @@ type ValidateFn<T> = (
67
x: any,
78
key: string
89
) => {
9-
value: T | KeyValueStore;
10+
value: T | KeyValueStore | firestore.Timestamp;
1011
valid: boolean;
1112
errors: BaseError[];
1213
};

src/SchemaTypes/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import StringSchemaType from "./String/class";
22
import NumberSchemaType from "./Number/class";
33
import BooleanSchemaType from "./Boolean/class";
4+
import DateSchemaType from "./Date/class";
45
import ObjectSchemaType from "./Object/class";
56
import { KeyValueStore } from "./Object/types/KeyValue";
67

78
const string = () => new StringSchemaType();
89
const number = () => new NumberSchemaType();
910
const boolean = () => new BooleanSchemaType();
11+
const date = () => new DateSchemaType();
1012
const object = <T extends KeyValueStore = any>() => new ObjectSchemaType<T>();
1113

12-
export { string, number, boolean, object };
14+
export { string, number, boolean, date, object };

src/__tests__/SchemaTypes/Boolean/class.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe("test Boolean Schema Parser", () => {
3737
const { valid, value, errors } = objectSchema.validate(testObj);
3838

3939
expect(valid).toBe(false);
40-
expect(value).toEqual({});
40+
expect(value).toEqual(testObj);
4141
expect(errors).toEqual([
4242
{
4343
error: "isBoolean must be a boolean",

0 commit comments

Comments
 (0)