Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 85 additions & 4 deletions packages/schema/src/tests/utils/idl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ describe('idl', () => {
expect(
schemaToIdl({
schema: Schema,
value: {username: 'David', status: {type: 'active', owner: 'abc'}}
value: {username: 'Hello', status: {type: 'active', owner: 'abc'}}
})
).toEqual({
username: 'David',
username: 'Hello',
status: {active: {owner: 'abc'}}
});
});
Expand Down Expand Up @@ -132,13 +132,94 @@ describe('idl', () => {
expect(
schemaFromIdl({
schema: Schema,
value: {username: 'David', status: {active: {owner: 'abc'}}}
value: {username: 'Hello', status: {active: {owner: 'abc'}}}
})
).toEqual({
username: 'David',
username: 'Hello',
status: {type: 'active', owner: 'abc'}
});
});
});
});

describe('camel case', () => {
describe('object with camelCase fields', () => {
const Schema = z.object({firstName: z.string(), lastName: z.string()});

it('converts camelCase keys to snake_case', () => {
expect(
schemaToIdl({schema: Schema, value: {firstName: 'Hello', lastName: 'World'}})
).toEqual({
first_name: 'Hello',
last_name: 'World'
});
});
});

describe('object with camelCase fields', () => {
const Schema = z.object({firstName: z.string(), lastName: z.string()});

it('converts snake_case keys back to camelCase', () => {
expect(
schemaFromIdl({schema: Schema, value: {first_name: 'Hello', last_name: 'World'}})
).toEqual({
firstName: 'Hello',
lastName: 'World'
});
});
});

describe('nested object with camelCase fields', () => {
const Schema = z.object({
userId: z.string(),
userProfile: z.object({displayName: z.string()})
});

it('converts nested camelCase keys to snake_case', () => {
expect(
schemaToIdl({
schema: Schema,
value: {userId: 'abc', userProfile: {displayName: 'Hello'}}
})
).toEqual({
user_id: 'abc',
user_profile: {display_name: 'Hello'}
});
});

it('converts nested snake_case keys back to camelCase', () => {
expect(
schemaFromIdl({
schema: Schema,
value: {user_id: 'abc', user_profile: {display_name: 'Hello'}}
})
).toEqual({
userId: 'abc',
userProfile: {displayName: 'Hello'}
});
});
});
});

describe('snake_case fields', () => {
const Schema = z.object({first_name: z.string(), last_name: z.string()});

it('keeps snake_case keys unchanged in schemaToIdl', () => {
expect(
schemaToIdl({schema: Schema, value: {first_name: 'Hello', last_name: 'World'}})
).toEqual({
first_name: 'Hello',
last_name: 'World'
});
});

it('keeps snake_case keys unchanged in schemaFromIdl', () => {
expect(
schemaFromIdl({schema: Schema, value: {first_name: 'Hello', last_name: 'World'}})
).toEqual({
first_name: 'Hello',
last_name: 'World'
});
});
});
});
11 changes: 9 additions & 2 deletions packages/schema/src/utils/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export interface IdlParams {
value: unknown;
}

// Duplicate utils to avoid to reference library utils in schema
const convertCamelToSnake = (str: string): string =>
str.replace(/([a-zA-Z])(?=[A-Z])/g, '$1_').toLowerCase();

/**
* Recursively converts a JavaScript value to its Candid IDL representation,
* guided by a Zod schema.
Expand Down Expand Up @@ -53,7 +57,7 @@ export const schemaToIdl = ({schema, value}: IdlParams): unknown => {
if (schema instanceof z.ZodObject) {
return Object.fromEntries(
Object.entries(schema._zod.def.shape).map(([k, t]) => [
k,
convertCamelToSnake(k),
schemaToIdl({schema: t as z.core.$ZodType, value: (value as Record<string, unknown>)[k]})
])
);
Expand Down Expand Up @@ -129,7 +133,10 @@ export const schemaFromIdl = ({schema, value}: IdlParams): unknown => {
return Object.fromEntries(
Object.entries(schema._zod.def.shape).map(([k, t]) => [
k,
schemaFromIdl({schema: t as z.core.$ZodType, value: (value as Record<string, unknown>)[k]})
schemaFromIdl({
schema: t as z.core.$ZodType,
value: (value as Record<string, unknown>)[convertCamelToSnake(k)]
})
])
);
}
Expand Down
Loading