@lokalise/node-core
Advanced tools
Comparing version 9.4.0 to 9.5.0
@@ -0,1 +1,2 @@ | ||
import type { ZodSchema } from 'zod'; | ||
import type { EnvValueValidator } from './configTypes'; | ||
@@ -24,7 +25,11 @@ export type EnvType = { | ||
getOptionalBoolean(param: string, defaultValue: boolean): boolean; | ||
getMandatoryJsonObject<T extends object>(param: string, schema: ZodSchema<T>): T; | ||
getOptionalNullableJsonObject<T extends object, Z extends T | null | undefined>(param: string, schema: ZodSchema<T>, defaultValue: Z): Z; | ||
getOptionalJsonObject<T extends object>(param: string, schema: ZodSchema<T>, defaultValue: T): T; | ||
isProduction(): boolean; | ||
isDevelopment(): boolean; | ||
isTest(): boolean; | ||
private validateSchema; | ||
} | ||
export declare function validateOneOf<const T>(validatedEntity: unknown, expectedOneOfEntities: T[], errorText?: string): T; | ||
export declare function validateNumber(validatedObject: unknown, errorText: string): number; |
@@ -113,2 +113,16 @@ "use strict"; | ||
} | ||
getMandatoryJsonObject(param, schema) { | ||
const rawValue = this.getMandatory(param); | ||
return this.validateSchema(JSON.parse(rawValue), schema, `Configuration parameter ${param} must be a valid JSON meeting the given schema, but was ${rawValue}`); | ||
} | ||
getOptionalNullableJsonObject(param, schema, defaultValue) { | ||
const rawValue = this.getOptionalNullable(param, undefined); | ||
if (!rawValue) { | ||
return defaultValue; | ||
} | ||
return this.validateSchema(JSON.parse(rawValue), schema, `Configuration parameter ${param} must be a valid JSON meeting the given schema, but was ${rawValue}`); | ||
} | ||
getOptionalJsonObject(param, schema, defaultValue) { | ||
return this.getOptionalNullableJsonObject(param, schema, defaultValue); | ||
} | ||
isProduction() { | ||
@@ -123,2 +137,13 @@ return this.env.NODE_ENV === 'production'; | ||
} | ||
validateSchema(value, schema, errorMessage) { | ||
const parsedValue = schema.safeParse(value); | ||
if (!parsedValue.success) { | ||
throw new InternalError_1.InternalError({ | ||
message: errorMessage, | ||
errorCode: 'CONFIGURATION_ERROR', | ||
details: parsedValue.error, | ||
}); | ||
} | ||
return parsedValue.data; | ||
} | ||
} | ||
@@ -125,0 +150,0 @@ exports.ConfigScope = ConfigScope; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const vitest_1 = require("vitest"); | ||
const zod_1 = require("zod"); | ||
const ConfigScope_1 = require("./ConfigScope"); | ||
@@ -22,3 +24,3 @@ const configTransformers_1 = require("./configTransformers"); | ||
const resolvedValue = configScope.getMandatoryInteger('value'); | ||
expect(resolvedValue).toBe(123); | ||
(0, vitest_1.expect)(resolvedValue).toBe(123); | ||
}); | ||
@@ -28,3 +30,3 @@ it('throws an error on non-number', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getMandatoryInteger('value')).toThrow(/must be a number/); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryInteger('value')).toThrow(/must be a number/); | ||
}); | ||
@@ -34,3 +36,3 @@ it('throws an error on missing value', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getMandatoryInteger('value')).toThrow(/Missing mandatory configuration parameter/); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryInteger('value')).toThrow(/Missing mandatory configuration parameter/); | ||
}); | ||
@@ -44,3 +46,3 @@ }); | ||
const resolvedValue = configScope.getMandatoryValidatedInteger('value', validator); | ||
expect(resolvedValue).toBe(15); | ||
(0, vitest_1.expect)(resolvedValue).toBe(15); | ||
}); | ||
@@ -50,3 +52,3 @@ it('throws an error on non-number', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getMandatoryValidatedInteger('value', validator)).toThrow(/must be a number/); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryValidatedInteger('value', validator)).toThrow(/must be a number/); | ||
}); | ||
@@ -56,3 +58,3 @@ it('throws an error on missing value', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getMandatoryInteger('value')).toThrow(/Missing mandatory configuration parameter/); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryInteger('value')).toThrow(/Missing mandatory configuration parameter/); | ||
}); | ||
@@ -62,3 +64,3 @@ it('throws an error on invalid number', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getMandatoryValidatedInteger('value', validator)).toThrow(/is invalid for parameter value/); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryValidatedInteger('value', validator)).toThrow(/is invalid for parameter value/); | ||
}); | ||
@@ -71,3 +73,3 @@ }); | ||
const resolvedValue = configScope.getMandatory('value'); | ||
expect(resolvedValue).toBe('123'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('123'); | ||
}); | ||
@@ -77,3 +79,3 @@ it('throws an error on missing value', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getMandatory('value')).toThrow(/Missing mandatory configuration parameter/); | ||
(0, vitest_1.expect)(() => configScope.getMandatory('value')).toThrow(/Missing mandatory configuration parameter/); | ||
}); | ||
@@ -86,3 +88,3 @@ }); | ||
const resolvedValue = configScope.getMandatoryOneOf('value', ['a', 'g', 'b']); | ||
expect(resolvedValue).toBe('g'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('g'); | ||
}); | ||
@@ -92,3 +94,3 @@ it('throws an error on item not from list', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getMandatoryOneOf('value', ['a', 'g', 'b'])).toThrow(/Unsupported value/); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryOneOf('value', ['a', 'g', 'b'])).toThrow(/Unsupported value/); | ||
}); | ||
@@ -98,3 +100,3 @@ it('throws an error on missing value', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getMandatoryOneOf('value', ['a'])).toThrow(/Missing mandatory configuration parameter/); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryOneOf('value', ['a'])).toThrow(/Missing mandatory configuration parameter/); | ||
}); | ||
@@ -107,3 +109,3 @@ }); | ||
const resolvedValue = configScope.getOptionalNullable('value', 'def'); | ||
expect(resolvedValue).toBe('val'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('val'); | ||
}); | ||
@@ -114,3 +116,3 @@ it('uses default value if not set', () => { | ||
const resolvedValue = configScope.getOptionalNullable('value', 'def'); | ||
expect(resolvedValue).toBe('def'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def'); | ||
}); | ||
@@ -121,3 +123,3 @@ it('keeps null if preferred', () => { | ||
const resolvedValue = configScope.getOptionalNullable('value', null); | ||
expect(resolvedValue).toBeNull(); | ||
(0, vitest_1.expect)(resolvedValue).toBeNull(); | ||
}); | ||
@@ -128,3 +130,3 @@ it('uses default on empty string', () => { | ||
const resolvedValue = configScope.getOptionalNullable('value', 'def'); | ||
expect(resolvedValue).toBe('def'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def'); | ||
}); | ||
@@ -137,3 +139,3 @@ }); | ||
const resolvedValue = configScope.getOptional('value', 'def'); | ||
expect(resolvedValue).toBe('val'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('val'); | ||
}); | ||
@@ -144,3 +146,3 @@ it('uses default value if not set', () => { | ||
const resolvedValue = configScope.getOptional('value', 'def'); | ||
expect(resolvedValue).toBe('def'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def'); | ||
}); | ||
@@ -154,3 +156,3 @@ // This case can happen when variable is in .env file, but is left empty. | ||
const resolvedValue = configScope.getOptional('value', 'def'); | ||
expect(resolvedValue).toBe('def'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def'); | ||
}); | ||
@@ -161,3 +163,3 @@ it('returns false if set to false', () => { | ||
const resolvedValue = configScope.getOptional('value', 'def'); | ||
expect(resolvedValue).toBe('false'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('false'); | ||
}); | ||
@@ -168,3 +170,3 @@ it('returns 0 if set to 0', () => { | ||
const resolvedValue = configScope.getOptional('value', 'def'); | ||
expect(resolvedValue).toBe('0'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('0'); | ||
}); | ||
@@ -175,3 +177,3 @@ it('returns undefined if set to undefined', () => { | ||
const resolvedValue = configScope.getOptional('value', 'def'); | ||
expect(resolvedValue).toBe('undefined'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('undefined'); | ||
}); | ||
@@ -183,3 +185,3 @@ // Side effect of how node.js handles assigning undefined to process.env | ||
const resolvedValue = configScope.getOptional('value', 'def'); | ||
expect(resolvedValue).toBe('undefined'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('undefined'); | ||
}); | ||
@@ -190,3 +192,3 @@ it('returns null if set to null', () => { | ||
const resolvedValue = configScope.getOptional('value', 'def'); | ||
expect(resolvedValue).toBe('null'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('null'); | ||
}); | ||
@@ -199,3 +201,3 @@ }); | ||
const resolvedValue = configScope.getOptionalOneOf('value', 'default', ['a', 'g', 'b']); | ||
expect(resolvedValue).toBe('g'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('g'); | ||
}); | ||
@@ -206,3 +208,3 @@ it('returns default if env value not exists and the default one exists on the list', () => { | ||
const resolvedValue = configScope.getOptionalOneOf('value', 'g', ['a', 'g', 'b']); | ||
expect(resolvedValue).toBe('g'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('g'); | ||
}); | ||
@@ -212,3 +214,3 @@ it('throws an error on env value item not from list', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getOptionalOneOf('value', 'default', ['a', 'g', 'b'])).toThrow(/Unsupported value/); | ||
(0, vitest_1.expect)(() => configScope.getOptionalOneOf('value', 'default', ['a', 'g', 'b'])).toThrow(/Unsupported value/); | ||
}); | ||
@@ -218,3 +220,3 @@ it('throws an error if env value not exists and the default one not exists on the list', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getOptionalOneOf('value', 'default', ['a'])).toThrow(/Unsupported value/); | ||
(0, vitest_1.expect)(() => configScope.getOptionalOneOf('value', 'default', ['a'])).toThrow(/Unsupported value/); | ||
}); | ||
@@ -227,3 +229,3 @@ }); | ||
const resolvedValue = configScope.getOptionalInteger('value', 1); | ||
expect(resolvedValue).toBe(3); | ||
(0, vitest_1.expect)(resolvedValue).toBe(3); | ||
}); | ||
@@ -234,3 +236,3 @@ it('uses default value if not set', () => { | ||
const resolvedValue = configScope.getOptionalInteger('value', 1); | ||
expect(resolvedValue).toBe(1); | ||
(0, vitest_1.expect)(resolvedValue).toBe(1); | ||
}); | ||
@@ -241,3 +243,3 @@ it('uses default value on empty string', () => { | ||
const resolvedValue = configScope.getOptionalInteger('value', 3); | ||
expect(resolvedValue).toBe(3); | ||
(0, vitest_1.expect)(resolvedValue).toBe(3); | ||
}); | ||
@@ -253,3 +255,3 @@ }); | ||
const resolvedValue = configScope.getOptionalValidatedInteger('value', 4, validator); | ||
expect(resolvedValue).toBe(3); | ||
(0, vitest_1.expect)(resolvedValue).toBe(3); | ||
}); | ||
@@ -260,3 +262,3 @@ it('uses default value if not set', () => { | ||
const resolvedValue = configScope.getOptionalValidatedInteger('value', 4, validator); | ||
expect(resolvedValue).toBe(4); | ||
(0, vitest_1.expect)(resolvedValue).toBe(4); | ||
}); | ||
@@ -266,3 +268,3 @@ it('throws when real value fails validation', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getOptionalValidatedInteger('value', 4, validator)).toThrow(/Value 2 is invalid for parameter value/); | ||
(0, vitest_1.expect)(() => configScope.getOptionalValidatedInteger('value', 4, validator)).toThrow(/Value 2 is invalid for parameter value/); | ||
}); | ||
@@ -272,3 +274,3 @@ it('throws when default value fails validation', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getOptionalValidatedInteger('value', 2, validator)).toThrow(/Value 2 is invalid for parameter value/); | ||
(0, vitest_1.expect)(() => configScope.getOptionalValidatedInteger('value', 2, validator)).toThrow(/Value 2 is invalid for parameter value/); | ||
}); | ||
@@ -281,3 +283,3 @@ }); | ||
const resolvedValue = configScope.getOptionalNullableInteger('value', 1); | ||
expect(resolvedValue).toBe(3); | ||
(0, vitest_1.expect)(resolvedValue).toBe(3); | ||
}); | ||
@@ -288,3 +290,3 @@ it('uses default value if not set', () => { | ||
const resolvedValue = configScope.getOptionalNullableInteger('value', 1); | ||
expect(resolvedValue).toBe(1); | ||
(0, vitest_1.expect)(resolvedValue).toBe(1); | ||
}); | ||
@@ -295,3 +297,3 @@ it('uses default undefined value if not set', () => { | ||
const resolvedValue = configScope.getOptionalNullableInteger('value', undefined); | ||
expect(resolvedValue).toBeUndefined(); | ||
(0, vitest_1.expect)(resolvedValue).toBeUndefined(); | ||
}); | ||
@@ -302,3 +304,3 @@ it('uses default null value if not set', () => { | ||
const resolvedValue = configScope.getOptionalNullableInteger('value', null); | ||
expect(resolvedValue).toBeNull(); | ||
(0, vitest_1.expect)(resolvedValue).toBeNull(); | ||
}); | ||
@@ -314,3 +316,3 @@ }); | ||
const resolvedValue = configScope.getOptionalValidated('value', 'def', validator); | ||
expect(resolvedValue).toBe('val'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('val'); | ||
}); | ||
@@ -321,3 +323,3 @@ it('uses default value if not set', () => { | ||
const resolvedValue = configScope.getOptionalValidated('value', 'def', validator); | ||
expect(resolvedValue).toBe('def'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def'); | ||
}); | ||
@@ -328,3 +330,3 @@ it('uses default value on empty string', () => { | ||
const resolvedValue = configScope.getOptionalValidated('value', 'def', validator); | ||
expect(resolvedValue).toBe('def'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def'); | ||
}); | ||
@@ -334,3 +336,3 @@ it('throws an error if failing validation', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getOptionalValidated('value', 'a', validator)).toThrow(/is invalid for parameter/); | ||
(0, vitest_1.expect)(() => configScope.getOptionalValidated('value', 'a', validator)).toThrow(/is invalid for parameter/); | ||
}); | ||
@@ -343,3 +345,3 @@ }); | ||
const resolvedValue = configScope.getOptionalTransformed('value', 'def/', configTransformers_1.ensureClosingSlashTransformer); | ||
expect(resolvedValue).toBe('val/'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('val/'); | ||
}); | ||
@@ -350,3 +352,3 @@ it('uses default value if not set', () => { | ||
const resolvedValue = configScope.getOptionalTransformed('value', 'def', configTransformers_1.ensureClosingSlashTransformer); | ||
expect(resolvedValue).toBe('def/'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def/'); | ||
}); | ||
@@ -357,3 +359,3 @@ it('uses default value on empty string', () => { | ||
const resolvedValue = configScope.getOptionalTransformed('value', 'def', configTransformers_1.ensureClosingSlashTransformer); | ||
expect(resolvedValue).toBe('def/'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def/'); | ||
}); | ||
@@ -366,3 +368,3 @@ }); | ||
const resolvedValue = configScope.getOptionalNullableTransformed('value', 'def/', maybeEnsureClosingSlashTransformer); | ||
expect(resolvedValue).toBe('val/'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('val/'); | ||
}); | ||
@@ -373,3 +375,3 @@ it('uses default value if not set', () => { | ||
const resolvedValue = configScope.getOptionalNullableTransformed('value', 'def', maybeEnsureClosingSlashTransformer); | ||
expect(resolvedValue).toBe('def/'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def/'); | ||
}); | ||
@@ -380,3 +382,3 @@ it('uses default undefined value if not set', () => { | ||
const resolvedValue = configScope.getOptionalNullableTransformed('value', undefined, maybeEnsureClosingSlashTransformer); | ||
expect(resolvedValue).toBeUndefined(); | ||
(0, vitest_1.expect)(resolvedValue).toBeUndefined(); | ||
}); | ||
@@ -387,3 +389,3 @@ it('uses default value on empty string', () => { | ||
const resolvedValue = configScope.getOptionalNullableTransformed('value', 'def', maybeEnsureClosingSlashTransformer); | ||
expect(resolvedValue).toBe('def/'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('def/'); | ||
}); | ||
@@ -396,3 +398,3 @@ }); | ||
const resolvedValue = configScope.getMandatoryTransformed('value', configTransformers_1.ensureClosingSlashTransformer); | ||
expect(resolvedValue).toBe('val/'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('val/'); | ||
}); | ||
@@ -402,3 +404,3 @@ it('throws an error if not set', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getMandatoryTransformed('value', configTransformers_1.ensureClosingSlashTransformer)).toThrow(/Missing mandatory configuration parameter/); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryTransformed('value', configTransformers_1.ensureClosingSlashTransformer)).toThrow(/Missing mandatory configuration parameter/); | ||
}); | ||
@@ -411,3 +413,3 @@ }); | ||
const resolvedValue = configScope.getOptionalBoolean('value', false); | ||
expect(resolvedValue).toBe(true); | ||
(0, vitest_1.expect)(resolvedValue).toBe(true); | ||
}); | ||
@@ -418,3 +420,3 @@ it('accepts false', () => { | ||
const resolvedValue = configScope.getOptionalBoolean('value', true); | ||
expect(resolvedValue).toBe(false); | ||
(0, vitest_1.expect)(resolvedValue).toBe(false); | ||
}); | ||
@@ -425,3 +427,3 @@ it('uses default value if not set', () => { | ||
const resolvedValue = configScope.getOptionalBoolean('value', true); | ||
expect(resolvedValue).toBe(true); | ||
(0, vitest_1.expect)(resolvedValue).toBe(true); | ||
}); | ||
@@ -432,3 +434,3 @@ it('uses default value if empty string', () => { | ||
const resolvedValue = configScope.getOptionalBoolean('value', true); | ||
expect(resolvedValue).toBe(true); | ||
(0, vitest_1.expect)(resolvedValue).toBe(true); | ||
}); | ||
@@ -438,5 +440,139 @@ it('throws an error if not a boolean', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(() => configScope.getOptionalBoolean('value', false)).toThrow(/Validated entity 1 is not one of: true,false/); | ||
(0, vitest_1.expect)(() => configScope.getOptionalBoolean('value', false)).toThrow(/Validated entity 1 is not one of: true,false/); | ||
}); | ||
}); | ||
describe('getMandatoryJsonObject', () => { | ||
it('empty env value throws error', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.object({ a: zod_1.z.string() }); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryJsonObject('emptyObjectValue', schema)).toThrow(/Missing mandatory configuration parameter/); | ||
}); | ||
it('env value not meeting schema throws error', () => { | ||
process.env.objectValue = JSON.stringify({ b: 1 }); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.object({ a: zod_1.z.string() }); | ||
(0, vitest_1.expect)(() => configScope.getMandatoryJsonObject('objectValue', schema)).toThrow(/Configuration parameter objectValue must be a valid JSON meeting the given schema, but was {"b":1}/); | ||
}); | ||
it('transform simple objects', () => { | ||
process.env.objectValue = JSON.stringify({ a: 'a', b: 1 }); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.object({ | ||
a: zod_1.z.string(), | ||
b: zod_1.z.number(), | ||
}); | ||
const result = configScope.getMandatoryJsonObject('objectValue', schema); | ||
(0, vitest_1.expect)(result).toEqual({ a: 'a', b: 1 }); | ||
}); | ||
it('transform array', () => { | ||
process.env.objectValue = JSON.stringify([ | ||
{ a: 'a1', b: 1 }, | ||
{ a: 'a2', b: 2 }, | ||
]); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.array(zod_1.z.object({ | ||
a: zod_1.z.string(), | ||
b: zod_1.z.number(), | ||
})); | ||
const result = configScope.getMandatoryJsonObject('objectValue', schema); | ||
(0, vitest_1.expect)(result).toEqual([ | ||
{ a: 'a1', b: 1 }, | ||
{ a: 'a2', b: 2 }, | ||
]); | ||
}); | ||
}); | ||
describe('getOptionalJsonObject', () => { | ||
it('empty env value returns default', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.object({ a: zod_1.z.string() }); | ||
const result = configScope.getOptionalJsonObject('emptyObjectValue', schema, { a: 'a' }); | ||
(0, vitest_1.expect)(result).toEqual({ a: 'a' }); | ||
}); | ||
it('env value not meeting schema throws error', () => { | ||
process.env.objectValue = JSON.stringify({ b: 1 }); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.object({ a: zod_1.z.string() }); | ||
(0, vitest_1.expect)(() => configScope.getOptionalJsonObject('objectValue', schema, { a: 'a' })).toThrow(/Configuration parameter objectValue must be a valid JSON meeting the given schema, but was {"b":1}/); | ||
}); | ||
it('transform simple objects', () => { | ||
process.env.objectValue = JSON.stringify({ a: 'a', b: 1 }); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.object({ | ||
a: zod_1.z.string(), | ||
b: zod_1.z.number(), | ||
}); | ||
const result = configScope.getOptionalJsonObject('objectValue', schema, { a: 'fake', b: -1 }); | ||
(0, vitest_1.expect)(result).toMatchObject({ a: 'a', b: 1 }); | ||
}); | ||
it('transform array', () => { | ||
process.env.objectValue = JSON.stringify([ | ||
{ a: 'a1', b: 1 }, | ||
{ a: 'a2', b: 2 }, | ||
]); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.array(zod_1.z.object({ | ||
a: zod_1.z.string(), | ||
b: zod_1.z.number(), | ||
})); | ||
const result = configScope.getOptionalJsonObject('objectValue', schema, [ | ||
{ a: 'fake', b: -1 }, | ||
]); | ||
(0, vitest_1.expect)(result).toMatchObject([ | ||
{ a: 'a1', b: 1 }, | ||
{ a: 'a2', b: 2 }, | ||
]); | ||
}); | ||
}); | ||
describe('getOptionalNullableJsonObject', () => { | ||
it('empty env value returns default', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.object({ a: zod_1.z.string() }); | ||
const result = configScope.getOptionalNullableJsonObject('emptyObjectValue', schema, undefined); | ||
(0, vitest_1.expect)(result).toEqual(undefined); | ||
const result2 = configScope.getOptionalNullableJsonObject('emptyObjectValue', schema, null); | ||
(0, vitest_1.expect)(result2).toEqual(null); | ||
const result3 = configScope.getOptionalNullableJsonObject('emptyObjectValue', schema, { | ||
a: 'a', | ||
}); | ||
(0, vitest_1.expect)(result3).toEqual({ a: 'a' }); | ||
}); | ||
it('env value not meeting schema returns default', () => { | ||
process.env.objectValue = JSON.stringify({ b: 1 }); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.object({ a: zod_1.z.string() }); | ||
(0, vitest_1.expect)(() => configScope.getOptionalNullableJsonObject('objectValue', schema, { a: 'a' })).toThrow(/Configuration parameter objectValue must be a valid JSON meeting the given schema, but was {"b":1}/); | ||
(0, vitest_1.expect)(() => configScope.getOptionalNullableJsonObject('objectValue', schema, null)).toThrow(/Configuration parameter objectValue must be a valid JSON meeting the given schema, but was {"b":1}/); | ||
(0, vitest_1.expect)(() => configScope.getOptionalNullableJsonObject('objectValue', schema, undefined)).toThrow(/Configuration parameter objectValue must be a valid JSON meeting the given schema, but was {"b":1}/); | ||
}); | ||
it('transform simple objects', () => { | ||
process.env.objectValue = JSON.stringify({ a: 'a', b: 1 }); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.object({ | ||
a: zod_1.z.string(), | ||
b: zod_1.z.number(), | ||
}); | ||
const result = configScope.getOptionalNullableJsonObject('objectValue', schema, { | ||
a: 'fake', | ||
b: -1, | ||
}); | ||
(0, vitest_1.expect)(result).toMatchObject({ a: 'a', b: 1 }); | ||
}); | ||
it('transform array', () => { | ||
process.env.objectValue = JSON.stringify([ | ||
{ a: 'a1', b: 1 }, | ||
{ a: 'a2', b: 2 }, | ||
]); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
const schema = zod_1.z.array(zod_1.z.object({ | ||
a: zod_1.z.string(), | ||
b: zod_1.z.number(), | ||
})); | ||
const result = configScope.getOptionalNullableJsonObject('objectValue', schema, [ | ||
{ a: 'fake', b: -1 }, | ||
]); | ||
(0, vitest_1.expect)(result).toMatchObject([ | ||
{ a: 'a1', b: 1 }, | ||
{ a: 'a2', b: 2 }, | ||
]); | ||
}); | ||
}); | ||
describe('updateEnv', () => { | ||
@@ -447,9 +583,9 @@ it('updates cached env', () => { | ||
const resolvedValue = configScope.getMandatory('value'); | ||
expect(resolvedValue).toBe('123'); | ||
(0, vitest_1.expect)(resolvedValue).toBe('123'); | ||
process.env.value = '456'; | ||
const resolvedValue2 = configScope.getMandatory('value'); | ||
expect(resolvedValue2).toBe('123'); | ||
(0, vitest_1.expect)(resolvedValue2).toBe('123'); | ||
configScope.updateEnv(); | ||
const resolvedValue3 = configScope.getMandatory('value'); | ||
expect(resolvedValue3).toBe('456'); | ||
(0, vitest_1.expect)(resolvedValue3).toBe('456'); | ||
}); | ||
@@ -461,3 +597,3 @@ }); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(configScope.isProduction()).toBe(true); | ||
(0, vitest_1.expect)(configScope.isProduction()).toBe(true); | ||
}); | ||
@@ -467,3 +603,3 @@ it('returns true if set to development', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(configScope.isProduction()).toBe(false); | ||
(0, vitest_1.expect)(configScope.isProduction()).toBe(false); | ||
}); | ||
@@ -473,3 +609,3 @@ it('returns true if set to test', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(configScope.isProduction()).toBe(false); | ||
(0, vitest_1.expect)(configScope.isProduction()).toBe(false); | ||
}); | ||
@@ -481,3 +617,3 @@ }); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(configScope.isDevelopment()).toBe(false); | ||
(0, vitest_1.expect)(configScope.isDevelopment()).toBe(false); | ||
}); | ||
@@ -487,3 +623,3 @@ it('returns true if set to development', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(configScope.isDevelopment()).toBe(true); | ||
(0, vitest_1.expect)(configScope.isDevelopment()).toBe(true); | ||
}); | ||
@@ -493,3 +629,3 @@ it('returns true if set to test', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(configScope.isDevelopment()).toBe(true); | ||
(0, vitest_1.expect)(configScope.isDevelopment()).toBe(true); | ||
}); | ||
@@ -501,3 +637,3 @@ }); | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(configScope.isTest()).toBe(false); | ||
(0, vitest_1.expect)(configScope.isTest()).toBe(false); | ||
}); | ||
@@ -507,3 +643,3 @@ it('returns true if set to development', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(configScope.isTest()).toBe(false); | ||
(0, vitest_1.expect)(configScope.isTest()).toBe(false); | ||
}); | ||
@@ -513,3 +649,3 @@ it('returns true if set to test', () => { | ||
const configScope = new ConfigScope_1.ConfigScope(); | ||
expect(configScope.isTest()).toBe(true); | ||
(0, vitest_1.expect)(configScope.isTest()).toBe(true); | ||
}); | ||
@@ -516,0 +652,0 @@ }); |
{ | ||
"name": "@lokalise/node-core", | ||
"version": "9.4.0", | ||
"version": "9.5.0", | ||
"author": { | ||
@@ -40,3 +40,4 @@ "name": "Lokalise", | ||
"undici": "^6.0.1", | ||
"undici-retry": "^5.0.2" | ||
"undici-retry": "^5.0.2", | ||
"zod": "^3.22.4" | ||
}, | ||
@@ -57,5 +58,4 @@ "devDependencies": { | ||
"vitest": "^0.34.6", | ||
"zod": "^3.22.4", | ||
"vite": "4.5.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
283538
14
3560
4
79
+ Addedzod@^3.22.4
+ Addedzod@3.24.2(transitive)