Skip to content

Commit

Permalink
fix: Allow unknown attribute types
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexnortung committed May 14, 2024
1 parent 5d324d4 commit 101779e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"eslint": "^8.22.0",
"rimraf": "^4.4.1",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
"typescript": "^5.4.5"
},
"dependencies": {
"change-case": "^4.1.2",
Expand Down
60 changes: 30 additions & 30 deletions pnpm-lock.yaml

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

39 changes: 30 additions & 9 deletions src/readers/types/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod';
import { CERTAINLY_REQUIRED_KEY } from '../../constants';

export const baseAttribute = z.object({
pluginOptions: z.any(),
pluginOptions: z.any().optional(),
required: z.boolean().optional(),
[CERTAINLY_REQUIRED_KEY]: z.boolean().optional(),
});
Expand Down Expand Up @@ -32,6 +32,11 @@ export const richTextAttribute = baseAttribute.extend({

export type RichTextAttribute = z.infer<typeof richTextAttribute>;

export const blocksAttribute = baseAttribute.extend({
type: z.literal('blocks'),
// TODO: fill out the rest of the fields
});

export const jsonAttribute = baseAttribute.extend({
type: z.literal('json'),
});
Expand Down Expand Up @@ -60,7 +65,7 @@ export const decimalAttribute = baseAttribute.extend({
type: z.literal('decimal'),
});

export const numberAttribute = z.union([
export const numberAttribute = z.discriminatedUnion('type', [
integerAttribute,
floatAttribute,
bigIntAttribute,
Expand Down Expand Up @@ -88,7 +93,7 @@ export const timeAttribute = baseAttribute.extend({
type: z.literal('time'),
});

export const dateAttribute = z.union([
export const dateAttribute = z.discriminatedUnion('type', [
dateOnlyAttribute,
dateTimeAttribute,
timeAttribute,
Expand Down Expand Up @@ -147,12 +152,12 @@ export const hasManyAttribute = baseRelationAttribute.extend({
relation: z.literal('oneToMany'),
});

export const morphToManyAttribute = z.object({
export const morphToManyAttribute = baseAttribute.extend({
type: z.literal('relation'),
relation: z.literal('morphToMany'),
});

export const morphOneAttribute = z.object({
export const morphOneAttribute = baseAttribute.extend({
type: z.literal('relation'),
relation: z.literal('morphToOne'),
});
Expand Down Expand Up @@ -185,24 +190,40 @@ export const dynamiczoneAttribute = baseAttribute.extend({

export type DynamiczoneAttribute = z.infer<typeof dynamiczoneAttribute>;

export const attribute = z.union([
export const knownAttribute = z.union([
textAttribute,
emailAttribute,
uidAttribute,
richTextAttribute,
jsonAttribute,
passwordAttribute,
numberAttribute,
...numberAttribute.options,
enumAttribute,
dateAttribute,
...dateAttribute.options,
mediaAttribute,
booleanAttribute,
relationAttribute,
...relationAttribute.options,
componentAttribute,
]);

export type KnownAttribute = z.infer<typeof knownAttribute>;


export const anyAttribute = baseAttribute.extend({
type: z.custom<'any'>(val => !knownAttribute.options.some(schema => schema.shape.type.safeParse(val).success)).transform(() => 'any' as const),
}).passthrough();

export type AnyAttribute = z.infer<typeof anyAttribute>;

export const attribute = z.union([...knownAttribute.options, anyAttribute]);
// export const attribute = knownAttribute;

export type Attribute = z.infer<typeof attribute>;

export const isKnownAttribute = <T extends Attribute>(attributeObject: T): attributeObject is T & KnownAttribute => {
return attribute.safeParse(attributeObject).success;
};

export const contentTypeAttribute = z.union([attribute, dynamiczoneAttribute]);

export type ContentTypeAttribute = z.infer<typeof contentTypeAttribute>;
36 changes: 22 additions & 14 deletions tsconfig.jsonc
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
{
"compilerOptions": {
"incremental": true, /* Enable incremental compilation */
"target": "es6", /* Specify ECMAScript target version: */
"module": "commonjs", /* 'none', 'commonjs', 'amd', 'system', etc */
"declaration": true, /* Concatenate & emit output to single file.*/
"outDir": "lib", /* Redirect output to the directory. */
"types": [ "node" ],
"allowJs": true,
"resolveJsonModule": true,
"esModuleInterop": true, /* Enables intero between CommonJS and ES */
"skipLibCheck": true /* Skip type checking of declaration files. */
},
"include": [ "src/**/*" ],
"exclude": [ "types/**/*", "src/testtypes/**/*" ]
"compilerOptions": {
"incremental": true, /* Enable incremental compilation */
"target": "es6", /* Specify ECMAScript target version: */
"module": "commonjs", /* 'none', 'commonjs', 'amd', 'system', etc */
"declaration": true, /* Concatenate & emit output to single file.*/
"outDir": "lib", /* Redirect output to the directory. */
"types": [
"node"
],
"allowJs": true,
"resolveJsonModule": true,
"esModuleInterop": true, /* Enables intero between CommonJS and ES */
// "strict": true,
"skipLibCheck": true /* Skip type checking of declaration files. */
},
"include": [
"src/**/*"
],
"exclude": [
"types/**/*",
"src/testtypes/**/*"
]
}

0 comments on commit 101779e

Please sign in to comment.