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