Skip to content

Commit

Permalink
fix: regex rule don't work with applyIf modifier #63
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi committed Feb 24, 2025
1 parent 87b536d commit 782be0e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/src/core-concepts/rules/built-in-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ const { r$ } = useRegle({ aliment: 'Fish' }, {
## `regex`

_**Params**_
- `...regexps: [...Ref<RegExp> | RegExp | () => RegExp]`
- `...regexps: [MaybeRefOrGetter<RegExp | RegExp[]>]`

Checks if the value matches one or more regular expressions.

Expand Down
8 changes: 8 additions & 0 deletions docs/src/core-concepts/rules/reusable-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ myValidator(5)
myValidator(5, 'foo', 'bar');
```


:::warning

While adding spread parameters `...anyOtherArg` is supported, keep in mind that it will receive every parameters, even the ones injected by a parent modifier like `applyIf`, `and`, `or` etc..

So it's not advised to use it
:::

### Reactivity

The real advantage of using `createRule` is that it automatically registers parameters as reactive dependencies. This means your rule works seamlessly with plain values, refs, or getter functions.
Expand Down
22 changes: 12 additions & 10 deletions packages/rules/src/rules/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { isFilled, matchRegex } from '../helpers';
/**
* Checks if the value matches one or more regular expressions.
*/
export const regex: RegleRuleWithParamsDefinition<string | number, [...regexp: RegExp[]], false, boolean> = createRule({
type: 'regex',
validator(value: Maybe<string | number>, ...regexp: RegExp[]) {
if (isFilled(value)) {
return matchRegex(value, ...regexp);
}
return true;
},
message: 'This field does not match the required pattern',
});
export const regex: RegleRuleWithParamsDefinition<string | number, [regexp: RegExp | RegExp[]], false, boolean> =
createRule({
type: 'regex',
validator(value: Maybe<string | number>, regexp: RegExp | RegExp[]) {
if (isFilled(value)) {
const filteredRegxp = Array.isArray(regexp) ? regexp : [regexp];
return matchRegex(value, ...filteredRegxp);
}
return true;
},
message: 'This field does not match the required pattern',
});
4 changes: 2 additions & 2 deletions packages/rules/src/rules/tests/regex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('regex validator', () => {
it('validates truthy values against regex', () => {
expect(regex(/ad/).exec('aaa')).toBe(false);
expect(regex(/ad/).exec('ad')).toBe(true);
expect(regex(/^a.*d$/, /\d{3}/).exec('ads')).toBe(false);
expect(regex(/^a.*d$/, /\d{3}/).exec('a123d')).toBe(true);
expect(regex([/^a.*d$/, /\d{3}/]).exec('ads')).toBe(false);
expect(regex([/^a.*d$/, /\d{3}/]).exec('a123d')).toBe(true);
});
});
8 changes: 4 additions & 4 deletions playground/vue3/src/components/Test13.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<main>
<input type="file" @change="handleEvent" placeholder="Competency" />
<input v-model="data.competency" placeholder="test" />
<ul v-if="r$Merged.$errors.r$.competency">
<li v-for="error of r$Merged.$errors.r$.competency" :key="error">
{{ error }}
Expand All @@ -22,7 +22,7 @@

<script setup lang="ts">
import { mergeRegles, useRegle } from '@regle/core';
import { numeric, required } from '@regle/rules';
import { applyIf, numeric, regex, required, withMessage } from '@regle/rules';
import { ref } from 'vue';
import JSONViewer from './JSONViewer.vue';
Expand All @@ -32,7 +32,7 @@ function handleEvent(event: Event) {
}
const data = ref({
competency: new Date(),
competency: 0,
level: { id: 1 },
});
Expand All @@ -42,7 +42,7 @@ const data2 = ref({
});
const { r$ } = useRegle(data, {
competency: { required },
competency: { number: applyIf(() => true, withMessage(regex(/\d/), 'Your password must have one number')) },
level: {
id: { required },
},
Expand Down

0 comments on commit 782be0e

Please # to comment.