Skip to content

Commit

Permalink
feat: update dependencies
Browse files Browse the repository at this point in the history
Update dependencies to latest, fix typing
  • Loading branch information
lcsmuller committed May 24, 2023
1 parent ce8d217 commit f0fe39c
Show file tree
Hide file tree
Showing 15 changed files with 2,660 additions and 3,475 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
}
},
"rules": {
"default-param-last": "off",
"@typescript-eslint/default-param-last": "error",
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["examples/*.*", "lib/*.test.ts"]}],
"import/extensions": ["error", "never"],
"@typescript-eslint/explicit-function-return-type": "error",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [12.x]
node-version: [18.x]

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/hydrogen
16 changes: 8 additions & 8 deletions lib/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ describe('createConvert', (): void => {
OPT_VAR: '0x1fffffffffffff',
REQ_VAR: '2021-01-02T12:34:56.000Z',
};
const container: Record<string, string | undefined> = {
const container = {
OPT_VAR: '0x1fffffffffffff',
REQ_VAR: '2021-01-02T12:34:56.000Z',
};
} as const;
const convert = createConvert(schema, schemaProperties(schema), {
OPT_VAR: (
value: string | undefined,
Expand All @@ -82,7 +82,7 @@ describe('createConvert', (): void => {
allSchema: JSONSchema7,
initialValues: EnvSchemaPartialValues<S>,
errors: EnvSchemaMaybeErrors<S>,
): BigInt | undefined =>
): bigint | undefined =>
typeof value === 'string' &&
propertySchema === schema.properties.OPT_VAR &&
key === 'OPT_VAR' &&
Expand Down Expand Up @@ -139,7 +139,7 @@ New Value.....: ${commonConvert.dateTime(container.REQ_VAR)}
allSchema: JSONSchema7,
initialValues: EnvSchemaPartialValues<S>,
errors: EnvSchemaMaybeErrors<S>,
): BigInt | undefined =>
): bigint | undefined =>
typeof value === 'string' &&
propertySchema === schema.properties.OPT_VAR &&
key === 'OPT_VAR' &&
Expand Down Expand Up @@ -177,7 +177,7 @@ New Value.....: ${commonConvert.dateTime(container.REQ_VAR)}
allSchema: JSONSchema7,
initialValues: EnvSchemaPartialValues<S>,
errors: EnvSchemaMaybeErrors<S>,
): BigInt | undefined =>
): bigint | undefined =>
typeof value === 'string' &&
propertySchema === schema.properties.OPT_VAR &&
key === 'OPT_VAR' &&
Expand Down Expand Up @@ -231,7 +231,7 @@ New Value.....: ${new Date(0)}
REQ_VAR: '2021-01-02T12:34:56.000Z',
};
const convert = createConvert(schema, schemaProperties(schema), {
OPT_VAR: (): BigInt | undefined => undefined,
OPT_VAR: (): bigint | undefined => undefined,
REQ_VAR: (): Date | undefined => undefined,
});
const consoleSpy = getConsoleMock();
Expand Down Expand Up @@ -265,7 +265,7 @@ New Value.....: ${new Date(0)}
};
const error = new Error('forced error');
const convert = createConvert(schema, schemaProperties(schema), {
OPT_VAR: (): BigInt => {
OPT_VAR: (): bigint => {
throw error;
},
REQ_VAR: (): Date => {
Expand Down Expand Up @@ -309,7 +309,7 @@ New Value.....: ${new Date(0)}
schemaNoRequired,
schemaProperties(schemaNoRequired),
{
OPT_VAR: (): BigInt | undefined => undefined,
OPT_VAR: (): bigint | undefined => undefined,
REQ_VAR: (): Date | undefined => undefined,
},
);
Expand Down
13 changes: 6 additions & 7 deletions lib/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dbg from './dbg';
// DO NOT THROW HERE!
type EnvSchemaConvert<
S extends BaseEnvSchema,
Converters extends EnvSchemaCustomConverters<S> | undefined
Converters extends EnvSchemaCustomConverters<S> | undefined,
> = (
value: EnvSchemaPartialValues<S>,
errors: EnvSchemaMaybeErrors<S>,
Expand All @@ -29,7 +29,7 @@ const noRequiredProperties: string[] = [];

const createConvert = <
S extends BaseEnvSchema,
Converters extends EnvSchemaCustomConverters<S>
Converters extends EnvSchemaCustomConverters<S>,
>(
schema: S,
properties: Readonly<EnvSchemaProperties<S>>,
Expand Down Expand Up @@ -111,15 +111,14 @@ New Value.....: ${newValue}
)}`,
);
removeValue(key);
errors = addErrors(errors, key, e);
errors = addErrors(errors, key, e as Error);
}
});

requiredProperties.forEach(key => {
if (values[key] === undefined) {
errors = addErrors(
errors,
key,
key as Extract<keyof S['properties'], string>,
new Error(`required property "${key}" is undefined`),
);
}
Expand All @@ -139,14 +138,14 @@ const noConversion = <S extends BaseEnvSchema>(

export default <
S extends BaseEnvSchema,
Converters extends EnvSchemaCustomConverters<S> | undefined
Converters extends EnvSchemaCustomConverters<S> | undefined,
>(
schema: Readonly<S>,
properties: Readonly<EnvSchemaProperties<S>>,
customize: Converters,
): EnvSchemaConvert<S, Converters> =>
customize === undefined
? ((noConversion as unknown) as EnvSchemaConvert<S, Converters>)
? (noConversion as unknown as EnvSchemaConvert<S, Converters>)
: createConvert(
schema,
properties,
Expand Down
7 changes: 6 additions & 1 deletion lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import type {
EnvSchemaErrors,
} from './types';

/* istanbul ignore next */
export const assertIsError: (e: unknown) => asserts e is Error = e => {
if (!(e instanceof Error)) throw e;
};

export const addErrors = <S extends BaseEnvSchema>(
initialErrors: EnvSchemaMaybeErrors<S>,
key: Extract<keyof S['properties'], string> | '$other',
Expand Down Expand Up @@ -35,7 +40,7 @@ export const addErrors = <S extends BaseEnvSchema>(
*/
export class EnvSchemaValidationError<
S extends BaseEnvSchema,
Customizations extends EnvSchemaCustomizations<S>
Customizations extends EnvSchemaCustomizations<S>,
> extends Error {
readonly schema: S;

Expand Down
59 changes: 32 additions & 27 deletions lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ describe('validateEnvSchema', (): void => {
REQ_VAR: 'bug',
};
const consoleSpy = getConsoleMock();
expect.hasAssertions();
try {
validateEnvSchema(schema, container);
expect(true).toBe(false); // should throw
} catch (e) {
expect(e).toBeInstanceOf(EnvSchemaValidationError);
expect(e.schema).toBe(schema);
expect(e.container).toBe(container);
expect(e.values).toEqual({
const err = e as EnvSchemaValidationError<typeof schema, undefined>;
expect(err.schema).toBe(schema);
expect(err.container).toBe(container);
expect(err.values).toEqual({
OPT_VAR: 42, // default
});
expect(e.errors).toEqual({
expect(err.errors).toEqual({
REQ_VAR: [new Ajv.ValidationError([])],
});
expect(container).toEqual({
Expand All @@ -94,7 +95,7 @@ describe('validateEnvSchema', (): void => {
const consoleSpy = getConsoleMock();
const values = validateEnvSchema(schema, container, {
convert: {
OPT_VAR: (value: number | undefined): BigInt | undefined =>
OPT_VAR: (value: number | undefined): bigint | undefined =>
value !== undefined ? BigInt(value * 1e6) : undefined,
},
parse: {
Expand All @@ -112,7 +113,7 @@ describe('validateEnvSchema', (): void => {
} as const);
type ValueType = typeof values;
type ExpectedType = {
OPT_VAR: BigInt | undefined;
OPT_VAR: bigint | undefined;
REQ_VAR: {
a?: number[];
s: string;
Expand Down Expand Up @@ -171,6 +172,7 @@ New Value.....: 1000000000
};
const consoleSpy = getConsoleMock();
const error = new Error('forced error');
expect.hasAssertions();
try {
validateEnvSchema(schema, container, {
parse: {
Expand All @@ -179,16 +181,16 @@ New Value.....: 1000000000
},
},
} as const);
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(EnvSchemaValidationError);
expect(e.schema).toBe(schema);
expect(e.container).toBe(container);
expect(e.values).toEqual({
const err = e as EnvSchemaValidationError<typeof schema, undefined>;
expect(err.schema).toBe(schema);
expect(err.container).toBe(container);
expect(err.values).toEqual({
OPT_VAR: 42, // goes to default value
REQ_VAR: { a: [2, 3], s: 'hello' },
});
expect(e.errors).toEqual({ OPT_VAR: [error] });
expect(err.errors).toEqual({ OPT_VAR: [error] });
expect(container).toEqual({
OPT_VAR: '42',
REQ_VAR: '{"a":[2,3],"s":"hello"}',
Expand All @@ -207,6 +209,7 @@ New Value.....: 1000000000
};
const consoleSpy = getConsoleMock();
const error = new Error('forced error');
expect.hasAssertions();
try {
validateEnvSchema(schema, container, {
serialize: {
Expand All @@ -215,16 +218,16 @@ New Value.....: 1000000000
},
},
} as const);
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(EnvSchemaValidationError);
expect(e.schema).toBe(schema);
expect(e.container).toBe(container);
expect(e.values).toEqual({
const err = e as EnvSchemaValidationError<typeof schema, undefined>;
expect(err.schema).toBe(schema);
expect(err.container).toBe(container);
expect(err.values).toEqual({
OPT_VAR: 1.23, // not deleted if fails to serialize
REQ_VAR: { a: [2, 3], s: 'hello' },
});
expect(e.errors).toEqual({ OPT_VAR: [error] });
expect(err.errors).toEqual({ OPT_VAR: [error] });
expect(container).toEqual({
REQ_VAR: '{"a":[2,3],"s":"hello"}',
});
Expand All @@ -242,6 +245,7 @@ New Value.....: 1000000000
};
const consoleSpy = getConsoleMock();
const error = new Error('forced error');
expect.hasAssertions();
try {
validateEnvSchema(schema, container, {
postValidate: {
Expand All @@ -250,15 +254,15 @@ New Value.....: 1000000000
},
},
} as const);
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(EnvSchemaValidationError);
expect(e.schema).toBe(schema);
expect(e.container).toBe(container);
expect(e.values).toEqual({
const err = e as EnvSchemaValidationError<typeof schema, undefined>;
expect(err.schema).toBe(schema);
expect(err.container).toBe(container);
expect(err.values).toEqual({
REQ_VAR: { a: [2, 3], s: 'hello' },
});
expect(e.errors).toEqual({ OPT_VAR: [error] });
expect(err.errors).toEqual({ OPT_VAR: [error] });
expect(container).toEqual({
REQ_VAR: '{"a":[2,3],"s":"hello"}',
});
Expand All @@ -278,6 +282,7 @@ New Value.....: 1000000000
};
const consoleSpy = getConsoleMock();
const error = new Error('forced error');
expect.hasAssertions();
try {
validateEnvSchema(schema, container, {
convert: {
Expand All @@ -286,15 +291,15 @@ New Value.....: 1000000000
},
},
} as const);
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(EnvSchemaValidationError);
expect(e.schema).toBe(schema);
expect(e.container).toBe(container);
expect(e.values).toEqual({
const err = e as EnvSchemaValidationError<typeof schema, undefined>;
expect(err.schema).toBe(schema);
expect(err.container).toBe(container);
expect(err.values).toEqual({
REQ_VAR: { a: [2, 3], s: 'hello' },
});
expect(e.errors).toEqual({ OPT_VAR: [error] });
expect(err.errors).toEqual({ OPT_VAR: [error] });
expect(container).toEqual({
REQ_VAR: '{"a":[2,3],"s":"hello"}',
});
Expand Down
6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const commonConvert = providedConverters;

type ValidateEnvSchema<
S extends BaseEnvSchema,
Customizations extends EnvSchemaCustomizations<S>
Customizations extends EnvSchemaCustomizations<S>,
> = (
container: Record<string, string | undefined>,
) => EnvSchemaConvertedValues<S, Customizations>;
Expand All @@ -53,7 +53,7 @@ type ValidateEnvSchema<
*/
export const createValidateEnvSchema = <
S extends BaseEnvSchema,
Customizations extends EnvSchemaCustomizations<S>
Customizations extends EnvSchemaCustomizations<S>,
>(
schema: S,
customize?: Customizations,
Expand Down Expand Up @@ -135,7 +135,7 @@ export const createValidateEnvSchema = <
*/
export const validateEnvSchema = <
S extends BaseEnvSchema,
Customizations extends EnvSchemaCustomizations<S>
Customizations extends EnvSchemaCustomizations<S>,
>(
schema: S,
container: Record<string, string | undefined> = process.env,
Expand Down
Loading

0 comments on commit f0fe39c

Please sign in to comment.