Skip to content

Commit

Permalink
fix(docs): schema for uploaded config
Browse files Browse the repository at this point in the history
  • Loading branch information
fengelniederhammer committed Feb 2, 2024
1 parent 45c9827 commit ecd2f9c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/LapisV2Protected.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ChangeEvent, type FC, useContext, useEffect, useMemo } from 'react';
import { type Config, ConfigContext, type MetadataType } from './configContext.tsx';
import { ConfigContext, type MetadataType, type Schema } from './configContext.tsx';
import { sentenceCase } from 'change-case';

export function AdditionalInformationWizard() {
Expand All @@ -21,7 +21,7 @@ const MetadataDropDown: FC<MetadataDropDownProps> = ({ name, filterByType }) =>
const { config, modifyConfigField, removeConfigField } = useContext(ConfigContext);

useEffect(() => {
function setInitialAdditionalInfo(additionalInfoName: keyof Config, additionalInfoType: string) {
function setInitialAdditionalInfo(additionalInfoName: keyof Schema, additionalInfoType: string) {
const currentAdditionalInfo = config[additionalInfoName];
let metadataOfAdditionalInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function MetadataEditModal({ index, metadata }: { index: number; metadata

const [metadataType, setMetadataType] = useState<MetadataType>(metadata.type);
const [metadataName, setMetadataName] = useState(metadata.name);
const [generateIndex, setGenerateIndex] = useState(metadata.generateIndex);
const [generateIndex, setGenerateIndex] = useState(!!metadata.generateIndex);

const handleUpdateMetadata = () => {
updateMetadata({ ...metadata, name: metadataName, type: metadataType, generateIndex }, index);
Expand Down
10 changes: 7 additions & 3 deletions lapis2-docs/src/components/ConfigGenerator/UploadConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ChangeEvent, type Dispatch, type DragEvent, type SetStateAction, useState } from 'react';
import { ConfigSchema, type PartialConfig } from './configContext.tsx';
import { configSchema, type PartialConfig } from './configContext.tsx';
import { load } from 'js-yaml';

export type UploadConfigProps = {
Expand All @@ -16,9 +16,13 @@ export function UploadConfig({ setConfig }: UploadConfigProps) {
return;
}
const fileContent = fileReader.result.toString();
const config = ConfigSchema.parse(load(fileContent));
const config = configSchema.safeParse(load(fileContent));

setConfig(config);
if (config.success) {
setConfig(config.data.schema);
} else {
alert('Invalid config file: ' + config.error.message);
}
};
fileReader.readAsText(file);
};
Expand Down
40 changes: 14 additions & 26 deletions lapis2-docs/src/components/ConfigGenerator/configContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,37 @@ export type MetadataType = z.infer<typeof metadataTypeSchema>;
export const metadataSchema = z.object({
name: z.string(),
type: metadataTypeSchema,
generateIndex: z.boolean(),
generateIndex: z.boolean().optional(),
autocomplete: z.boolean().optional(),
required: z.boolean().optional(),
notSearchable: z.boolean().optional(),
});

export type Metadata = {
name: string;
type: MetadataType;
generateIndex: boolean;
autocomplete?: boolean;
required?: boolean;
notSearchable?: boolean;
};
export type Metadata = z.infer<typeof metadataSchema>;

export const featureSchema = z.object({
name: z.string(),
});
export type Feature = z.infer<typeof featureSchema>;

export const ConfigSchema = z.object({
export const schemaSchema = z.object({
instanceName: z.string(),
opennessLevel: opennessLevelSchema,
metadata: z.array(metadataSchema),
primaryKey: z.string(),
dateToSortBy: z.string(),
partitionBy: z.string(),
tableColumns: z.array(z.string()),
tableColumns: z.array(z.string()).optional(),
features: z.array(featureSchema),
});

export type Config = {
instanceName: string;
opennessLevel: OpennessLevel;
metadata: Metadata[];
primaryKey: string;
dateToSortBy: string;
partitionBy: string;
tableColumns: string[];
features: Feature[];
};
export type Schema = z.infer<typeof schemaSchema>;

export const configSchema = z.object({
schema: schemaSchema,
});

export type PartialConfig = Partial<Config> & { metadata: Metadata[] };
export type PartialConfig = Partial<Schema> & { metadata: Metadata[] };

export type ConfigContextType = {
configType: ConfigType;
Expand All @@ -78,8 +66,8 @@ export type ConfigContextType = {
addNewTableColumn: (newColumnName: string) => void;
updateTableColumn: (columnName: string, index: number) => void;
deleteTableColumn: (index: number) => void;
modifyConfigField: <T extends keyof Config>(field: T, value: Config[T]) => void;
removeConfigField: (field: keyof Config) => void;
modifyConfigField: <T extends keyof Schema>(field: T, value: Schema[T]) => void;
removeConfigField: (field: keyof Schema) => void;
modifyFeatureFields: (featureName: string, action: 'add' | 'delete') => void;
};

Expand Down Expand Up @@ -166,11 +154,11 @@ export const ConfigProvider: FC<PropsWithChildren<{ initialConfig: PartialConfig
});
};

const modifyConfigField = <T extends keyof Config>(field: T, value: Config[T]) => {
const modifyConfigField = <T extends keyof Schema>(field: T, value: Schema[T]) => {
setConfig({ ...config, [field]: value });
};

const removeConfigField = (field: keyof Config) => {
const removeConfigField = (field: keyof Schema) => {
delete config[field];
setConfig({ ...config });
};
Expand Down

0 comments on commit ecd2f9c

Please sign in to comment.