Form validation
Two optional entries ship ready-made schemas. Install the library you already use; it’s an optional peer dependency.
import { provinceCitySchema, cityIdSchema, citySchema } from '@code-plate/iran-cities/zod';
provinceCitySchema.parse({ provinceId: '7', cityId: '292' }); // { provinceId: 7, cityId: 292 }provinceCitySchema.safeParse({ provinceId: 28, cityId: 292 }); // error on cityId: «این شهر در استان انتخابشده نیست.»cityIdSchema.parse('۲۹۲'); // 292, Persian digits workcitySchema.parse('شیراز'); // the full city objectimport * as v from 'valibot';import { provinceCitySchema } from '@code-plate/iran-cities/valibot';
v.parse(provinceCitySchema, { provinceId: 7, cityId: 292 });| Schema | Accepts | Output |
|---|---|---|
provinceIdSchema |
number or numeric string | province id |
cityIdSchema |
number or numeric string | city id |
provinceSchema |
name, English id or id | Province |
citySchema |
name, English id or id | FlatCity |
provinceCitySchema |
{ provinceId, cityId } |
ids; the error is reported on cityId |
Custom messages
Section titled “Custom messages”Messages are Persian by default. Pass your own:
import { createZodSchemas } from '@code-plate/iran-cities/zod';
const schemas = createZodSchemas({ province: 'Please choose a province', city: 'Please choose a city', cityNotInProvince: 'This city is not in the selected province',});createValibotSchemas works the same way.