diff --git a/lib/types.ts b/lib/types.ts index 42e8339..959da69 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 @@ -42,9 +43,11 @@ export type EnvSchemaPropertyValue< * * Multiple phases may produce errors, then it's stored as array. */ -export type EnvSchemaErrors = Partial<{ - [K in keyof S['properties'] | '$other']: Error[]; -}>; +export type EnvSchemaErrors = Partial< + { + [K in keyof S['properties'] | '$other']: Error[]; + } +>; export type EnvSchemaMaybeErrors = | EnvSchemaErrors | undefined; @@ -57,7 +60,7 @@ export type EnvSchemaMaybeErrors = */ export type EnvSchemaParserFn< S extends BaseEnvSchema, - K extends keyof S['properties'], + K extends keyof S['properties'] > = ( str: string, propertySchema: Readonly, @@ -72,9 +75,11 @@ export type EnvSchemaParserFn< * keep the original value as a string. */ export type EnvSchemaCustomParsers = Readonly< - Partial<{ - [K in keyof S['properties']]: EnvSchemaParserFn; - }> + Partial< + { + [K in keyof S['properties']]: EnvSchemaParserFn; + } + > >; /** @@ -84,7 +89,7 @@ export type EnvSchemaCustomParsers = Readonly< */ export type EnvSchemaSerializeFn< S extends BaseEnvSchema, - K extends keyof S['properties'], + K extends keyof S['properties'] > = ( value: Exclude, undefined>, propertySchema: Readonly, @@ -99,9 +104,11 @@ export type EnvSchemaSerializeFn< * unless it's already a string. */ export type EnvSchemaCustomSerializers = Readonly< - Partial<{ - [K in keyof S['properties']]: EnvSchemaSerializeFn; - }> + Partial< + { + [K in keyof S['properties']]: EnvSchemaSerializeFn; + } + > >; /** @@ -119,14 +126,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, @@ -135,9 +143,11 @@ export type EnvSchemaPostValidateFn< * Each validator will receive the other properties for convenience. */ export type EnvSchemaCustomPostValidators = Readonly< - Partial<{ - [K in keyof S['properties']]: EnvSchemaPostValidateFn; - }> + Partial< + { + [K in keyof S['properties']]: EnvSchemaPostValidateFn; + } + > >; /** @@ -149,8 +159,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, @@ -166,11 +177,13 @@ export type EnvSchemaConvertFn< * otherwise it may be converted to high-level type, such as `Date`. */ export type EnvSchemaCustomConverters = Readonly< - Partial<{ - // we must match any return type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [K in keyof S['properties']]: EnvSchemaConvertFn; - }> + Partial< + { + // we must match any return type + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [K in keyof S['properties']]: EnvSchemaConvertFn; + } + > >; /** @@ -193,15 +206,16 @@ 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< S extends BaseEnvSchema, - Converters extends EnvSchemaCustomConverters, + Converters extends EnvSchemaCustomConverters > = { -readonly [K in keyof S['properties']]: EnvSchemaConvertedValue< S, @@ -212,14 +226,14 @@ type EnvSchemaConvertedValuesWithConvertInternal< export type EnvSchemaConverters< S extends BaseEnvSchema, - Customizations extends EnvSchemaCustomizations, + Customizations extends EnvSchemaCustomizations > = Customizations extends { readonly convert: EnvSchemaCustomConverters } ? Customizations['convert'] : undefined; export type EnvSchemaConvertedPartialValuesWithConvert< S extends BaseEnvSchema, - Converters extends EnvSchemaCustomConverters | undefined, + Converters > = Converters extends EnvSchemaCustomConverters ? Partial> : EnvSchemaPartialValues; @@ -230,7 +244,7 @@ export type EnvSchemaConvertedPartialValuesWithConvert< */ export type EnvSchemaConvertedPartialValues< S extends BaseEnvSchema, - Customizations extends EnvSchemaCustomizations, + Customizations extends EnvSchemaCustomizations > = EnvSchemaConvertedPartialValuesWithConvert< S, EnvSchemaConverters @@ -238,7 +252,7 @@ export type EnvSchemaConvertedPartialValues< export type EnvSchemaConvertedValuesWithConvert< S extends BaseEnvSchema, - Converters extends EnvSchemaCustomConverters | undefined, + Converters > = Converters extends EnvSchemaCustomConverters ? EnvSchemaConvertedValuesWithConvertInternal : TypeFromJSONSchema; @@ -250,7 +264,7 @@ export type EnvSchemaConvertedValuesWithConvert< */ export type EnvSchemaConvertedValues< S extends BaseEnvSchema, - Customizations extends EnvSchemaCustomizations, + Customizations extends EnvSchemaCustomizations > = EnvSchemaConvertedValuesWithConvert< S, EnvSchemaConverters diff --git a/package.json b/package.json index 687f095..da087e3 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,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", @@ -52,6 +53,5 @@ }, "peerDependencies": { "ajv-formats": "^2.1.1" - }, - "packageManager": "yarn@3.5.0" + } } diff --git a/yarn.lock b/yarn.lock index 6780ab9..cd4357a 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"