mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Add utils for validating slugs & languages
This commit is contained in:
parent
02ebb6b3f6
commit
97d9abca62
7
api/src/models/utils/index.ts
Normal file
7
api/src/models/utils/index.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export const ref = (id: string) => `#/components/schemas/${id}`;
|
||||
|
||||
export * from "./external-id";
|
||||
export * from "./genres";
|
||||
export * from "./image";
|
||||
export * from "./language";
|
||||
export * from "./resource";
|
28
api/src/models/utils/language.ts
Normal file
28
api/src/models/utils/language.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { FormatRegistry } from "@sinclair/typebox";
|
||||
import { t } from "elysia";
|
||||
import { comment } from "../../utils";
|
||||
|
||||
FormatRegistry.Set("language", (lang) => {
|
||||
try {
|
||||
const normalized = new Intl.Locale(lang).baseName;
|
||||
// TODO: we should actually replace the locale with normalized if we managed to parse it but transforms aren't working
|
||||
return lang === normalized;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
type StringProps = NonNullable<Parameters<typeof t.String>[0]>;
|
||||
|
||||
// TODO: format validation doesn't work in record's key. We should have a proper way to check that.
|
||||
export const Language = (props?: StringProps) =>
|
||||
t.String({
|
||||
format: "language",
|
||||
description: comment`
|
||||
${props?.description ?? ""}
|
||||
This is a BCP 47 language code (the IETF Best Current Practices on Tags for Identifying Languages).
|
||||
BCP 47 is also known as RFC 5646. It subsumes ISO 639 and is backward compatible with it.
|
||||
`,
|
||||
error: "Expected a valid (and NORMALIZED) bcp-47 language code.",
|
||||
...props,
|
||||
});
|
11
api/src/models/utils/resource.ts
Normal file
11
api/src/models/utils/resource.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { FormatRegistry } from "@sinclair/typebox";
|
||||
import { t } from "elysia";
|
||||
|
||||
FormatRegistry.Set("slug", (slug) => {
|
||||
return /^[a-z0-9-]+$/g.test(slug);
|
||||
});
|
||||
|
||||
export const Resource = t.Object({
|
||||
id: t.String({ format: "uuid" }),
|
||||
slug: t.String({ format: "slug" }),
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user