-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
94 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,57 @@ | ||
import type { ImgLink } from "components/ImgEditor"; | ||
import { genFileSchema } from "schemas/file"; | ||
import { schema as schemaFn, stringNumber } from "schemas/shape"; | ||
import { requiredString } from "schemas/string"; | ||
import { array, string } from "yup"; | ||
import { MAX_SIZE_IN_BYTES, VALID_MIME_TYPES } from "../common"; | ||
import type { FormValues as FV } from "./types"; | ||
import * as v from "valibot"; | ||
import { MAX_SIZE_IN_BYTES, VALID_MIME_TYPES, target } from "../common"; | ||
|
||
const fileObj = schemaFn<ImgLink>({ | ||
file: genFileSchema(MAX_SIZE_IN_BYTES, VALID_MIME_TYPES).required("required"), | ||
const str = v.pipe(v.string(), v.trim()); | ||
|
||
const fileObject = v.object({ | ||
name: str, | ||
publicUrl: v.pipe(str, v.url()), | ||
}); | ||
|
||
export const imgLink = v.object({ | ||
file: v.optional( | ||
v.pipe( | ||
v.file(), | ||
v.mimeType(VALID_MIME_TYPES, "invalid type"), | ||
v.maxSize(MAX_SIZE_IN_BYTES, "exceeds size limit") | ||
) | ||
), | ||
preview: v.pipe(str, v.url()), | ||
...fileObject.entries, | ||
}); | ||
|
||
const targetTypeKey: keyof FV = "targetType"; | ||
export const endowOption = v.object({ | ||
id: v.number(), | ||
name: str, | ||
logo: v.optional(v.pipe(str, v.url())), | ||
}); | ||
|
||
export const schema = schemaFn<FV>({ | ||
name: requiredString, | ||
description: requiredString, | ||
banner: fileObj, | ||
logo: fileObj, | ||
members: array().min(1, "must contain at least one endowment"), | ||
expiration: string() | ||
.transform((v) => { | ||
if (!v) return ""; | ||
return new Date(v).toISOString(); | ||
}) | ||
.datetime("invalid date") | ||
.test( | ||
"", | ||
"must be in the future", | ||
(v) => !v || v >= new Date().toISOString() | ||
), | ||
export const settings = v.object({ | ||
from: str, | ||
allowBgTip: v.boolean(), | ||
}); | ||
|
||
fixedTarget: stringNumber( | ||
(s) => | ||
s.when(targetTypeKey, (values, schema) => { | ||
const [type] = values as [FV["targetType"]]; | ||
return type === "fixed" ? schema.required("required") : schema; | ||
}), | ||
(n) => n.positive("must be greater than 0") | ||
export const schema = v.object({ | ||
name: v.pipe(str, v.nonEmpty("required")), | ||
description: v.pipe(str, v.nonEmpty("required")), | ||
banner: imgLink, | ||
logo: imgLink, | ||
members: v.pipe( | ||
v.array(endowOption), | ||
v.minLength(1, "must contain at least one endowment") | ||
), | ||
featured: v.boolean(), | ||
settings, | ||
expiration: v.pipe( | ||
str, | ||
v.nonEmpty("required"), | ||
v.transform((v) => new Date(v).toISOString()), | ||
v.isoTimestamp("invalid date"), | ||
v.minValue(new Date().toISOString(), "must be in the future") | ||
), | ||
target, | ||
}); | ||
|
||
export interface FundMember extends v.InferOutput<typeof endowOption> {} | ||
export interface EndowOption extends FundMember {} | ||
export type FV = v.InferOutput<typeof schema>; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as v from "valibot"; | ||
export const target = v.variant("type", [ | ||
v.object({ | ||
type: v.literal("fixed"), | ||
value: v.pipe( | ||
v.string("required"), | ||
v.nonEmpty("required"), | ||
v.transform((x) => +x), | ||
v.number("invalid number"), | ||
v.minValue(0, "must be greater than 0"), | ||
/** so that the inferred type is string */ | ||
v.transform((x) => x.toString()) | ||
), | ||
}), | ||
v.object({ type: v.literal("smart"), value: v.optional(v.string()) }), | ||
v.object({ type: v.literal("none"), value: v.optional(v.string()) }), | ||
]); | ||
|
||
export type Target = v.InferOutput<typeof target>; | ||
export type TargetType = Target["type"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters