zcb 0.0.27 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -24
- package/dist/cjs/cli.cjs +6 -6
- package/dist/cjs/cli.cjs.map +1 -1
- package/dist/cjs/index.cjs +44 -19
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/cli.mjs +6 -6
- package/dist/esm/cli.mjs.map +1 -1
- package/dist/esm/index.mjs +45 -20
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/cjs/createConfigBuilder.d.cts +9 -9
- package/dist/types/cjs/createConfigBuilder.d.cts.map +1 -1
- package/dist/types/cjs/utils/collateObjectPropertyDefaults.d.cts +4 -0
- package/dist/types/cjs/utils/collateObjectPropertyDefaults.d.cts.map +1 -0
- package/dist/types/cjs/utils/jsonStringifyReplacer.d.cts +15 -0
- package/dist/types/cjs/utils/jsonStringifyReplacer.d.cts.map +1 -0
- package/dist/types/cjs/utils/objectPropertyHasDefaults.d.cts +3 -0
- package/dist/types/cjs/utils/objectPropertyHasDefaults.d.cts.map +1 -0
- package/dist/types/esm/createConfigBuilder.d.ts +9 -9
- package/dist/types/esm/createConfigBuilder.d.ts.map +1 -1
- package/dist/types/esm/utils/collateObjectPropertyDefaults.d.ts +4 -0
- package/dist/types/esm/utils/collateObjectPropertyDefaults.d.ts.map +1 -0
- package/dist/types/esm/utils/jsonStringifyReplacer.d.ts +15 -0
- package/dist/types/esm/utils/jsonStringifyReplacer.d.ts.map +1 -0
- package/dist/types/esm/utils/objectPropertyHasDefaults.d.ts +3 -0
- package/dist/types/esm/utils/objectPropertyHasDefaults.d.ts.map +1 -0
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +15 -14
- package/src/__snapshots__/createConfigBuilder.test.ts.snap +30 -0
- package/src/__testUtils__/configBuilder.ts +12 -12
- package/src/createConfigBuilder.test.ts +92 -68
- package/src/createConfigBuilder.ts +31 -32
- package/src/utils/collateObjectPropertyDefaults.ts +26 -0
- package/src/utils/importValidateTransformWriteConfig.ts +6 -6
- package/src/utils/isPropertyReservedWord.ts +9 -9
- package/src/utils/jsonStringifyReplacer.ts +6 -0
- package/src/utils/{objectHasInvalidDefaults.ts → objectPropertyHasDefaults.ts} +1 -1
- package/src/utils/transformConfig.test.ts +2 -2
- package/tsconfig.build.json +1 -4
- package/tsconfig.json +0 -3
- package/dist/types/cjs/utils/objectHasInvalidDefaults.d.cts +0 -3
- package/dist/types/cjs/utils/objectHasInvalidDefaults.d.cts.map +0 -1
- package/dist/types/esm/utils/objectHasInvalidDefaults.d.ts +0 -3
- package/dist/types/esm/utils/objectHasInvalidDefaults.d.ts.map +0 -1
|
@@ -4,13 +4,14 @@ import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
|
4
4
|
import { cloneNonEnumerableValues } from './transformers/cloneNonEnumerableValues.ts';
|
|
5
5
|
import { NonEmumeralProperties } from './types.ts';
|
|
6
6
|
import { arrayHasInvalidDefaults } from './utils/arrayHasInvalidDefaults.ts';
|
|
7
|
+
import { collateObjectPropertyDefaults } from './utils/collateObjectPropertyDefaults.ts';
|
|
7
8
|
import { isDerivedValueCallback } from './utils/isDerivedValueCallback.ts';
|
|
8
9
|
import { isInvalidPropertyOverride } from './utils/isInvalidPropertyOverride.ts';
|
|
9
10
|
import { RESERVED_KEYWORDS, isPropertyReservedWord } from './utils/isPropertyReservedWord.ts';
|
|
10
11
|
import { isSchemaValid } from './utils/isSchemaValid.ts';
|
|
11
12
|
import { isValidPropertyDefinition } from './utils/isValidPropertyDefinition.ts';
|
|
12
13
|
import { isValidValue } from './utils/isValidValue.ts';
|
|
13
|
-
import {
|
|
14
|
+
import { jsonStringifyReplacer } from './utils/jsonStringifyReplacer.ts';
|
|
14
15
|
import { recordHasInvalidDefaults } from './utils/recordHasInvalidDefaults.ts';
|
|
15
16
|
import { transformConfigSync } from './utils/transformConfig.ts';
|
|
16
17
|
|
|
@@ -20,15 +21,15 @@ export type ConfigBuilder<ZodTypes> = {
|
|
|
20
21
|
override?: boolean
|
|
21
22
|
) => ConfigBuilder<ZodTypes>;
|
|
22
23
|
} & {
|
|
23
|
-
disable: () => ConfigBuilder<ZodTypes>;
|
|
24
|
-
errors: () => ZodError['errors'];
|
|
25
|
-
experiment: (key: string) => ConfigBuilder<ZodTypes>;
|
|
26
|
-
extend: (configBuilder: ConfigBuilder<ZodTypes>) => ConfigBuilder<ZodTypes>;
|
|
27
|
-
flush: () => ZodTypes;
|
|
28
|
-
fork: () => ConfigBuilder<ZodTypes>;
|
|
29
|
-
toJson: () => string;
|
|
30
|
-
validate: () => boolean;
|
|
31
|
-
values: () => ZodTypes;
|
|
24
|
+
$disable: () => ConfigBuilder<ZodTypes>;
|
|
25
|
+
$errors: () => ZodError['errors'];
|
|
26
|
+
$experiment: (key: string) => ConfigBuilder<ZodTypes>;
|
|
27
|
+
$extend: (configBuilder: ConfigBuilder<ZodTypes>) => ConfigBuilder<ZodTypes>;
|
|
28
|
+
$flush: () => ZodTypes;
|
|
29
|
+
$fork: () => ConfigBuilder<ZodTypes>;
|
|
30
|
+
$toJson: () => string;
|
|
31
|
+
$validate: () => boolean;
|
|
32
|
+
$values: () => ZodTypes;
|
|
32
33
|
};
|
|
33
34
|
|
|
34
35
|
export const createConfigBuilder = <ZodTypes>(
|
|
@@ -61,7 +62,7 @@ export const createConfigBuilder = <ZodTypes>(
|
|
|
61
62
|
let callbacks: Partial<Record<keyof Config, DerivedValueCallback>> = { ...derivedValueCallbacks };
|
|
62
63
|
|
|
63
64
|
const configBuilder = {
|
|
64
|
-
disable: () => {
|
|
65
|
+
$disable: () => {
|
|
65
66
|
Object.defineProperty(config, NonEmumeralProperties.DISABLED, {
|
|
66
67
|
configurable: false,
|
|
67
68
|
enumerable: false,
|
|
@@ -70,15 +71,15 @@ export const createConfigBuilder = <ZodTypes>(
|
|
|
70
71
|
|
|
71
72
|
return configBuilder;
|
|
72
73
|
},
|
|
73
|
-
errors: () => {
|
|
74
|
+
$errors: () => {
|
|
74
75
|
try {
|
|
75
|
-
zodSchema.parse(configBuilder
|
|
76
|
+
zodSchema.parse(configBuilder.$values());
|
|
76
77
|
return [];
|
|
77
78
|
} catch (error: unknown) {
|
|
78
79
|
return (error as ZodError).errors;
|
|
79
80
|
}
|
|
80
81
|
},
|
|
81
|
-
experiment: (key: string) => {
|
|
82
|
+
$experiment: (key: string) => {
|
|
82
83
|
Object.defineProperty(config, NonEmumeralProperties.EXPERIMENT, {
|
|
83
84
|
configurable: false,
|
|
84
85
|
enumerable: false,
|
|
@@ -87,13 +88,13 @@ export const createConfigBuilder = <ZodTypes>(
|
|
|
87
88
|
|
|
88
89
|
return configBuilder;
|
|
89
90
|
},
|
|
90
|
-
extend: (builder: ConfigBuilder<Config>) => {
|
|
91
|
-
config = transformConfigSync<Config>(builder
|
|
91
|
+
$extend: (builder: ConfigBuilder<Config>) => {
|
|
92
|
+
config = transformConfigSync<Config>(builder.$values(), [cloneNonEnumerableValues]);
|
|
92
93
|
// @ts-expect-error private property
|
|
93
94
|
callbacks = { ...builder.__callbacks } as Partial<Record<keyof Config, DerivedValueCallback>>;
|
|
94
95
|
},
|
|
95
|
-
flush: () => {
|
|
96
|
-
const values = configBuilder
|
|
96
|
+
$flush: () => {
|
|
97
|
+
const values = configBuilder.$values();
|
|
97
98
|
config = {} as Config;
|
|
98
99
|
|
|
99
100
|
Object.defineProperty(config, NonEmumeralProperties.ZCB, {
|
|
@@ -104,18 +105,18 @@ export const createConfigBuilder = <ZodTypes>(
|
|
|
104
105
|
|
|
105
106
|
return values;
|
|
106
107
|
},
|
|
107
|
-
fork: () => createConfigBuilder<Config>(zodSchema, derivedValueCallbacks),
|
|
108
|
-
toJson: () => JSON.stringify(configBuilder
|
|
109
|
-
validate: () => {
|
|
108
|
+
$fork: () => createConfigBuilder<Config>(zodSchema, derivedValueCallbacks),
|
|
109
|
+
$toJson: () => JSON.stringify(configBuilder.$values(), jsonStringifyReplacer, 2),
|
|
110
|
+
$validate: () => {
|
|
110
111
|
try {
|
|
111
|
-
zodSchema.parse(configBuilder
|
|
112
|
+
zodSchema.parse(configBuilder.$values());
|
|
112
113
|
return true;
|
|
113
114
|
} catch (error: unknown) {
|
|
114
115
|
console.error(error);
|
|
115
116
|
return false;
|
|
116
117
|
}
|
|
117
118
|
},
|
|
118
|
-
values: () => {
|
|
119
|
+
$values: () => {
|
|
119
120
|
for (const property in callbacks) {
|
|
120
121
|
const callback = callbacks[property];
|
|
121
122
|
|
|
@@ -166,20 +167,18 @@ export const createConfigBuilder = <ZodTypes>(
|
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
if (propertyDefinition.type === 'object') {
|
|
169
|
-
if (
|
|
170
|
+
if (recordHasInvalidDefaults(propertyDefinition)) {
|
|
170
171
|
throw new Error(
|
|
171
|
-
`When setting schema property defaults for the
|
|
172
|
+
`When setting schema property defaults for the value of the record assigned to "${String(
|
|
172
173
|
castProperty
|
|
173
|
-
)}", set them on the
|
|
174
|
+
)}", set them on the record and not the value.`
|
|
174
175
|
);
|
|
175
176
|
}
|
|
176
177
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
)}", set them on the object and not the property.`
|
|
182
|
-
);
|
|
178
|
+
const propertyDefaults = collateObjectPropertyDefaults(propertyDefinition);
|
|
179
|
+
|
|
180
|
+
if (propertyDefaults) {
|
|
181
|
+
config[castProperty] = propertyDefaults as Config[keyof Config];
|
|
183
182
|
}
|
|
184
183
|
}
|
|
185
184
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { JSONSchema7 } from 'json-schema';
|
|
2
|
+
import { isBoolean, isUndefined } from 'lodash-es';
|
|
3
|
+
import type { Jsonifiable } from 'type-fest';
|
|
4
|
+
import { objectPropertyHasDefaults } from './objectPropertyHasDefaults.ts';
|
|
5
|
+
|
|
6
|
+
export const collateObjectPropertyDefaults = (propertyDefinition: JSONSchema7) => {
|
|
7
|
+
const { properties } = propertyDefinition;
|
|
8
|
+
|
|
9
|
+
if (!properties || !objectPropertyHasDefaults(propertyDefinition)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return Object.keys(properties).reduce<Record<string, Jsonifiable>>((acc, key) => {
|
|
14
|
+
const propertyDef = properties[key];
|
|
15
|
+
|
|
16
|
+
if (!propertyDef || isBoolean(propertyDef)) {
|
|
17
|
+
return acc;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if ('default' in propertyDef && !isUndefined(propertyDef.default)) {
|
|
21
|
+
acc[key] = propertyDef.default;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return acc;
|
|
25
|
+
}, {});
|
|
26
|
+
};
|
|
@@ -13,20 +13,20 @@ export const importValidateTransformWriteConfig = (
|
|
|
13
13
|
) => {
|
|
14
14
|
import(resolve(process.cwd(), inputFile))
|
|
15
15
|
.then(({ default: configBuilder }: { default: ConfigBuilder<object> }) => {
|
|
16
|
-
if (!configBuilder
|
|
16
|
+
if (!configBuilder.$validate()) {
|
|
17
17
|
shelljs.echo(`zcd ${command} => invalid config`);
|
|
18
|
-
shelljs.echo(`zcd ${command} => config values:\n${configBuilder
|
|
19
|
-
shelljs.echo(`zcd ${command} => errors:\n${JSON.stringify(configBuilder
|
|
18
|
+
shelljs.echo(`zcd ${command} => config values:\n${configBuilder.$toJson()}\n`);
|
|
19
|
+
shelljs.echo(`zcd ${command} => errors:\n${JSON.stringify(configBuilder.$errors(), undefined, 2)}\n`);
|
|
20
20
|
shelljs.exit(1);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
shelljs.echo('zcd ${command} => valid config');
|
|
24
|
-
shelljs.echo(`zcd ${command} => config values:\n${configBuilder
|
|
24
|
+
shelljs.echo(`zcd ${command} => config values:\n${configBuilder.$toJson()}\n`);
|
|
25
25
|
|
|
26
26
|
if (experimentCallbackFile) {
|
|
27
27
|
import(resolve(process.cwd(), experimentCallbackFile))
|
|
28
28
|
.then(({ default: experimentsCallback }: { default: SetupExperimentsCallback }) => {
|
|
29
|
-
void transformWriteConfig(configBuilder
|
|
29
|
+
void transformWriteConfig(configBuilder.$values(), { experimentsCallback, outputFile });
|
|
30
30
|
})
|
|
31
31
|
.catch((error: unknown) => {
|
|
32
32
|
if (error instanceof Error) {
|
|
@@ -41,7 +41,7 @@ export const importValidateTransformWriteConfig = (
|
|
|
41
41
|
return;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
void transformWriteConfig(configBuilder
|
|
44
|
+
void transformWriteConfig(configBuilder.$values(), { outputFile });
|
|
45
45
|
})
|
|
46
46
|
.catch((error: unknown) => {
|
|
47
47
|
if (error instanceof Error) {
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export const RESERVED_KEYWORDS = new Set([
|
|
2
|
-
'disable',
|
|
3
|
-
'errors',
|
|
4
|
-
'experiment',
|
|
5
|
-
'extend',
|
|
6
|
-
'flush',
|
|
7
|
-
'fork',
|
|
8
|
-
'toJson',
|
|
9
|
-
'validate',
|
|
10
|
-
'values',
|
|
2
|
+
'$disable',
|
|
3
|
+
'$errors',
|
|
4
|
+
'$experiment',
|
|
5
|
+
'$extend',
|
|
6
|
+
'$flush',
|
|
7
|
+
'$fork',
|
|
8
|
+
'$toJson',
|
|
9
|
+
'$validate',
|
|
10
|
+
'$values',
|
|
11
11
|
]);
|
|
12
12
|
|
|
13
13
|
export const isPropertyReservedWord = (propertyName: string) => RESERVED_KEYWORDS.has(propertyName);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Jsonifiable } from 'type-fest';
|
|
2
|
+
import { ZodType } from 'zod';
|
|
3
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
4
|
+
|
|
5
|
+
export const jsonStringifyReplacer = (_key: string, value: Jsonifiable) =>
|
|
6
|
+
value instanceof ZodType ? zodToJsonSchema(value, { errorMessages: true }) : value;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { JSONSchema7 } from 'json-schema';
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const objectPropertyHasDefaults = (propertyDefinition: JSONSchema7) =>
|
|
4
4
|
propertyDefinition.properties &&
|
|
5
5
|
Object.values(propertyDefinition.properties).some(value => typeof value === 'object' && 'default' in value);
|
|
@@ -4,7 +4,7 @@ describe('transformConfig', () => {
|
|
|
4
4
|
describe('when no handlers are passed in', () => {
|
|
5
5
|
it('should return the correct config', async () => {
|
|
6
6
|
const { transformConfig } = await import('./transformConfig.ts');
|
|
7
|
-
await expect(transformConfig(configBuilder
|
|
7
|
+
await expect(transformConfig(configBuilder.$values())).resolves.toMatchSnapshot();
|
|
8
8
|
});
|
|
9
9
|
});
|
|
10
10
|
});
|
|
@@ -13,7 +13,7 @@ describe('transformConfigSync', () => {
|
|
|
13
13
|
describe('when no handlers are passed in', () => {
|
|
14
14
|
it('should return the correct config', async () => {
|
|
15
15
|
const { transformConfigSync } = await import('./transformConfig.ts');
|
|
16
|
-
expect(transformConfigSync(configBuilder
|
|
16
|
+
expect(transformConfigSync(configBuilder.$values())).toMatchSnapshot();
|
|
17
17
|
});
|
|
18
18
|
});
|
|
19
19
|
});
|
package/tsconfig.build.json
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
{
|
|
2
|
-
"extends": "@repodog/ts-config/build.json",
|
|
2
|
+
"extends": ["./tsconfig.json", "@repodog/ts-config/build.json"],
|
|
3
3
|
"compilerOptions": {
|
|
4
4
|
"rootDir": "src",
|
|
5
5
|
"outDir": "dist/types/esm"
|
|
6
6
|
},
|
|
7
|
-
"include": [
|
|
8
|
-
"src/**/*"
|
|
9
|
-
],
|
|
10
7
|
"exclude": [
|
|
11
8
|
"**/*.test.*",
|
|
12
9
|
"**/__testUtils__/**"
|
package/tsconfig.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"objectHasInvalidDefaults.d.cts","sourceRoot":"","sources":["../../../../src/utils/objectHasInvalidDefaults.cts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,eAAO,MAAM,wBAAwB,uBAAwB,WAAW,wBAEqC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"objectHasInvalidDefaults.d.ts","sourceRoot":"","sources":["../../../../src/utils/objectHasInvalidDefaults.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,eAAO,MAAM,wBAAwB,uBAAwB,WAAW,wBAEqC,CAAC"}
|