zcb 0.1.0 → 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/dist/cjs/index.cjs +26 -6
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.mjs +27 -7
- package/dist/esm/index.mjs.map +1 -1
- 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/objectPropertyHasDefaults.d.cts +3 -0
- package/dist/types/cjs/utils/objectPropertyHasDefaults.d.cts.map +1 -0
- 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/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 +1 -1
- package/src/createConfigBuilder.test.ts +14 -15
- package/src/createConfigBuilder.ts +8 -10
- package/src/utils/collateObjectPropertyDefaults.ts +26 -0
- package/src/utils/{objectHasInvalidDefaults.ts → objectPropertyHasDefaults.ts} +1 -1
- 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
package/package.json
CHANGED
|
@@ -205,38 +205,37 @@ describe('createConfigBuilder', () => {
|
|
|
205
205
|
expect(config.$values()).toEqual({ colors: ['red', 'yellow', 'pink', 'green'] });
|
|
206
206
|
});
|
|
207
207
|
});
|
|
208
|
-
});
|
|
209
208
|
|
|
210
|
-
|
|
211
|
-
describe('and that value is a record of booleans', () => {
|
|
209
|
+
describe('and that value is an object of key/value pairs', () => {
|
|
212
210
|
it('should throw an error', async () => {
|
|
213
211
|
const { createConfigBuilder } = await import('./createConfigBuilder.ts');
|
|
214
212
|
|
|
215
213
|
const extendedSchema = configSchema.extend({
|
|
216
|
-
flags: z
|
|
214
|
+
flags: z
|
|
215
|
+
.object({
|
|
216
|
+
alpha: z.boolean().default(true),
|
|
217
|
+
bravo: z.boolean().default(true),
|
|
218
|
+
})
|
|
219
|
+
.optional(),
|
|
217
220
|
});
|
|
218
221
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
);
|
|
222
|
+
const config = createConfigBuilder<z.infer<typeof extendedSchema>>(extendedSchema);
|
|
223
|
+
expect(config.$values()).toEqual({ flags: { alpha: true, bravo: true } });
|
|
222
224
|
});
|
|
223
225
|
});
|
|
226
|
+
});
|
|
224
227
|
|
|
225
|
-
|
|
228
|
+
describe('and a config has an invalid default value', () => {
|
|
229
|
+
describe('and that value is a record of booleans', () => {
|
|
226
230
|
it('should throw an error', async () => {
|
|
227
231
|
const { createConfigBuilder } = await import('./createConfigBuilder.ts');
|
|
228
232
|
|
|
229
233
|
const extendedSchema = configSchema.extend({
|
|
230
|
-
flags: z
|
|
231
|
-
.object({
|
|
232
|
-
alpha: z.boolean().default(true),
|
|
233
|
-
bravo: z.boolean().default(true),
|
|
234
|
-
})
|
|
235
|
-
.optional(),
|
|
234
|
+
flags: z.record(z.boolean().default(true)).optional(),
|
|
236
235
|
});
|
|
237
236
|
|
|
238
237
|
expect(() => createConfigBuilder<z.infer<typeof extendedSchema>>(extendedSchema)).toThrow(
|
|
239
|
-
'When setting schema property defaults for the
|
|
238
|
+
'When setting schema property defaults for the value of the record assigned to "flags", set them on the record and not the value.'
|
|
240
239
|
);
|
|
241
240
|
});
|
|
242
241
|
});
|
|
@@ -4,6 +4,7 @@ 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';
|
|
@@ -11,7 +12,6 @@ import { isSchemaValid } from './utils/isSchemaValid.ts';
|
|
|
11
12
|
import { isValidPropertyDefinition } from './utils/isValidPropertyDefinition.ts';
|
|
12
13
|
import { isValidValue } from './utils/isValidValue.ts';
|
|
13
14
|
import { jsonStringifyReplacer } from './utils/jsonStringifyReplacer.ts';
|
|
14
|
-
import { objectHasInvalidDefaults } from './utils/objectHasInvalidDefaults.ts';
|
|
15
15
|
import { recordHasInvalidDefaults } from './utils/recordHasInvalidDefaults.ts';
|
|
16
16
|
import { transformConfigSync } from './utils/transformConfig.ts';
|
|
17
17
|
|
|
@@ -167,20 +167,18 @@ export const createConfigBuilder = <ZodTypes>(
|
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
if (propertyDefinition.type === 'object') {
|
|
170
|
-
if (
|
|
170
|
+
if (recordHasInvalidDefaults(propertyDefinition)) {
|
|
171
171
|
throw new Error(
|
|
172
|
-
`When setting schema property defaults for the
|
|
172
|
+
`When setting schema property defaults for the value of the record assigned to "${String(
|
|
173
173
|
castProperty
|
|
174
|
-
)}", set them on the
|
|
174
|
+
)}", set them on the record and not the value.`
|
|
175
175
|
);
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
)}", set them on the object and not the property.`
|
|
183
|
-
);
|
|
178
|
+
const propertyDefaults = collateObjectPropertyDefaults(propertyDefinition);
|
|
179
|
+
|
|
180
|
+
if (propertyDefaults) {
|
|
181
|
+
config[castProperty] = propertyDefaults as Config[keyof Config];
|
|
184
182
|
}
|
|
185
183
|
}
|
|
186
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
|
+
};
|
|
@@ -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);
|
|
@@ -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"}
|