@astral/validations 3.1.1 → 3.2.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/README.md +63 -0
- package/index.d.ts +2 -1
- package/index.js +1 -0
- package/object/object.d.ts +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1237,6 +1237,8 @@ validate(new Date())
|
|
1237
1237
|
|
1238
1238
|
### [Codesandbox](https://codesandbox.io/s/astral-validations-react-hook-form-tnq4of?file=/src/Form.tsx)
|
1239
1239
|
|
1240
|
+
### Basic usage
|
1241
|
+
|
1240
1242
|
```tsx
|
1241
1243
|
import { object, string, optional } from '@astral/validations';
|
1242
1244
|
import { resolver } from '@astral/validations-react-hook-form-resolver';
|
@@ -1275,6 +1277,67 @@ const Form = () => {
|
|
1275
1277
|
};
|
1276
1278
|
```
|
1277
1279
|
|
1280
|
+
### Переиспользуемый useForm
|
1281
|
+
|
1282
|
+
```tsx
|
1283
|
+
import { ObjectGuard, object, optional, string } from '@astral/validations';
|
1284
|
+
import { resolver } from '@astral/validations-react-hook-form-resolver';
|
1285
|
+
import {
|
1286
|
+
FieldValues,
|
1287
|
+
UseFormReturn,
|
1288
|
+
UseFormProps as UseReactHookFormProps,
|
1289
|
+
useForm as useReactHookForm,
|
1290
|
+
} from 'react-hook-form';
|
1291
|
+
|
1292
|
+
type UseFormProps<TFieldValues extends FieldValues = FieldValues> = Omit<
|
1293
|
+
UseReactHookFormProps<TFieldValues>,
|
1294
|
+
'resolver'
|
1295
|
+
> & {
|
1296
|
+
validationSchema?: ObjectGuard<TFieldValues, TFieldValues>;
|
1297
|
+
};
|
1298
|
+
|
1299
|
+
const useForm = <TFieldValues extends FieldValues = FieldValues>({
|
1300
|
+
validationSchema,
|
1301
|
+
defaultValues,
|
1302
|
+
...params
|
1303
|
+
}: UseFormProps<TFieldValues>): UseFormReturn<TFieldValues> =>
|
1304
|
+
useReactHookForm<TFieldValues>({
|
1305
|
+
...params,
|
1306
|
+
defaultValues,
|
1307
|
+
resolver: validationSchema && resolver(validationSchema),
|
1308
|
+
});
|
1309
|
+
|
1310
|
+
type Values = {
|
1311
|
+
name: string;
|
1312
|
+
info: { description?: string };
|
1313
|
+
};
|
1314
|
+
|
1315
|
+
const validationSchema = object<Values>({
|
1316
|
+
name: string(),
|
1317
|
+
info: object<Values['info']>({
|
1318
|
+
description: optional(string()),
|
1319
|
+
}),
|
1320
|
+
});
|
1321
|
+
|
1322
|
+
const Form = () => {
|
1323
|
+
const { register, handleSubmit, formState } = useForm<Values>({
|
1324
|
+
validationSchema,
|
1325
|
+
});
|
1326
|
+
|
1327
|
+
return (
|
1328
|
+
<form onSubmit={handleSubmit(() => {})}>
|
1329
|
+
<input {...register('name')} />
|
1330
|
+
{formState.errors.name && <p>{formState.errors.name.message}</p>}
|
1331
|
+
<input {...register('info.description')} />
|
1332
|
+
{formState.errors.info?.description && (
|
1333
|
+
<p>{formState.errors.info.description.message}</p>
|
1334
|
+
)}
|
1335
|
+
<button type="submit">submit</button>
|
1336
|
+
</form>
|
1337
|
+
);
|
1338
|
+
};
|
1339
|
+
```
|
1340
|
+
|
1278
1341
|
# Guides
|
1279
1342
|
|
1280
1343
|
|
package/index.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
export { object, OBJECT_TYPE_ERROR_INFO, type Schema, type SchemaValue, } from './object';
|
1
|
+
export { object, OBJECT_TYPE_ERROR_INFO, type Schema, type SchemaValue, type ObjectGuard, } from './object';
|
2
2
|
export { optional } from './optional';
|
3
3
|
export { string, STRING_TYPE_ERROR_INFO } from './string';
|
4
4
|
export { date, INVALID_DATE_ERROR_INFO, DATE_TYPE_ERROR_INFO } from './date';
|
@@ -27,3 +27,4 @@ export { ogrnIP, OGRN_IP_ERROR_INFO } from './ogrnIP';
|
|
27
27
|
export { any } from './any';
|
28
28
|
export { when } from './when';
|
29
29
|
export { toPrettyError } from './toPrettyError';
|
30
|
+
export { transform } from './transform';
|
package/index.js
CHANGED
package/object/object.d.ts
CHANGED
@@ -44,7 +44,7 @@ export type Schema<TValue extends Record<string, unknown>, TValues = unknown> =
|
|
44
44
|
* });
|
45
45
|
* ```
|
46
46
|
*/
|
47
|
-
export declare const object: <
|
47
|
+
export declare const object: <TValue extends Record<string, unknown>, TValues = unknown>(schema: Schema<TValue, TValues>) => {
|
48
48
|
(value: unknown, prevCtx?: ValidationContext<TValues> | undefined): import("../core").ValidationResult;
|
49
49
|
define(overridesDefOptions: Partial<AdditionalDefOptions> & {
|
50
50
|
requiredErrorMessage?: string | undefined;
|
@@ -52,4 +52,5 @@ export declare const object: <Value extends Record<string, unknown>, TValues = u
|
|
52
52
|
isOptional?: boolean | undefined;
|
53
53
|
}): any;
|
54
54
|
};
|
55
|
+
export type ObjectGuard<TValue extends Record<string, unknown>, TValues = unknown> = ReturnType<typeof object<TValue, TValues>>;
|
55
56
|
export {};
|