Skip to content

Commit cab462f

Browse files
committed
fix: schema utils
1 parent 775480b commit cab462f

File tree

8 files changed

+88
-15
lines changed

8 files changed

+88
-15
lines changed

src/schemas/authFactor.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { numericId } from "../utils/schema"
2+
3+
export const idSchema = numericId()

src/schemas/klass.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { uppercaseAlphanumericString, alphaString } from "../utils/schema"
2+
3+
export const idSchema = uppercaseAlphanumericString().min(5).max(5)
4+
5+
export const nameSchema = alphaString({ spaces: true }).max(200)

src/schemas/otpBypassToken.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { numericId } from "../utils/schema"
2+
3+
export const idSchema = numericId()

src/schemas/school.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { alphaString, numericId } from "../utils/schema"
2+
3+
export const idSchema = numericId()
4+
5+
export const nameSchema = alphaString({ spaces: true }).max(200)

src/schemas/student.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { numericId } from "../utils/schema"
2+
3+
export const idSchema = numericId()

src/schemas/teacher.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { numericId } from "../utils/schema"
2+
3+
export const idSchema = numericId()

src/schemas/user.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
import { alphaString } from "../utils/schema"
1+
import { alphaString, numericId } from "../utils/schema"
2+
3+
export const idSchema = numericId()
24

35
export const firstNameSchema = alphaString().max(150)
6+
7+
export const lastNameSchema = alphaString().max(150)

src/utils/schema.ts

+61-14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
type TypeFromShape,
1111
type ValidateOptions,
1212
type StringSchema,
13+
type NumberSchema,
1314
string as YupString,
15+
number as YupNumber,
1416
} from "yup"
1517

1618
export type _<T> = T extends {}
@@ -32,6 +34,10 @@ export type ObjectSchemaFromShape<Shape extends ObjectShape> = ObjectSchema<
3234
""
3335
>
3436

37+
export function numericId(schema: NumberSchema = YupNumber()) {
38+
return schema.min(1)
39+
}
40+
3541
// -----------------------------------------------------------------------------
3642
// Limited Character Sets
3743
// -----------------------------------------------------------------------------
@@ -44,39 +50,80 @@ export function limitCharSet(
4450
return schema.matches(new RegExp(`^[${charSet}]*$`), message)
4551
}
4652

47-
export function alphaString(schema: StringSchema = YupString()) {
48-
return limitCharSet(
53+
type CharSetOptions = Partial<{
54+
schema: StringSchema
55+
spaces: boolean
56+
specialChars: string
57+
}>
58+
59+
function charSet(
60+
charSet: string,
61+
message: string,
62+
options: CharSetOptions = {},
63+
) {
64+
const { schema, spaces = false, specialChars } = options
65+
66+
if (spaces) {
67+
charSet += " "
68+
message += ", spaces"
69+
}
70+
if (specialChars) {
71+
charSet += specialChars
72+
message += `, special characters (${specialChars})`
73+
}
74+
75+
return limitCharSet(charSet, message, schema)
76+
}
77+
78+
export function alphaString(options?: CharSetOptions) {
79+
return charSet(
4980
"a-zA-Z",
5081
"can only contain alpha characters (a-z, A-Z)",
51-
schema,
82+
options,
5283
)
5384
}
5485

55-
export function lowercaseAlphaString(schema: StringSchema = YupString()) {
56-
return limitCharSet(
86+
export function lowercaseAlphaString(options?: CharSetOptions) {
87+
return charSet(
5788
"a-z",
5889
"can only contain lowercase alpha characters (a-z)",
59-
schema,
90+
options,
6091
)
6192
}
6293

63-
export function uppercaseAlphaString(schema: StringSchema = YupString()) {
64-
return limitCharSet(
94+
export function lowercaseAlphanumericString(options?: CharSetOptions) {
95+
return charSet(
96+
"a-z0-9",
97+
"can only contain lowercase alphanumeric characters (a-z, 0-9)",
98+
options,
99+
)
100+
}
101+
102+
export function uppercaseAlphaString(options?: CharSetOptions) {
103+
return charSet(
65104
"A-Z",
66105
"can only contain uppercase alpha characters (A-Z)",
67-
schema,
106+
options,
107+
)
108+
}
109+
110+
export function uppercaseAlphanumericString(options?: CharSetOptions) {
111+
return charSet(
112+
"A-Z0-9",
113+
"can only contain uppercase alphanumeric characters (A-Z, 0-9)",
114+
options,
68115
)
69116
}
70117

71-
export function numericString(schema: StringSchema = YupString()) {
72-
return limitCharSet("0-9", "can only contain numbers (0-9)", schema)
118+
export function numericString(options?: CharSetOptions) {
119+
return charSet("0-9", "can only contain numbers (0-9)", options)
73120
}
74121

75-
export function alphanumericString(schema: StringSchema = YupString()) {
76-
return limitCharSet(
122+
export function alphanumericString(options?: CharSetOptions) {
123+
return charSet(
77124
"a-zA-Z0-9",
78125
"can only contain alphanumeric characters (a-z, A-Z, 0-9)",
79-
schema,
126+
options,
80127
)
81128
}
82129

0 commit comments

Comments
 (0)