1 | import { Joi } from './joi.extensions'
|
2 | import {
|
3 | AnySchemaTyped,
|
4 | ArraySchemaTyped,
|
5 | BooleanSchemaTyped,
|
6 | ObjectSchemaTyped,
|
7 | StringSchemaTyped,
|
8 | } from './joi.model'
|
9 |
|
10 | export const booleanSchema = Joi.boolean() as BooleanSchemaTyped
|
11 | export const booleanDefaultToFalseSchema = Joi.boolean().default(false) as BooleanSchemaTyped
|
12 | export const stringSchema = Joi.string()
|
13 | export const numberSchema = Joi.number()
|
14 | export const integerSchema = Joi.number().integer()
|
15 | export const percentageSchema = Joi.number().integer().min(0).max(100)
|
16 | export const dateStringSchema = stringSchema.dateString()
|
17 | export const binarySchema = Joi.binary()
|
18 |
|
19 | export const urlSchema = (scheme: string | string[] = 'https'): StringSchemaTyped =>
|
20 | Joi.string().uri({ scheme })
|
21 |
|
22 | export function arraySchema<T>(items?: AnySchemaTyped<T, T>): ArraySchemaTyped<T> {
|
23 | return items ? Joi.array().items(items) : Joi.array()
|
24 | }
|
25 |
|
26 | export function objectSchema<IN, OUT = IN>(
|
27 | schema?: { [key in keyof Partial<IN>]: AnySchemaTyped<IN[key]> },
|
28 | ): ObjectSchemaTyped<IN, OUT> {
|
29 | return Joi.object(schema)
|
30 | }
|
31 |
|
32 | export const anySchema = Joi.any()
|
33 | export const anyObjectSchema = Joi.object().options({ stripUnknown: false })
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | export const idSchema = stringSchema
|
41 | .regex(/^[a-z0-9_]*$/)
|
42 | .min(6)
|
43 | .max(64)
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | export const SLUG_PATTERN = /^[a-z0-9-]*$/
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | export const slugSchema = stringSchema.regex(SLUG_PATTERN).min(1).max(255)
|
54 |
|
55 |
|
56 | export const unixTimestampSchema = numberSchema.integer().min(0).max(16725225600)
|
57 |
|
58 |
|
59 | export const verSchema = numberSchema.optional().integer().min(1).max(100)
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export const emailSchema = stringSchema.email().lowercase()
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | export const SEM_VER_PATTERN = /^[0-9]+\.[0-9]+\.[0-9]+$/
|
70 | export const semVerSchema = stringSchema.regex(SEM_VER_PATTERN)
|
71 |
|
72 |
|
73 | export const userAgentSchema = stringSchema
|
74 | .min(5)
|
75 | .max(400)
|
76 |
|
77 | export const utcOffsetSchema = numberSchema
|
78 | .min(-14 * 60)
|
79 | .max(14 * 60)
|
80 | .dividable(15)
|
81 |
|
82 | export const ipAddressSchema = stringSchema.ip()
|