Skip to content

Commit

Permalink
Made references to Assets and Entries translateble by using a record …
Browse files Browse the repository at this point in the history
…of language to array of objects
  • Loading branch information
Nils-Kolvenbach committed Jun 13, 2024
1 parent 2c21217 commit 4fa6e64
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 193 deletions.
4 changes: 4 additions & 0 deletions src/baseSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,7 @@ export const translatableBooleanSchema = z.record(
z.boolean({ required_error: 'shared.translatableBooleanRequired' })
);
export type TranslatableBoolean = z.infer<typeof translatableBooleanSchema>;

export function translatableArrayOf<T extends z.ZodTypeAny>(schema: T) {
return z.record(supportedLanguageSchema, z.array(schema));
}
15 changes: 4 additions & 11 deletions src/entrySchema.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import z from 'zod';
import { objectTypeSchema, uuidSchema } from './baseSchema.js';
import { baseFileSchema } from './fileSchema.js';
import {
resolvedValueSchema,
valueSchema,
type ResolvedValue,
} from './valueSchema.js';
import { resolvedValueSchema, valueSchema } from './valueSchema.js';

export const entryFileSchema = baseFileSchema.extend({
objectType: z.literal(objectTypeSchema.Enum.entry).readonly(),
values: z.array(valueSchema),
});
export type EntryFile = z.infer<typeof entryFileSchema>;

// @see https://github.com/colinhacks/zod?tab=readme-ov-file#recursive-types
export type Entry = z.infer<typeof entryFileSchema> & {
values: ResolvedValue[];
};
export const entrySchema = entryFileSchema.extend({
values: z.lazy(() => resolvedValueSchema.array()),
}) satisfies z.ZodType<Entry>;
values: z.array(resolvedValueSchema),
});
export type Entry = z.infer<typeof entryFileSchema>;

export const entryExportSchema = entrySchema.extend({});
export type EntryExport = z.infer<typeof entryExportSchema>;
Expand Down
5 changes: 0 additions & 5 deletions src/fileSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import z from 'zod';
import {
objectTypeSchema,
supportedAssetExtensionSchema,
supportedLanguageSchema,
uuidSchema,
Expand All @@ -16,10 +15,6 @@ export const baseFileSchema = z.object({
* The ID is part of the files name.
*/
id: uuidSchema.readonly(),
/**
* The type of the file is used to identify the content structure of it
*/
objectType: objectTypeSchema.readonly(),
/**
* The timestamp of the file being created is set by the service of "objectType" while creating it
*/
Expand Down
72 changes: 28 additions & 44 deletions src/valueSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
objectTypeSchema,
supportedAssetMimeTypeSchema,
supportedLanguageSchema,
translatableArrayOf,
translatableBooleanSchema,
translatableNumberSchema,
translatableStringSchema,
Expand Down Expand Up @@ -262,49 +263,44 @@ export const valueDefinitionSchema = z.union([
]);
export type ValueDefinition = z.infer<typeof valueDefinitionSchema>;

export const valueContentReferenceToAssetSchema = z.object({
referenceObjectType: z.literal(objectTypeSchema.Enum.asset),
references: z.array(
z.object({
id: uuidSchema,
language: supportedLanguageSchema,
})
),
export const valueContentReferenceBase = z.object({
id: uuidSchema,
});

export const valueContentReferenceWithLanguageBase =
valueContentReferenceBase.extend({
language: supportedLanguageSchema,
});

export const valueContentReferenceToAssetSchema =
valueContentReferenceWithLanguageBase.extend({
objectType: z.literal(objectTypeSchema.Enum.asset),
});
export type ValueContentReferenceToAsset = z.infer<
typeof valueContentReferenceToAssetSchema
>;

export const resolvedValueContentReferenceToAssetSchema =
valueContentReferenceToAssetSchema.extend({
references: z.array(assetSchema),
});
valueContentReferenceToAssetSchema.merge(assetSchema);
export type ResolvedValueContentReferenceToAsset = z.infer<
typeof resolvedValueContentReferenceToAssetSchema
>;

export const valueContentReferenceToEntrySchema = z.object({
referenceObjectType: z.literal(objectTypeSchema.Enum.entry),
references: z.array(
z.object({
id: uuidSchema,
})
),
});
export const valueContentReferenceToEntrySchema =
valueContentReferenceBase.extend({
objectType: z.literal(objectTypeSchema.Enum.entry),
});
export type ValueContentReferenceToEntry = z.infer<
typeof valueContentReferenceToEntrySchema
>;

// @see https://github.com/colinhacks/zod?tab=readme-ov-file#recursive-types
export type ResolvedValueContentReferenceToEntry = z.infer<
typeof valueContentReferenceToEntrySchema
> & {
references: Entry[];
};
> &
z.ZodType<Entry>;
export const resolvedValueContentReferenceToEntrySchema: z.ZodType<ResolvedValueContentReferenceToEntry> =
valueContentReferenceToEntrySchema.extend({
references: z.array(z.lazy(() => entrySchema)),
});
valueContentReferenceToEntrySchema.merge(z.lazy(() => entrySchema));

Check failure on line 304 in src/valueSchema.ts

View workflow job for this annotation

GitHub Actions / Test

Type 'ZodObject<extendShape<extendShape<{ id: ZodString; }, { objectType: ZodLiteral<"entry">; }>, { id: unknown; objectType: unknown; readonly _type: unknown; readonly _output: unknown; readonly _input: unknown; ... 32 more ...; isNullable: unknown; }>, any, any, { ...; }, { ...; }>' is not assignable to type 'ZodType<ResolvedValueContentReferenceToEntry, ZodTypeDef, ResolvedValueContentReferenceToEntry>'.
// export const valueContentReferenceToSharedValueSchema = z.object({

Check failure on line 305 in src/valueSchema.ts

View workflow job for this annotation

GitHub Actions / Test

Argument of type 'ZodLazy<ZodObject<extendShape<extendShape<{ id: ZodReadonly<ZodString>; created: ZodReadonly<ZodNumber>; updated: ZodOptional<ZodNumber>; }, { ...; }>, { ...; }>, "strip", ZodTypeAny, { ...; }, { ...; }>>' is not assignable to parameter of type 'AnyZodObject'.
// referenceObjectType: z.literal(objectTypeSchema.Enum.sharedValue),
Expand Down Expand Up @@ -402,15 +398,15 @@ export const referencedValueSchema = z.object({
objectType: z.literal(objectTypeSchema.Enum.value).readonly(),
definitionId: uuidSchema.readonly(),
valueType: z.literal(ValueTypeSchema.Enum.reference).readonly(),
content: valueContentReferenceSchema,
content: translatableArrayOf(valueContentReferenceSchema),
});
export type ReferencedValue = z.infer<typeof referencedValueSchema>;

export const valueSchema = z.union([directValueSchema, referencedValueSchema]);
export type Value = z.infer<typeof valueSchema>;

export const resolvedReferencedValueSchema = referencedValueSchema.extend({
content: resolvedValueContentReferenceSchema,
content: translatableArrayOf(resolvedValueContentReferenceSchema),
});
export type ResolvedReferencedValue = z.infer<
typeof resolvedReferencedValueSchema
Expand Down Expand Up @@ -524,12 +520,12 @@ function getReferenceValueContentSchema(
switch (definition.inputType) {
case ValueInputTypeSchema.Enum.asset:
{
schema = valueContentReferenceToAssetSchema.extend({}); // Deep copy to not overwrite the base schema
schema = z.array(valueContentReferenceToAssetSchema);
}
break;
case ValueInputTypeSchema.Enum.entry:
{
schema = valueContentReferenceToEntrySchema.extend({}); // Deep copy to not overwrite the base schema
schema = z.array(valueContentReferenceToEntrySchema);
}
break;
// case ValueInputTypeSchema.Enum.sharedValue: {
Expand All @@ -550,27 +546,15 @@ function getReferenceValueContentSchema(
}

if (definition.isRequired) {
const requiredReferences = schema.shape.references.min(
1,
'shared.referenceRequired'
);
schema = schema.extend({
references: requiredReferences,
});
schema = schema.min(1, 'shared.referenceRequired');
}

if (definition.min) {
const minReferences = schema.shape.references.min(definition.min);
schema = schema.extend({
references: minReferences,
});
schema = schema.min(definition.min);
}

if (definition.max) {
const maxReferences = schema.shape.references.max(definition.max);
schema = schema.extend({
references: maxReferences,
});
schema = schema.max(definition.max);
}

return schema;
Expand Down
Loading

0 comments on commit 4fa6e64

Please sign in to comment.