diff --git a/README.md b/README.md index a407c55..9e42eff 100644 --- a/README.md +++ b/README.md @@ -87,11 +87,11 @@ Imagine a hypothetical page component ## Make Required Schema Based on Configuration -You can make a Zod schema required based on a configuration (components need) using the makeSchemaRequired function. +You can make a Zod schema required based on a configuration (components need) using the transformSchema function. ```ts import { z } from 'zod'; -import { makeSchemaRequired } from 'adaptate'; +import { transformSchema } from 'adaptate'; const schema = z.object({ name: z.string().optional(), @@ -112,7 +112,7 @@ const config = { }, }; -const updatedSchema = makeSchemaRequired(schema, config); +const updatedSchema = transformSchema(schema, config); updatedSchema.parse({ name: 'Davin', @@ -194,7 +194,7 @@ I have attempted to recreate what I have done at work with the help of **ChatGPT ```diff import { z, ZodObject, ZodArray, ZodTypeAny } from 'zod'; --export function makeSchemaRequired(schema: ZodTypeAny, config: any, parentData: any = {}) { +-export function transformSchema(schema: ZodTypeAny, config: any, parentData: any = {}) { - const schemaWithConditionalRequirements = applyConditionalRequirements(schema, config, parentData); - - if (schemaWithConditionalRequirements instanceof ZodObject && typeof config === 'object' && !Array.isArray(config)) { @@ -204,7 +204,7 @@ I have attempted to recreate what I have done at work with the help of **ChatGPT - if (config[key] === true) { - return [key, value.required()]; - } else if (typeof config[key] === 'object') { -- return [key, makeSchemaRequired(value, config[key], parentData)]; +- return [key, transformSchema(value, config[key], parentData)]; - } - return [key, value]; - }) @@ -212,10 +212,10 @@ I have attempted to recreate what I have done at work with the help of **ChatGPT - return z.object(newShape).required(); - } else if (schemaWithConditionalRequirements instanceof ZodArray && config['*']) { - const elementSchema = schemaWithConditionalRequirements.element; -- return z.array(makeSchemaRequired(elementSchema, config['*'], parentData)); +- return z.array(transformSchema(elementSchema, config['*'], parentData)); - } - return schemaWithConditionalRequirements; -+export function makeSchemaRequired( ++export function transformSchema( + schema: ZodTypeAny, + config: Config +): ZodTypeAny { @@ -274,7 +274,7 @@ I have attempted to recreate what I have done at work with the help of **ChatGPT + + if (schema instanceof ZodArray && config['*']) { + // @ts-ignore -+ updatedSchema = makeSchemaRequired(schema.element, config['*']); ++ updatedSchema = transformSchema(schema.element, config['*']); + updatedSchema = z.array(schema.element.merge(updatedSchema)); + } else if (schema instanceof ZodObject) { + // @ts-ignore diff --git a/coverage/coverage-summary.json b/coverage/coverage-summary.json index fc71d0c..6df7743 100644 --- a/coverage/coverage-summary.json +++ b/coverage/coverage-summary.json @@ -1,5 +1,5 @@ -{"total": {"lines":{"total":197,"covered":197,"skipped":0,"pct":100},"statements":{"total":197,"covered":197,"skipped":0,"pct":100},"functions":{"total":8,"covered":8,"skipped":0,"pct":100},"branches":{"total":82,"covered":75,"skipped":0,"pct":91.46},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}} -,"/Users/peram/code/adaptate/packages/core/src/index.ts": {"lines":{"total":82,"covered":82,"skipped":0,"pct":100},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":82,"covered":82,"skipped":0,"pct":100},"branches":{"total":36,"covered":36,"skipped":0,"pct":100}} +{"total": {"lines":{"total":199,"covered":199,"skipped":0,"pct":100},"statements":{"total":199,"covered":199,"skipped":0,"pct":100},"functions":{"total":8,"covered":8,"skipped":0,"pct":100},"branches":{"total":84,"covered":77,"skipped":0,"pct":91.66},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}} +,"/Users/peram/code/adaptate/packages/core/src/index.ts": {"lines":{"total":84,"covered":84,"skipped":0,"pct":100},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":84,"covered":84,"skipped":0,"pct":100},"branches":{"total":38,"covered":38,"skipped":0,"pct":100}} ,"/Users/peram/code/adaptate/packages/utils/src/load-yaml.ts": {"lines":{"total":14,"covered":14,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":14,"covered":14,"skipped":0,"pct":100},"branches":{"total":1,"covered":1,"skipped":0,"pct":100}} ,"/Users/peram/code/adaptate/packages/utils/src/openapi.ts": {"lines":{"total":101,"covered":101,"skipped":0,"pct":100},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":101,"covered":101,"skipped":0,"pct":100},"branches":{"total":45,"covered":38,"skipped":0,"pct":84.44}} } diff --git a/package.json b/package.json index 0b25e40..b83fe1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "adaptate", - "version": "0.1.0-beta.2", + "version": "0.1.0-beta.3", "author": { "name": "Peramanathan Sathyamoorthy", "url": "https://github.com/p10ns11y/adaptate.git" diff --git a/packages/core/README.md b/packages/core/README.md index 9acc52c..7fa03a1 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -70,11 +70,11 @@ Imagine a hypothetical page component ## Make Required Schema Based on Configuration -You can make a Zod schema required based on a configuration (components need) using the makeSchemaRequired function. +You can make a Zod schema required based on a configuration (components need) using the transformSchema function. ```ts import { z } from 'zod'; -import { makeSchemaRequired } from '@adaptate/core'; +import { transformSchema } from '@adaptate/core'; const schema = z.object({ name: z.string().optional(), @@ -95,7 +95,7 @@ const config = { }, }; -const updatedSchema = makeSchemaRequired(schema, config); +const updatedSchema = transformSchema(schema, config); updatedSchema.parse({ name: 'Davin', diff --git a/packages/core/package.json b/packages/core/package.json index 5052b28..1970026 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@adaptate/core", - "version": "0.1.0-beta.2", + "version": "0.1.0-beta.3", "author": { "name": "Peramanathan Sathyamoorthy", "url": "https://github.com/p10ns11y/adaptate.git" diff --git a/packages/core/src/__tests__/index.test.ts b/packages/core/src/__tests__/index.test.ts index e2253e8..ac769b8 100644 --- a/packages/core/src/__tests__/index.test.ts +++ b/packages/core/src/__tests__/index.test.ts @@ -9,9 +9,9 @@ import { getDereferencedOpenAPIDocument, openAPISchemaToZod, } from '@adaptate/utils/openapi'; -import { makeSchemaRequired, applyConditionalRequirements } from '../'; +import { transformSchema, applyConditionalRequirements } from '../'; -describe('makeSchemaRequired', () => { +describe('transformSchema', () => { it('should make properties required based on the config', async () => { let baseSchema = z.object({ category: z @@ -46,7 +46,7 @@ describe('makeSchemaRequired', () => { type: true, }; - let transformedSchema = makeSchemaRequired(baseSchema, config); + let transformedSchema = transformSchema(baseSchema, config); let validData = { category: { @@ -61,7 +61,19 @@ describe('makeSchemaRequired', () => { type: 'electronics', }; - let invalidDataMissingName = { + let invalidDataMissingCategoryName = { + category: { + subcategories: [ + { + name: 'Phones', + items: ['iPhone', 'Samsung Galaxy', 'Google Pixel'], + }, + ], + }, + type: 'electronics', + }; + + let invalidDataMissingSubCategoryName = { category: { subcategories: [{ items: ['iPhone', 'Samsung Galaxy'] }], }, @@ -83,9 +95,11 @@ describe('makeSchemaRequired', () => { expect(() => transformedSchema.parse(validData)).not.toThrow(); - expect(() => baseSchema.parse(invalidDataMissingName)).not.toThrow(); + expect(() => + baseSchema.parse(invalidDataMissingSubCategoryName) + ).not.toThrow(); - expect(() => transformedSchema.parse(invalidDataMissingName)) + expect(() => transformedSchema.parse(invalidDataMissingSubCategoryName)) .toThrowErrorMatchingInlineSnapshot(` [ZodError: [ { @@ -115,7 +129,7 @@ describe('makeSchemaRequired', () => { // Re transforming the schema with different config // Here making warrantyPeriod required - let reTransformedSchema = makeSchemaRequired(transformedSchema, { + let reTransformedSchema = transformSchema(transformedSchema, { warrantyPeriod: true, }); @@ -134,6 +148,19 @@ describe('makeSchemaRequired', () => { ]] `); + let reReTransformedSchema = transformSchema(reTransformedSchema, { + category: { + name: false, + }, + }); + + expect(() => + reReTransformedSchema.parse({ + ...invalidDataMissingCategoryName, + warrantyPeriod: '2 years', + }) + ).not.toThrow(); + expect(() => baseSchema.parse({})).not.toThrow(); expect(() => baseSchema.parse({ @@ -210,7 +237,7 @@ describe('makeSchemaRequired', () => { }, }; - let anotherTransformedSchema = makeSchemaRequired( + let anotherTransformedSchema = transformSchema( z.array(baseSchema), anotherConfig ); @@ -232,14 +259,16 @@ describe('makeSchemaRequired', () => { dereferencedOpenAPIDocument['components']['schemas']['Category'] ); - let yetAnotherTransformedSchema = makeSchemaRequired(dataZodSchema, config); + let yetAnotherTransformedSchema = transformSchema(dataZodSchema, config); expect(() => yetAnotherTransformedSchema.parse(validData['category']) ).not.toThrow(); expect(() => - yetAnotherTransformedSchema.parse(invalidDataMissingName['category']) + yetAnotherTransformedSchema.parse( + invalidDataMissingSubCategoryName['category'] + ) ).toThrow(); expect(() => @@ -261,7 +290,7 @@ describe('makeSchemaRequired', () => { }, }; - const transformedSchema = makeSchemaRequired(baseSchema, config); + const transformedSchema = transformSchema(baseSchema, config); const validData = [{ name: 'John', age: 30 }]; const invalidData = [{ age: 30 }]; @@ -299,7 +328,7 @@ describe('makeSchemaRequired', () => { }, }; - const transformedSchema = makeSchemaRequired(baseSchema, config); + const transformedSchema = transformSchema(baseSchema, config); const validData = { category: { @@ -330,7 +359,7 @@ describe('makeSchemaRequired', () => { const config = {}; expect(() => - makeSchemaRequired(invalidSchema, config) + transformSchema(invalidSchema, config) ).toThrowErrorMatchingInlineSnapshot( `[Error: The given schema must be a Zod object.]` ); @@ -344,7 +373,7 @@ describe('makeSchemaRequired', () => { // @ts-ignore const config = []; // @ts-ignore - const transformedSchema = makeSchemaRequired(baseSchema, config); + const transformedSchema = transformSchema(baseSchema, config); expect(transformedSchema).toBeInstanceOf(z.ZodObject); // @ts-ignore diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 1e23e64..f90958b 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -26,7 +26,7 @@ export type Config = Record; * city: true * } * }; - * const updatedSchema = makeSchemaRequired(schema, config); + * const updatedSchema = transformSchema(schema, config); * const validData = { * name: 'John Doe', * address: { city: 'New York' }, @@ -42,10 +42,10 @@ export type Config = Record; * schema.parse(invalidDataMissingName); // Should fail due to missing 'name' * schema.parse(invalidDataMissingCity); // Should fail due to missing 'address.city' * @category Helper - * @module makeSchemaRequired + * @module transformSchema * */ // @ts-ignore -export function makeSchemaRequired( +export function transformSchema( schema: ZodTypeAny, config: Config ): ZodTypeAny { @@ -73,6 +73,9 @@ export function makeSchemaRequired( if (partialConfig[key] === true) { // @ts-ignore return [key, unwrappedValue]; + } else if (partialConfig[key] === false) { + // @ts-ignore + return [key, unwrappedValue.optional()]; } else if (typeof partialConfig[key] === 'object') { // @ts-ignore return [key, extendSchema(value, partialConfig[key])]; @@ -104,7 +107,7 @@ export function makeSchemaRequired( if (schema instanceof ZodArray && config['*']) { // @ts-ignore - updatedSchema = makeSchemaRequired(schema.element, config['*']); + updatedSchema = transformSchema(schema.element, config['*']); updatedSchema = z.array(schema.element.merge(updatedSchema)); } else if (schema instanceof ZodObject) { // @ts-ignore diff --git a/packages/utils/package.json b/packages/utils/package.json index f70b7ea..2d6a02a 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@adaptate/utils", - "version": "0.1.0-beta.2", + "version": "0.1.0-beta.3", "author": { "name": "Peramanathan Sathyamoorthy", "url": "https://github.com/p10ns11y/adaptate.git"