MCPcopy Create free account
hub / github.com/Lemoncode/fonk / FormValidation

Class FormValidation

src/form-validation.ts:26–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24} from './helpers';
25
26export class FormValidation {
27 private fieldSchema: InternalFieldValidationSchema = {};
28 private recordSchema: InternalRecordValidationSchema = {};
29
30 constructor(validationSchema: ValidationSchema) {
31 this.setupValidationSchema(validationSchema);
32 }
33
34 private setupValidationSchema = (validationSchema: ValidationSchema) => {
35 if (validationSchema && typeof validationSchema === 'object') {
36 const { record, field } = validationSchema;
37
38 if (field && typeof field === 'object') {
39 this.fieldSchema = mapToInternalFieldValidationSchema(
40 validationSchema.field
41 );
42 }
43
44 if (record && typeof record === 'object') {
45 this.recordSchema = mapToInternalRecordValidationSchema(
46 validationSchema.record
47 );
48 }
49 } else {
50 console.error('ValidationSchema must be a valid object');
51 }
52 };
53
54 public validateField = (
55 fieldId: string,
56 value: any,
57 values?: any
58 ): Promise<ValidationResult | { [fieldId: string]: ValidationResult }> => {
59 const field = hasFieldIdArrayValidator(fieldId, this.fieldSchema)
60 ? formatFieldForArrayField(fieldId, value)
61 : { id: fieldId, value };
62
63 return validateField(field.id, field.value, values, this.fieldSchema).then(
64 (internalValidationResult) => {
65 const validationResult = mapInternalValidationResultToValidationResult(
66 internalValidationResult
67 );
68 return fieldId !== field.id
69 ? get(validationResult, fieldId)
70 : validationResult;
71 }
72 );
73 };
74
75 public validateRecord = (values: any): Promise<RecordValidationResult> =>
76 validateRecord(values, this.recordSchema);
77
78 public validateForm = (values: any): Promise<FormValidationResult> =>
79 validateForm(values, this.fieldSchema, this.recordSchema).then(
80 mapInternalFormValidationResultToFormValidationResult
81 );
82
83 public updateValidationSchema = (

Callers

nothing calls this directly

Calls 9

hasFieldIdArrayValidatorFunction · 0.90
formatFieldForArrayFieldFunction · 0.90
validateFieldFunction · 0.90
getFunction · 0.90
validateRecordFunction · 0.90
validateFormFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…