-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathschemas.ts
More file actions
53 lines (48 loc) · 2.36 KB
/
schemas.ts
File metadata and controls
53 lines (48 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { z } from 'zod'
import { PayoutType } from './types'
export const logoFormatRe = /^data:image\/(png|svg\+xml|webp|jpeg)(?:;base64)?,/
export const validatorSchema = z.object({
name: z.string(),
address: z.string().regex(/^NQ\d{2}(\s\w{4}){8}$/, 'Invalid Nimiq address format'),
fee: z.literal(null).or(z.number().min(0).max(1)).default(null),
payoutType: z.nativeEnum(PayoutType).default(PayoutType.None),
payoutSchedule: z.string().optional().default(''),
isMaintainedByNimiq: z.boolean().optional(),
description: z.string().optional(),
website: z.string().url().optional(),
logo: z.string().regex(logoFormatRe).optional(),
hasDefaultLogo: z.boolean().default(true),
accentColor: z.string().optional(),
contact: z.object({
email: z.string().email().optional(),
twitter: z.string().regex(/^@?(\w){1,15}$/).optional(),
telegram: z.string().regex(/^@?(\w){5,32}$/).optional(),
discordInvitationUrl: z.string().url().optional(),
bluesky: z.string().regex(/^@?[\w.-]{1,100}$/).optional(),
github: z.string().regex(/^@?[\w-]{1,39}$/).optional(),
linkedin: z.string().regex(/^@?[a-z0-9%-]{1,100}$/i).optional(),
facebook: z.string().regex(/^@?[\w.-]{1,100}$/).optional(),
instagram: z.string().regex(/^@?(\w){1,30}$/).optional(),
youtube: z.string().regex(/^@?(\w){1,50}$/).optional(),
}).optional(),
})
export const validatorsSchema = z.array(validatorSchema)
export type ValidatorJSON = z.infer<typeof validatorSchema>
function getDefaults<Schema extends z.ZodObject<any>>(schema: Schema) {
return Object.fromEntries(
Object.entries(schema.shape).map(([key, value]) => {
if (value instanceof z.ZodDefault)
return [key, value.parse(undefined)]
return [key, undefined]
}),
)
}
export const defaultValidatorJSON = getDefaults(validatorSchema) as ValidatorJSON
export const mainQuerySchema = z.object({
'payout-type': z.nativeEnum(PayoutType).optional(),
'only-known': z.literal('true').or(z.literal('false')).default('true').transform(v => v === 'true'),
'with-identicons': z.literal('true').or(z.literal('false')).optional().transform(v => v === undefined ? undefined : v === 'true'),
'force': z.literal('true').or(z.literal('false')).default('false').transform(v => v === 'true'),
'epoch-number': z.coerce.number().min(1).default(1),
})
export type MainQuerySchema = z.infer<typeof mainQuerySchema>