From 101779e348c6e3e8bd72e492fc4d748221ee4a00 Mon Sep 17 00:00:00 2001 From: Alexander Nortung Date: Tue, 14 May 2024 13:51:33 +0200 Subject: [PATCH] fix: Allow unknown attribute types --- package.json | 2 +- pnpm-lock.yaml | 60 ++++++++++++++++----------------- src/readers/types/attributes.ts | 39 ++++++++++++++++----- tsconfig.jsonc | 36 ++++++++++++-------- 4 files changed, 83 insertions(+), 54 deletions(-) diff --git a/package.json b/package.json index a960f88..f02bb89 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 32cb27c..3d6d436 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -27,10 +27,10 @@ devDependencies: version: 2.7.0 '@typescript-eslint/eslint-plugin': specifier: ^5.33.1 - version: 5.33.1(@typescript-eslint/parser@5.33.1)(eslint@8.22.0)(typescript@4.7.4) + version: 5.33.1(@typescript-eslint/parser@5.33.1)(eslint@8.22.0)(typescript@5.4.5) '@typescript-eslint/parser': specifier: ^5.33.1 - version: 5.33.1(eslint@8.22.0)(typescript@4.7.4) + version: 5.33.1(eslint@8.22.0)(typescript@5.4.5) bumpp: specifier: ^9.3.0 version: 9.3.0 @@ -42,10 +42,10 @@ devDependencies: version: 4.4.1 ts-node: specifier: ^10.9.1 - version: 10.9.1(@types/node@18.7.2)(typescript@4.7.4) + version: 10.9.1(@types/node@18.7.2)(typescript@5.4.5) typescript: - specifier: ^4.7.4 - version: 4.7.4 + specifier: ^5.4.5 + version: 5.4.5 packages: @@ -167,7 +167,7 @@ packages: resolution: {integrity: sha512-RI1L7N4JnW5gQw2spvL7Sllfuf1SaHdrZpCHiBlCXjIlufi1SMNnbu2teze3/QE67Fg2tBlH7W+mi4hVNk4p0A==} dev: true - /@typescript-eslint/eslint-plugin@5.33.1(@typescript-eslint/parser@5.33.1)(eslint@8.22.0)(typescript@4.7.4): + /@typescript-eslint/eslint-plugin@5.33.1(@typescript-eslint/parser@5.33.1)(eslint@8.22.0)(typescript@5.4.5): resolution: {integrity: sha512-S1iZIxrTvKkU3+m63YUOxYPKaP+yWDQrdhxTglVDVEVBf+aCSw85+BmJnyUaQQsk5TXFG/LpBu9fa+LrAQ91fQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -178,23 +178,23 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/parser': 5.33.1(eslint@8.22.0)(typescript@4.7.4) + '@typescript-eslint/parser': 5.33.1(eslint@8.22.0)(typescript@5.4.5) '@typescript-eslint/scope-manager': 5.33.1 - '@typescript-eslint/type-utils': 5.33.1(eslint@8.22.0)(typescript@4.7.4) - '@typescript-eslint/utils': 5.33.1(eslint@8.22.0)(typescript@4.7.4) + '@typescript-eslint/type-utils': 5.33.1(eslint@8.22.0)(typescript@5.4.5) + '@typescript-eslint/utils': 5.33.1(eslint@8.22.0)(typescript@5.4.5) debug: 4.3.4 eslint: 8.22.0 functional-red-black-tree: 1.0.1 ignore: 5.2.0 regexpp: 3.2.0 semver: 7.3.7 - tsutils: 3.21.0(typescript@4.7.4) - typescript: 4.7.4 + tsutils: 3.21.0(typescript@5.4.5) + typescript: 5.4.5 transitivePeerDependencies: - supports-color dev: true - /@typescript-eslint/parser@5.33.1(eslint@8.22.0)(typescript@4.7.4): + /@typescript-eslint/parser@5.33.1(eslint@8.22.0)(typescript@5.4.5): resolution: {integrity: sha512-IgLLtW7FOzoDlmaMoXdxG8HOCByTBXrB1V2ZQYSEV1ggMmJfAkMWTwUjjzagS6OkfpySyhKFkBw7A9jYmcHpZA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -206,10 +206,10 @@ packages: dependencies: '@typescript-eslint/scope-manager': 5.33.1 '@typescript-eslint/types': 5.33.1 - '@typescript-eslint/typescript-estree': 5.33.1(typescript@4.7.4) + '@typescript-eslint/typescript-estree': 5.33.1(typescript@5.4.5) debug: 4.3.4 eslint: 8.22.0 - typescript: 4.7.4 + typescript: 5.4.5 transitivePeerDependencies: - supports-color dev: true @@ -222,7 +222,7 @@ packages: '@typescript-eslint/visitor-keys': 5.33.1 dev: true - /@typescript-eslint/type-utils@5.33.1(eslint@8.22.0)(typescript@4.7.4): + /@typescript-eslint/type-utils@5.33.1(eslint@8.22.0)(typescript@5.4.5): resolution: {integrity: sha512-X3pGsJsD8OiqhNa5fim41YtlnyiWMF/eKsEZGsHID2HcDqeSC5yr/uLOeph8rNF2/utwuI0IQoAK3fpoxcLl2g==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -232,11 +232,11 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/utils': 5.33.1(eslint@8.22.0)(typescript@4.7.4) + '@typescript-eslint/utils': 5.33.1(eslint@8.22.0)(typescript@5.4.5) debug: 4.3.4 eslint: 8.22.0 - tsutils: 3.21.0(typescript@4.7.4) - typescript: 4.7.4 + tsutils: 3.21.0(typescript@5.4.5) + typescript: 5.4.5 transitivePeerDependencies: - supports-color dev: true @@ -246,7 +246,7 @@ packages: engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dev: true - /@typescript-eslint/typescript-estree@5.33.1(typescript@4.7.4): + /@typescript-eslint/typescript-estree@5.33.1(typescript@5.4.5): resolution: {integrity: sha512-JOAzJ4pJ+tHzA2pgsWQi4804XisPHOtbvwUyqsuuq8+y5B5GMZs7lI1xDWs6V2d7gE/Ez5bTGojSK12+IIPtXA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -261,13 +261,13 @@ packages: globby: 11.1.0 is-glob: 4.0.3 semver: 7.3.7 - tsutils: 3.21.0(typescript@4.7.4) - typescript: 4.7.4 + tsutils: 3.21.0(typescript@5.4.5) + typescript: 5.4.5 transitivePeerDependencies: - supports-color dev: true - /@typescript-eslint/utils@5.33.1(eslint@8.22.0)(typescript@4.7.4): + /@typescript-eslint/utils@5.33.1(eslint@8.22.0)(typescript@5.4.5): resolution: {integrity: sha512-uphZjkMaZ4fE8CR4dU7BquOV6u0doeQAr8n6cQenl/poMaIyJtBu8eys5uk6u5HiDH01Mj5lzbJ5SfeDz7oqMQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -276,7 +276,7 @@ packages: '@types/json-schema': 7.0.11 '@typescript-eslint/scope-manager': 5.33.1 '@typescript-eslint/types': 5.33.1 - '@typescript-eslint/typescript-estree': 5.33.1(typescript@4.7.4) + '@typescript-eslint/typescript-estree': 5.33.1(typescript@5.4.5) eslint: 8.22.0 eslint-scope: 5.1.1 eslint-utils: 3.0.0(eslint@8.22.0) @@ -1500,7 +1500,7 @@ packages: is-number: 7.0.0 dev: true - /ts-node@10.9.1(@types/node@18.7.2)(typescript@4.7.4): + /ts-node@10.9.1(@types/node@18.7.2)(typescript@5.4.5): resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==} hasBin: true peerDependencies: @@ -1526,7 +1526,7 @@ packages: create-require: 1.1.1 diff: 4.0.2 make-error: 1.3.6 - typescript: 4.7.4 + typescript: 5.4.5 v8-compile-cache-lib: 3.0.1 yn: 3.1.1 dev: true @@ -1539,14 +1539,14 @@ packages: resolution: {integrity: sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==} dev: false - /tsutils@3.21.0(typescript@4.7.4): + /tsutils@3.21.0(typescript@5.4.5): resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==} engines: {node: '>= 6'} peerDependencies: typescript: '>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta' dependencies: tslib: 1.14.1 - typescript: 4.7.4 + typescript: 5.4.5 dev: true /type-check@0.4.0: @@ -1566,9 +1566,9 @@ packages: engines: {node: '>=10'} dev: true - /typescript@4.7.4: - resolution: {integrity: sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==} - engines: {node: '>=4.2.0'} + /typescript@5.4.5: + resolution: {integrity: sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==} + engines: {node: '>=14.17'} hasBin: true dev: true diff --git a/src/readers/types/attributes.ts b/src/readers/types/attributes.ts index 655cd14..1afda4b 100644 --- a/src/readers/types/attributes.ts +++ b/src/readers/types/attributes.ts @@ -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(), }); @@ -32,6 +32,11 @@ export const richTextAttribute = baseAttribute.extend({ export type RichTextAttribute = z.infer; +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'), }); @@ -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, @@ -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, @@ -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'), }); @@ -185,24 +190,40 @@ export const dynamiczoneAttribute = baseAttribute.extend({ export type DynamiczoneAttribute = z.infer; -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; + + +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; + +export const attribute = z.union([...knownAttribute.options, anyAttribute]); +// export const attribute = knownAttribute; + export type Attribute = z.infer; +export const isKnownAttribute = (attributeObject: T): attributeObject is T & KnownAttribute => { + return attribute.safeParse(attributeObject).success; +}; + export const contentTypeAttribute = z.union([attribute, dynamiczoneAttribute]); export type ContentTypeAttribute = z.infer; diff --git a/tsconfig.jsonc b/tsconfig.jsonc index 66100ba..e7fd0a0 100644 --- a/tsconfig.jsonc +++ b/tsconfig.jsonc @@ -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/**/*" + ] }