Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return previous schema type #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ const schema: JSONSchemaType<Env> = {
}
}

const config = envSchema({
const config = envSchema<Env>({
schema
})
```
Expand Down
7 changes: 3 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Ajv, { KeywordDefinition, JSONSchemaType } from 'ajv';
import { AnySchema } from 'ajv/dist/core';
import { DotenvConfigOptions } from 'dotenv';

type EnvSchema = typeof envSchema
Expand All @@ -11,8 +10,8 @@ declare namespace envSchema {
[key: string]: unknown;
};

export type EnvSchemaOpt<T = EnvSchemaData> = {
schema?: JSONSchemaType<T> | AnySchema;
export type EnvSchemaOpt = {
schema?: object;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you evaluate adding the fluent-schema's JSONSchema type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not quite sure what you mean by that. Are you suggesting to add JSONSchema type from fluent-json-schema to union type for schema? If so, read description of this pull request

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. object is not a very good type. Should use something like Record<string, any> or so.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And to be honest i dont understand why we remove the generic type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is not compatible to different JSON schema library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should read backstory of this pull request - #134 , #137 , #138 . I think it will become clear to you

As for typescript:
Record<string, any> and object are equal. Also, we can not use Record<string, unknown> type, because it will not work with interfaces (typescript playground)

data?: [EnvSchemaData, ...EnvSchemaData[]] | EnvSchemaData;
env?: boolean;
dotenv?: boolean | DotenvConfigOptions;
Expand All @@ -32,5 +31,5 @@ declare namespace envSchema {
export { envSchema as default }
}

declare function envSchema<T = envSchema.EnvSchemaData>(_opts?: envSchema.EnvSchemaOpt<T>): T
declare function envSchema<T = envSchema.EnvSchemaData>(_opts?: envSchema.EnvSchemaOpt): T
export = envSchema
21 changes: 16 additions & 5 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import envSchema, {
} from '..';
import Ajv, { KeywordDefinition, JSONSchemaType } from 'ajv';
import { Static, Type } from '@sinclair/typebox';
import S from 'fluent-json-schema';

interface EnvData {
PORT: number;
Expand All @@ -24,6 +25,11 @@ const schemaWithType: JSONSchemaType<EnvData> = {
},
};

const schemaFluent = S.object().prop(
'PORT',
S.number().default(3000).required()
);

const schemaTypebox = Type.Object({
PORT: Type.Number({ default: 3000 }),
});
Expand All @@ -41,15 +47,20 @@ expectType<EnvSchemaData>(envSchemaDefault());
const emptyOpt: EnvSchemaOpt = {};
expectType<EnvSchemaOpt>(emptyOpt);

const optWithSchemaFluent: EnvSchemaOpt = {
schema: schemaFluent,
};
expectType<EnvSchemaOpt>(optWithSchemaFluent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it can never be false, because you cast it to EnvSchemaOpt in line 50.


const optWithSchemaTypebox: EnvSchemaOpt = {
schema: schemaTypebox,
};
expectType<EnvSchemaOpt>(optWithSchemaTypebox);

const optWithSchemaWithType: EnvSchemaOpt<EnvData> = {
const optWithSchemaWithType: EnvSchemaOpt = {
schema: schemaWithType,
};
expectType<EnvSchemaOpt<EnvData>>(optWithSchemaWithType);
expectType<EnvSchemaOpt>(optWithSchemaWithType);

const optWithData: EnvSchemaOpt = {
data,
Expand Down Expand Up @@ -105,11 +116,11 @@ expectError<EnvSchemaOpt>({
},
});

const envSchemaWithType = envSchema({ schema: schemaWithType });
const envSchemaWithType = envSchema<EnvData>({ schema: schemaWithType });
expectType<EnvData>(envSchemaWithType);

const envSchemaTypebox = envSchema<SchemaTypebox>({ schema: schemaTypebox });
expectType<SchemaTypebox>(envSchemaTypebox);

expectType<KeywordDefinition>(keywords.separator)
expectType<KeywordDefinition>(envSchema.keywords.separator)
expectType<KeywordDefinition>(keywords.separator);
expectType<KeywordDefinition>(envSchema.keywords.separator);