diff --git a/lib/types.ts b/lib/types.ts index 42e8339..79d77db 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -34,7 +34,8 @@ export type EnvSchemaPartialValues = Partial< export type EnvSchemaPropertyValue< S extends BaseEnvSchema, K extends keyof S['properties'], -> = K extends keyof TypeFromJSONSchema ? TypeFromJSONSchema[K] : never; + V = TypeFromJSONSchema, +> = K extends keyof V ? V[K] : never; /** * Errors are stored per-property/variable, if something @@ -119,14 +120,15 @@ export type EnvSchemaCustomSerializers = Readonly< export type EnvSchemaPostValidateFn< S extends BaseEnvSchema, K extends keyof S['properties'], + V = EnvSchemaPropertyValue, > = ( - value: EnvSchemaPropertyValue | undefined, + value: V | undefined, propertySchema: S['properties'][K], key: K, schema: Readonly, allValues: EnvSchemaPartialValues, errors: Readonly>, -) => EnvSchemaPropertyValue | undefined; +) => V | undefined; /** * Customize the validator to be executed for each property, @@ -149,8 +151,9 @@ export type EnvSchemaConvertFn< S extends BaseEnvSchema, K extends keyof S['properties'], R, + V = EnvSchemaPropertyValue, > = ( - value: EnvSchemaPropertyValue | undefined, + value: V | undefined, propertySchema: S['properties'][K], key: K, schema: Readonly, @@ -193,10 +196,11 @@ type EnvSchemaConvertedValue< S extends BaseEnvSchema, K extends keyof S['properties'], Convert, + V = TypeFromJSONSchema, > = Convert extends EnvSchemaConvertFn ? N - : K extends keyof TypeFromJSONSchema - ? TypeFromJSONSchema[K] + : K extends keyof V + ? V[K] : never; type EnvSchemaConvertedValuesWithConvertInternal< @@ -219,7 +223,7 @@ export type EnvSchemaConverters< export type EnvSchemaConvertedPartialValuesWithConvert< S extends BaseEnvSchema, - Converters extends EnvSchemaCustomConverters | undefined, + Converters, > = Converters extends EnvSchemaCustomConverters ? Partial> : EnvSchemaPartialValues; @@ -238,7 +242,7 @@ export type EnvSchemaConvertedPartialValues< export type EnvSchemaConvertedValuesWithConvert< S extends BaseEnvSchema, - Converters extends EnvSchemaCustomConverters | undefined, + Converters, > = Converters extends EnvSchemaCustomConverters ? EnvSchemaConvertedValuesWithConvertInternal : TypeFromJSONSchema; diff --git a/package.json b/package.json index 08b8109..28ee0fe 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "husky": "^8.0.3", "install-peers-cli": "^2.2.0", "jest": "^29.5.0", + "json-schema-to-ts": "^2.8.0", "lint-staged": "^13.2.2", "npm-run-all": "^4.1.5", "prettier": "^2.8.8", diff --git a/yarn.lock b/yarn.lock index 5245bb7..0756cd3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -105,7 +105,7 @@ "@babel/traverse" "^7.21.5" "@babel/types" "^7.21.5" -"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.20.2", "@babel/helper-plugin-utils@^7.8.0": +"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.20.2", "@babel/helper-plugin-utils@^7.8.0": version "7.21.5" resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.21.5.tgz#345f2377d05a720a4e5ecfa39cbf4474a4daed56" integrity sha512-0WDaIlXKOX/3KfBK/dwP1oQGiPh6rjMkT7HIRv7i5RR2VUMwrx5ZL0dwBkKx7+SW1zwNdgjHd34IMk5ZjTeHVg== @@ -157,7 +157,7 @@ chalk "^2.0.0" js-tokens "^4.0.0" -"@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.20.7", "@babel/parser@^7.21.5", "@babel/parser@^7.21.8": +"@babel/parser@^7.1.0", "@babel/parser@^7.12.13", "@babel/parser@^7.14.7", "@babel/parser@^7.20.7", "@babel/parser@^7.21.5", "@babel/parser@^7.21.8": version "7.21.8" resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.21.8.tgz#642af7d0333eab9c0ad70b14ac5e76dbde7bfdf8" integrity sha512-6zavDGdzG3gUqAdWvlLFfk+36RilI+Pwyuuh7HItyeScCWP3k6i8vKclAQ0bM/0y/Kz/xiwvxhMv9MgTJP5gmA== @@ -247,11 +247,11 @@ "@babel/helper-plugin-utils" "^7.8.0" "@babel/plugin-syntax-top-level-await@^7.8.3": - version "7.14.5" - resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-top-level-await/-/plugin-syntax-top-level-await-7.14.5.tgz#c1cfdadc35a646240001f06138247b741c34d94c" - integrity sha512-hx++upLv5U1rgYfwe1xBQUhRmU41NEvpUvrp8jkrSCdvGSnM5/qdRMtylJ6PG5OFkBaHkbTAKTnd3/YyESRHFw== + version "7.12.13" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-top-level-await/-/plugin-syntax-top-level-await-7.12.13.tgz#c5f0fa6e249f5b739727f923540cf7a806130178" + integrity sha512-A81F9pDwyS7yM//KwbCSDqy3Uj4NMIurtplxphWxoYtNPov7cJsDkAFNNyVlIZ3jwGycVsurZ+LtOA8gZ376iQ== dependencies: - "@babel/helper-plugin-utils" "^7.14.5" + "@babel/helper-plugin-utils" "^7.12.13" "@babel/plugin-syntax-typescript@^7.7.2": version "7.21.4" @@ -260,7 +260,14 @@ dependencies: "@babel/helper-plugin-utils" "^7.20.2" -"@babel/template@^7.20.7", "@babel/template@^7.3.3": +"@babel/runtime@^7.18.3": + version "7.21.5" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.5.tgz#8492dddda9644ae3bda3b45eabe87382caee7200" + integrity sha512-8jI69toZqqcsnqGGqwGS4Qb1VwLOEp4hz+CXPywcvjs60u3B4Pom/U/7rm4W8tMOYEB+E9wgD0mW1l3r8qlI9Q== + dependencies: + regenerator-runtime "^0.13.11" + +"@babel/template@^7.20.7": version "7.20.7" resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.20.7.tgz#a15090c2839a83b02aa996c0b4994005841fd5a8" integrity sha512-8SegXApWe6VoNw0r9JHpSteLKTpTiLZ4rMlGIm9JQ18KiCtyQiAMEazujAHrUS5flrcqYZa75ukev3P6QmUwUw== @@ -269,6 +276,15 @@ "@babel/parser" "^7.20.7" "@babel/types" "^7.20.7" +"@babel/template@^7.3.3": + version "7.12.13" + resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.12.13.tgz#530265be8a2589dbb37523844c5bcb55947fb327" + integrity sha512-/7xxiGA57xMo/P2GVvdEumr8ONhFOhfgq2ihK3h1e6THqzTAkHbkXgB0xI9yeTfIUoH3+oAeHhqm/I43OTbbjA== + dependencies: + "@babel/code-frame" "^7.12.13" + "@babel/parser" "^7.12.13" + "@babel/types" "^7.12.13" + "@babel/traverse@^7.21.5", "@babel/traverse@^7.7.2": version "7.21.5" resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.21.5.tgz#ad22361d352a5154b498299d523cf72998a4b133" @@ -285,7 +301,7 @@ debug "^4.1.0" globals "^11.1.0" -"@babel/types@^7.0.0", "@babel/types@^7.18.6", "@babel/types@^7.20.7", "@babel/types@^7.21.0", "@babel/types@^7.21.4", "@babel/types@^7.21.5", "@babel/types@^7.3.0", "@babel/types@^7.3.3": +"@babel/types@^7.0.0", "@babel/types@^7.12.13", "@babel/types@^7.18.6", "@babel/types@^7.20.7", "@babel/types@^7.21.0", "@babel/types@^7.21.4", "@babel/types@^7.21.5", "@babel/types@^7.3.0", "@babel/types@^7.3.3": version "7.21.5" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.21.5.tgz#18dfbd47c39d3904d5db3d3dc2cc80bedb60e5b6" integrity sha512-m4AfNvVF2mVC/F7fDEdH2El3HzUg9It/XsCxZiOTTA3m3qYfcSVSbTfM6Q9xG+hYDniZssYhlXKKUMD5m8tF4Q== @@ -3183,6 +3199,15 @@ json-parse-even-better-errors@^2.3.0: resolved "https://registry.yarnpkg.com/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz#7c47805a94319928e05777405dc12e1f7a4ee02d" integrity sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w== +json-schema-to-ts@^2.8.0: + version "2.8.0" + resolved "https://registry.yarnpkg.com/json-schema-to-ts/-/json-schema-to-ts-2.8.0.tgz#4e31a6ac98fd7ceae2a7ad065fa73977f0588ddb" + integrity sha512-fNAnQ6N0a/0rJlqkzKBdb+5EnMgcKTlrAwMLg3adlKRTT1uNNT4ziqwBzMhj4vp768pO9PoQzZcUyv3QWmD0pQ== + dependencies: + "@babel/runtime" "^7.18.3" + "@types/json-schema" "^7.0.9" + ts-algebra "^1.2.0" + json-schema-traverse@^0.4.1: version "0.4.1" resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660" @@ -3933,6 +3958,11 @@ redent@^3.0.0: indent-string "^4.0.0" strip-indent "^3.0.0" +regenerator-runtime@^0.13.11: + version "0.13.11" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9" + integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg== + regexp.prototype.flags@^1.4.3: version "1.5.0" resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.5.0.tgz#fe7ce25e7e4cca8db37b6634c8a2c7009199b9cb" @@ -4426,6 +4456,11 @@ trim-newlines@^3.0.0: resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-3.0.1.tgz#260a5d962d8b752425b32f3a7db0dcacd176c144" integrity sha512-c1PTsA3tYrIsLGkJkzHF+w9F2EyxfXGo4UyJc4pFL++FMjnq0HJS69T3M7d//gKrFKwy429bouPescbjecU+Zw== +ts-algebra@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/ts-algebra/-/ts-algebra-1.2.0.tgz#f91c481207a770f0d14d055c376cbee040afdfc9" + integrity sha512-kMuJJd8B2N/swCvIvn1hIFcIOrLGbWl9m/J6O3kHx9VRaevh00nvgjPiEGaRee7DRaAczMYR2uwWvXU22VFltw== + ts-jest@^29.1.0: version "29.1.0" resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-29.1.0.tgz#4a9db4104a49b76d2b368ea775b6c9535c603891"