Add schema utils

This commit is contained in:
Jamie Kyle 2024-10-02 12:03:10 -07:00 committed by GitHub
parent c8a729f8be
commit b26466e59d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
45 changed files with 674 additions and 151 deletions

View file

@ -5,6 +5,7 @@ import { parse } from 'csv-parse';
import fs from 'fs/promises';
import { z } from 'zod';
import { _getAvailableLocales } from '../../app/locale';
import { parseUnknown } from '../util/schemas';
const type = process.argv[2];
if (type !== 'countries' && type !== 'locales') {
@ -119,7 +120,7 @@ function assertValuesForAllCountries(result: LocaleDisplayNamesResult) {
async function main() {
const contents = await fs.readFile(localeDisplayNamesDataPath, 'utf-8');
const records = await parseCsv(contents);
const data = LocaleDisplayNames.parse(records);
const data = parseUnknown(LocaleDisplayNames, records as unknown);
const result = convertData(data);
if (type === 'locales') {
assertValuesForAllLocales(result);

View file

@ -9,6 +9,7 @@ import prettier from 'prettier';
import type { OptionalResourceType } from '../types/OptionalResource';
import { OptionalResourcesDictSchema } from '../types/OptionalResource';
import { parseUnknown } from '../util/schemas';
const MANIFEST_URL =
'https://updates.signal.org/dynamic/android/emoji/search/manifest.json';
@ -29,7 +30,7 @@ async function fetchJSON(url: string): Promise<unknown> {
}
async function main(): Promise<void> {
const manifest = ManifestSchema.parse(await fetchJSON(MANIFEST_URL));
const manifest = parseUnknown(ManifestSchema, await fetchJSON(MANIFEST_URL));
// eslint-disable-next-line dot-notation
manifest.languageToSmartlingLocale['zh_TW'] = 'zh-Hant';
@ -75,8 +76,9 @@ async function main(): Promise<void> {
'build',
'optional-resources.json'
);
const resources = OptionalResourcesDictSchema.parse(
JSON.parse(await readFile(resourcesPath, 'utf8'))
const resources = parseUnknown(
OptionalResourcesDictSchema,
JSON.parse(await readFile(resourcesPath, 'utf8')) as unknown
);
for (const [locale, resource] of extraResources) {

View file

@ -9,6 +9,7 @@ import prettier from 'prettier';
import type { OptionalResourceType } from '../types/OptionalResource';
import { OptionalResourcesDictSchema } from '../types/OptionalResource';
import { parseUnknown } from '../util/schemas';
const VERSION = 10;
@ -28,7 +29,10 @@ async function fetchJSON(url: string): Promise<unknown> {
}
async function main(): Promise<void> {
const { jumbomoji } = ManifestSchema.parse(await fetchJSON(MANIFEST_URL));
const { jumbomoji } = parseUnknown(
ManifestSchema,
await fetchJSON(MANIFEST_URL)
);
const extraResources = new Map<string, OptionalResourceType>();
@ -68,8 +72,9 @@ async function main(): Promise<void> {
'build',
'optional-resources.json'
);
const resources = OptionalResourcesDictSchema.parse(
JSON.parse(await readFile(resourcesPath, 'utf8'))
const resources = parseUnknown(
OptionalResourcesDictSchema,
JSON.parse(await readFile(resourcesPath, 'utf8')) as unknown
);
for (const [sheet, resource] of extraResources) {

View file

@ -13,6 +13,7 @@ import logSymbols from 'log-symbols';
import { explodePromise } from '../util/explodePromise';
import { missingCaseError } from '../util/missingCaseError';
import { SECOND } from '../util/durations';
import { parseUnknown } from '../util/schemas';
const ROOT_DIR = join(__dirname, '..', '..');
@ -137,7 +138,10 @@ async function launchElectron(
return;
}
const event = eventSchema.parse(JSON.parse(match[1]));
const event = parseUnknown(
eventSchema,
JSON.parse(match[1]) as unknown
);
if (event.type === 'pass') {
pass += 1;