Regex not working as expected #2798
-
|
I'm trying to add a validator to ensure keys of records are written in camel case. The regex works as expected when used directly, but within the schema parser, it doesn't behave the same. I wrote a test to illustrate. import { z } from 'zod'
const REG = /^[a-z]([A-Za-z0-9]+)$/
const schema = z.record(
z.string(),
z.string().regex(REG, 'Key must be camelCase'))
describe('Zod Regex', () => {
// Valid camelCase strings
it('should idenfify valid camelCase strings directly', () => {
expect(REG.test('camelCase')).toBe(true)
expect(REG.test('camel')).toBe(true)
expect(REG.test('camelCaseId')).toBe(true)
expect(REG.test('camelCaseID')).toBe(true)
})
it('should parse zod record with valid camelCase keys', () => {
expect(() => schema.parse({ camelCase: 'value' })).not.toThrow()
expect(() => schema.parse({ camel: 'value' })).not.toThrow()
expect(() => schema.parse({ camelCaseId: 'value' })).not.toThrow()
expect(() => schema.parse({ camelCaseID: 'value' })).not.toThrow()
})
// Invalid camelCase strings
it('should idenfify invalid camelCase strings directly', () => {
expect(REG.test('CamelCase')).toBe(false)
expect(REG.test('camel_Case')).toBe(false)
expect(REG.test('camelCase_')).toBe(false)
expect(REG.test('CAMEL_CASE')).toBe(false)
expect(REG.test('_camelCase')).toBe(false)
})
it('should throw error when parsing zod record with invalid camelCase keys', () => {
expect(() => schema.parse({ CamelCase: 'value' })).toThrow()
expect(() => schema.parse({ camel_case: 'value' })).toThrow()
expect(() => schema.parse({ camelCase_: 'value' })).toThrow()
expect(() => schema.parse({ CAMEL_CASE: 'value' })).toThrow()
expect(() => schema.parse({ _camelCase: 'value' })).toThrow()
})
})The test fails during the first expect of the last test: Am I using it the wrong way? I know with the Using the latest version of Zod |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
Hey @nolawnchairs I managed to make it work Solution import { record, string, ZodError } from "../index";
const REG = /^[a-z]([A-Za-z0-9]+)$/;
const isCamelCase = (key: string) => REG.test(key);
const camelCaseKeySchema = string()
.refine(isCamelCase, {
message: 'Key must be in camelCase',
});
const schema = record(camelCaseKeySchema, string());
describe('Zod Regex', () => {
// Valid camelCase strings
it('should idenfify valid camelCase strings directly', () => {
expect(REG.test('camelCase')).toBe(true)
expect(REG.test('camel')).toBe(true)
expect(REG.test('camelCaseId')).toBe(true)
expect(REG.test('camelCaseID')).toBe(true)
})
it('should parse zod record with valid camelCase keys', () => {
expect(() => schema.parse({ camelCase: 'value' })).not.toThrow()
expect(() => schema.parse({ camel: 'value' })).not.toThrow()
expect(() => schema.parse({ camelCaseId: 'value' })).not.toThrow()
expect(() => schema.parse({ camelCaseID: 'value' })).not.toThrow()
})
// Invalid camelCase strings
it('should idenfify invalid camelCase strings directly', () => {
expect(REG.test('CamelCase')).toBe(false)
expect(REG.test('camel_Case')).toBe(false)
expect(REG.test('camelCase_')).toBe(false)
expect(REG.test('CAMEL_CASE')).toBe(false)
expect(REG.test('_camelCase')).toBe(false)
})
it('should throw error when parsing zod record with invalid camelCase keys', () => {
expect(() => schema.parse({ CamelCase: 'value' })).toThrow(ZodError)
expect(() => schema.parse({ camel_case: 'value' })).toThrow(ZodError)
expect(() => schema.parse({ camelCase_: 'value' })).toThrow(ZodError)
expect(() => schema.parse({ CAMEL_CASE: 'value' })).toThrow(ZodError)
expect(() => schema.parse({ _camelCase: 'value' })).toThrow(ZodError)
});
})is it work to you? |
Beta Was this translation helpful? Give feedback.
-
|
Is this what you are looking for? https://github.com/colinhacks/zod#record-key-type
const REG = /^[a-z]([A-Za-z0-9]+)$/
const schema = z.record(
z.string().regex( REG, 'Key must be camelCase' ),
z.string(),
)
console.log( schema.parse( { camelCase: 'value' } ) ) // { camelCase: 'value' }
const result = schema.safeParse( { NotCamelCase: 'value' } )
!result.success && console.log( result.error.issues )
// [
// {
// validation: "regex",
// code: "invalid_string",
// message: "Key must be camelCase",
// path: [ "NotCamelCase" ]
// }
// ]If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
-
|
You could also just transform the keys to camelCase regardless: import _ from 'lodash'
const schema = z.record(
z.string().transform( _.camelCase ),
z.string(),
)
console.log( schema.parse( { camelCase: 'value' } ) ) // { camelCase: 'value' }
console.log( schema.parse( { StartedAsNotCamelCase: 'value' } ) ) // { startedAsNotCamelCase: 'value' }If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
Is this what you are looking for?
https://github.com/colinhacks/zod#record-key-type
If you found my answer satisfactory, please …