-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,38 @@ | ||
import type { NormalizedField, RuleOptions, RuleResult } from '@/forms/axioma' | ||
|
||
export class ValidateFieldRules { | ||
private setPossibleErrors(field: NormalizedField, result: RuleResult) { | ||
if (result === true) | ||
private setPossibleErrors(field: NormalizedField, result: RuleResult, options?: RuleOptions) { | ||
if (result === true || options?.preventErrorMessage) | ||
return | ||
|
||
field.errors = [result] | ||
} | ||
|
||
private isValidReturn(result: unknown, errorMessage: string): RuleResult { | ||
if (typeof result === 'string' || (typeof result === 'boolean' && result === true)) | ||
return result | ||
|
||
throw new Error(errorMessage) | ||
} | ||
|
||
execute(field: NormalizedField, options?: RuleOptions): RuleResult { | ||
if (!field.rules) | ||
return true | ||
|
||
const errorMessage = `Unable to validate the field ${field.name}, rules function needs to return string or true. Otherwise you will need to process the initial return` | ||
let result: RuleResult | ||
const rawResult = field.rules(field.modelValue) | ||
|
||
if (options?.processResult) { | ||
const result = options.processResult(rawResult) | ||
this.setPossibleErrors(field, result) | ||
result = options.processResult(rawResult) | ||
result = this.isValidReturn(result, errorMessage) | ||
this.setPossibleErrors(field, result, options) | ||
|
||
return result | ||
} | ||
|
||
if (typeof rawResult === 'string' || (typeof rawResult === 'boolean' && rawResult === true)) { | ||
this.setPossibleErrors(field, rawResult) | ||
return rawResult | ||
} | ||
|
||
throw new Error( | ||
`Unable to validate the field ${field.name}, rules function needs to return string or true. | ||
Otherwise you will need to process the initial return`, | ||
) | ||
result = this.isValidReturn(rawResult, errorMessage) | ||
this.setPossibleErrors(field, result, options) | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { RuleOptions, RuleOptionsManager } from '@/forms/axioma' | ||
|
||
const ruleOptions = new Map<symbol, RuleOptions>() | ||
|
||
export class RuleOptionsManagerHandler implements RuleOptionsManager { | ||
constructor(private id: symbol) {} | ||
|
||
private getRuleOptionsFromMap() { | ||
if (!ruleOptions.get(this.id)) | ||
ruleOptions.set(this.id, {}) | ||
|
||
return ruleOptions.get(this.id)! | ||
} | ||
|
||
setRuleOptions(newRuleOptions: RuleOptions) { | ||
Object.assign(this.getRuleOptionsFromMap(), newRuleOptions) | ||
} | ||
|
||
getRuleOptions(): RuleOptions { | ||
return this.getRuleOptionsFromMap() | ||
} | ||
|
||
removeRuleOptions() { | ||
ruleOptions.delete(this.id) | ||
} | ||
} |