-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Async with params working, withAsync, withParams with async rules
- Loading branch information
1 parent
96affd4
commit a593731
Showing
21 changed files
with
381 additions
and
422 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
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
133 changes: 133 additions & 0 deletions
133
packages/core/src/core/useRegle/useStateProperties/createReactiveRuleStatus.ts
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,133 @@ | ||
import { Ref, computed, reactive, ref, watch } from 'vue'; | ||
import { isEmpty } from '../../../helpers'; | ||
import { | ||
CustomRulesDeclarationTree, | ||
InlineRuleDeclaration, | ||
InternalRuleType, | ||
RegleRuleDefinition, | ||
RegleRuleDefinitionProcessor, | ||
RegleSoftRuleStatus, | ||
} from '../../../types'; | ||
import { unwrapRuleParameters } from '../../createRule/unwrapRuleParameters'; | ||
import { isFormInline, isFormRuleDefinition } from '../guards'; | ||
|
||
export function createReactiveRuleStatus({ | ||
$dirty, | ||
customMessages, | ||
rule, | ||
ruleKey, | ||
state, | ||
}: { | ||
state: Ref<unknown>; | ||
ruleKey: string; | ||
rule: Ref<InlineRuleDeclaration<any>> | Ref<RegleRuleDefinition<any, any>>; | ||
$dirty: Ref<boolean>; | ||
customMessages: Partial<CustomRulesDeclarationTree>; | ||
}) { | ||
const $pending = ref(false); | ||
const $valid = ref(true); | ||
|
||
const $active = computed<boolean>(() => { | ||
if (isFormInline(rule)) { | ||
return true; | ||
} else { | ||
if (typeof rule.value.active === 'function') { | ||
return rule.value.active(state.value, ...$params.value); | ||
} else { | ||
return rule.value.active; | ||
} | ||
} | ||
}); | ||
|
||
const $message = computed<string>(() => { | ||
let message = ''; | ||
const customMessageRule = customMessages[ruleKey]?.message; | ||
|
||
if (customMessageRule) { | ||
if (typeof customMessageRule === 'function') { | ||
message = customMessageRule(state.value, ...$params.value); | ||
} else { | ||
message = customMessageRule; | ||
} | ||
} | ||
if (isFormRuleDefinition(rule)) { | ||
if (!(customMessageRule && !rule.value._patched)) { | ||
if (typeof rule.value.message === 'function') { | ||
message = rule.value.message(state.value, ...$params.value); | ||
} else { | ||
message = rule.value.message; | ||
} | ||
} | ||
} | ||
|
||
if (isEmpty(message)) { | ||
message = 'Error'; | ||
console.warn(`No error message defined for ${ruleKey}`); | ||
} | ||
|
||
return message; | ||
}); | ||
|
||
const $type = computed(() => { | ||
if (isFormInline(rule)) { | ||
return ruleKey; | ||
} else { | ||
return Object.values(InternalRuleType).includes(rule.value.type) ? ruleKey : rule.value.type; | ||
} | ||
}); | ||
|
||
const $validator = computed<RegleRuleDefinitionProcessor<any, any, boolean | Promise<boolean>>>( | ||
() => { | ||
if (isFormInline(rule)) { | ||
return rule.value; | ||
} else { | ||
return rule.value.validator; | ||
} | ||
} | ||
); | ||
|
||
const $params = computed<any[]>(() => { | ||
if (typeof rule.value === 'function') { | ||
return []; | ||
} | ||
return unwrapRuleParameters(rule.value._params ?? []); | ||
}); | ||
|
||
watch( | ||
[state, $dirty, $params], | ||
async () => { | ||
const validator = $validator.value; | ||
let ruleResult = false; | ||
const resultOrPromise = validator(state.value, ...$params.value); | ||
|
||
if (resultOrPromise instanceof Promise) { | ||
if ($dirty.value) { | ||
try { | ||
$valid.value = true; | ||
$pending.value = true; | ||
const promiseResult = await resultOrPromise; | ||
ruleResult = promiseResult; | ||
} catch (e) { | ||
ruleResult = false; | ||
} finally { | ||
$pending.value = false; | ||
} | ||
} | ||
} else { | ||
ruleResult = resultOrPromise; | ||
} | ||
$valid.value = ruleResult; | ||
}, | ||
{ immediate: true, deep: true } | ||
); | ||
|
||
return reactive({ | ||
$message, | ||
$active, | ||
$pending, | ||
$type, | ||
$valid, | ||
$validator, | ||
...($params.value.length && { $params }), | ||
}) satisfies RegleSoftRuleStatus; | ||
} |
Oops, something went wrong.