Skip to content

Commit

Permalink
feat: removed ruleHelpers alias, replaced by direct imports
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi committed Dec 24, 2024
1 parent e204b41 commit 4dab715
Show file tree
Hide file tree
Showing 55 changed files with 238 additions and 271 deletions.
8 changes: 4 additions & 4 deletions docs/src/advanced-usage/async-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To declare an async rule, you simply have to use the `async await` syntax.

```ts
const myAsyncRule = async (value: Maybe<number>) => {
if (ruleHelpers.isFilled(value)) {
if (isFilled(value)) {
return await someStuff();
}
return true;
Expand All @@ -40,7 +40,7 @@ In the same way of an inline rule, your validator function must be using `async
```ts
const myAsyncRule = createRule({
validator(value: Maybe<string>) {
if (ruleHelpers.isFilled(value)) {
if (isFilled(value)) {
return await someStuff();
}
return true;
Expand Down Expand Up @@ -92,12 +92,12 @@ function timeout(count: number) {
}
// ---cut---
import { createRule, useRegle, type Maybe } from '@regle/core';
import { email, ruleHelpers } from '@regle/rules';
import { email, isEmpty } from '@regle/rules';
import { ref } from 'vue';
const checkEmailExists = createRule({
async validator(value: Maybe<string>) {
if (ruleHelpers.isEmpty(value) || !email.exec(value)) {
if (isEmpty(value) || !email.exec(value)) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions docs/src/advanced-usage/rule-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ You can use `createRule` to define your custom rules. Let's explore a real-world
// @filename strongPassword.ts
// ---cut---
import { createRule, Maybe } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isFilled } from '@regle/rules';
import { passwordStrength, type Options } from 'check-password-strength';

export const strongPassword = createRule({
validator(value: Maybe<string>, options?: Options<string>) {
if (ruleHelpers.isFilled(value)) {
if (isFilled(value)) {
const result = passwordStrength(value, options);
return {
$valid: result.id > 1,
Expand Down
46 changes: 23 additions & 23 deletions docs/src/advanced-usage/validations-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ To simplify this process, Regle provides a set of utility functions to assist in
These utilities can be accessed via:

```ts
import { ruleHelpers } from '@regle/rules';
import { isFilled, isEmpty, getSize, ... } from '@regle/rules';
```

## Runtime and Type guards
Expand All @@ -27,16 +27,16 @@ You can base your validator result on this.
const check = (value: any) => false;
//---cut---
import { createRule } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isFilled } from '@regle/rules';

const rule = createRule({
validator(value: unknown) {
if (ruleHelpers.isFilled(value)) {
if (isFilled(value)) {
return check(value);
}
return true;
},
message: ''
message: 'Error'
})
```

Expand All @@ -50,17 +50,17 @@ This is the inverse of `isFilled`. It will check if the value is in any way empt
const check = (value: any) => false;
//---cut---
import { createRule, type Maybe } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isEmpty } from '@regle/rules';

const rule = createRule({
validator(value: Maybe<string>) {
if (ruleHelpers.isEmpty(value)) {
if (isEmpty(value)) {
return true;
}
return check(value);
// ^?
},
message: ''
message: 'Error'
})
```

Expand All @@ -74,17 +74,17 @@ This also returns false for `NaN`, so this is better than `typeof value === "num
const checkNumber = (value: number) => false;
//---cut---
import { createRule, type Maybe } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isFilled, isNumber } from '@regle/rules';

const rule = createRule({
validator(value: Maybe<number | string>) {
if (ruleHelpers.isFilled(value) && ruleHelpers.isNumber(value)) {
if (isFilled(value) && isNumber(value)) {
return checkNumber(value);
// ^?
}
return true;
},
message: ''
message: 'Error'
})
```

Expand All @@ -98,58 +98,58 @@ This can also check strings.
const checkDate = (value: Date) => false;
//---cut---
import { createRule, type Maybe } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isFilled, isDate } from '@regle/rules';

const rule = createRule({
validator(value: Maybe<string | Date>) {
if (ruleHelpers.isFilled(value) && ruleHelpers.isDate(value)) {
if (isFilled(value) && isDate(value)) {
return checkDate(value);
// ^?
}
return true;
},
message: ''
message: 'Error'
})
```

## Operations utils

### `size`
### `getSize`

This helper will return the length of any data type you pass.
It works with strings, arrays, objects and numbers.

```ts twoslash
import { createRule, type Maybe } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isFilled, getSize } from '@regle/rules';

const rule = createRule({
validator(value: Maybe<string | Array<number>>) {
if (ruleHelpers.isFilled(value)) {
return ruleHelpers.size(value) > 6;
if (isFilled(value)) {
return getSize(value) > 6;
}
return true;
},
message: ''
message: 'Error'
})
```

### `regex`
### `matchRegex`

This utility can take multiple regular expressions as arguments. It checks the input's validity and tests it against the provided regex patterns.

```ts twoslash
import { createRule, type Maybe } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isFilled, matchRegex } from '@regle/rules';

const rule = createRule({
validator(value: Maybe<string>, ...regexps: RegExp[]) {
if (ruleHelpers.isFilled(value)) {
return ruleHelpers.regex(value, ...regexps);
if (isFilled(value)) {
return matchRegex(value, ...regexps);
}
return true;
},
message: ''
message: 'Error'
})
```

Expand Down
4 changes: 2 additions & 2 deletions docs/src/core-concepts/global-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ const someAsyncCall = async () => await Promise.resolve(true);
// ---cut---
// @noErrors
import { defineRegleConfig, createRule, Maybe } from '@regle/core';
import { withMessage, ruleHelpers } from '@regle/rules';
import { withMessage, isFilled } from '@regle/rules';

const asyncEmail = createRule({
async validator(value: Maybe<string>) {
if (!ruleHelpers.isFilled(value)) {
if (!isFilled(value)) {
return true;
}

Expand Down
12 changes: 6 additions & 6 deletions docs/src/core-concepts/rules/reusable-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Example: Recreating a simple `required` rule
```ts twoslash
// @noErrors
import { createRule } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isFilled } from '@regle/rules';

export const required = createRule({
validator: (value: unknown) => {
return ruleHelpers.isFilled(value);
return isFilled(value);
},
message: 'This field is required',
});
Expand Down Expand Up @@ -165,15 +165,15 @@ useRegle(state, rules);
```ts twoslash include requiredIf [requiredIf.ts]
// @noErrors
import { createRule, useRegle } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isFilled } from '@regle/rules';
import { ref } from 'vue';

export const requiredIf = createRule({
validator(value: unknown, condition: boolean) {
// Params like `condition` will always be unwrapped here
// no need to check if it's a value, a ref or a getter function
if (condition) {
return ruleHelpers.isFilled(value);
return isFilled(value);
}
return true;
},
Expand Down Expand Up @@ -268,12 +268,12 @@ function timeout(count: number) {
}
// ---cut---
import { createRule, useRegle, type Maybe } from '@regle/core';
import { email, ruleHelpers } from '@regle/rules';
import { email, isEmpty } from '@regle/rules';
import { ref } from 'vue';
const checkEmailExists = createRule({
async validator(value: Maybe<string>) {
if (ruleHelpers.isEmpty(value) || !email.exec(value)) {
if (isEmpty(value) || !email.exec(value)) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions docs/src/integrations/nuxt.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ The following exports will become globally available in your Nuxt application:
- defineRegleConfig
- inferRules
- `@regle/rules` Note: Built-in rules are not auto-injected to minimize the risk of name conflicts.
- ruleHelpers
- withAsync
- withMessage
- withParams
- withTooltip
- `@regle/zod` (if present)
- useZodRegle

- `@regle/valibot` (if present)
- useValibotRegle
14 changes: 5 additions & 9 deletions docs/src/parts/components/metadata/UsingMetadataCreateRule.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
<button type="button" @click="r$.$resetAll">Reset</button>
</div>

<div
class="password-strength"
:class="[`level-${r$.$fields.password.$rules.strongPassword.$metadata.result?.id}`]">
<div class="password-strength" :class="[`level-${r$.$fields.password.$rules.strongPassword.$metadata.result?.id}`]">
</div>

<ul v-if="r$.$errors.password.length">
Expand All @@ -21,27 +19,25 @@
</li>
</ul>

<div v-else-if="r$.$fields.password.$valid" class="success">
Your password is strong enough
</div>
<div v-else-if="r$.$fields.password.$valid" class="success"> Your password is strong enough </div>
</div>
</template>

<script setup lang="ts">
import { createRule, useRegle, type Maybe } from '@regle/core';
import { ruleHelpers } from '@regle/rules';
import { isFilled } from '@regle/rules';
import { passwordStrength, type Options } from 'check-password-strength';
const strongPassword = createRule({
validator: (value: Maybe<string>, options?: Options<string>) => {
if (ruleHelpers.isFilled(value)) {
if (isFilled(value)) {
const result = passwordStrength(value, options);
return {
$valid: result.id > 1,
result,
};
}
return { $valid: true };
},
message({ result }) {
Expand Down
4 changes: 2 additions & 2 deletions docs/src/parts/components/rules/AsyncRule.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<script setup lang="ts">
import { createRule, useRegle, type Maybe } from '@regle/core';
import { email, ruleHelpers } from '@regle/rules';
import { email, isEmpty } from '@regle/rules';
import { ref } from 'vue';
function randomBoolean(): boolean {
Expand All @@ -33,7 +33,7 @@ function timeout(count: number) {
const checkEmailExists = createRule({
async validator(value: Maybe<string>) {
if (ruleHelpers.isEmpty(value) || !email.exec(value)) {
if (isEmpty(value) || !email.exec(value)) {
return true;
}
await timeout(1000);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/core/createRule/createRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import { getFunctionParametersLength } from './unwrapRuleParameters';
*
* ```ts
* // Create a simple rule with no params
* import {ruleHelpers} from '@regle/rules';
* import {isFilled} from '@regle/rules';
*
* export const isFoo = createRule({
* validator(value: string) {
* if (ruleHelpers.isFilled(value)) {
* if (isFilled(value)) {
* return value === 'foo';
* }
* return true
Expand Down
4 changes: 1 addition & 3 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export default defineNuxtModule<ModuleOptions>({

addImportsSources({
from: '@regle/rules',
imports: ['ruleHelpers', 'withAsync', 'withMessage', 'withParams', 'withTooltip'] as Array<
keyof typeof import('@regle/rules')
>,
imports: ['withAsync', 'withMessage', 'withParams', 'withTooltip'] as Array<keyof typeof import('@regle/rules')>,
});

try {
Expand Down
2 changes: 1 addition & 1 deletion packages/rules/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { withTooltip } from './withTooltip';
export { withAsync } from './withAsync';
export { withParams } from './withParams';
export { applyIf } from './applyIf';
export { ruleHelpers } from './ruleHelpers';
export { matchRegex, getSize, isDate, isEmpty, isFilled, isNumber, toDate, toNumber } from './ruleHelpers';
export { and } from './and';
export { or } from './or';
export { not } from './not';
6 changes: 3 additions & 3 deletions packages/rules/src/helpers/not.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
RegleRuleMetadataDefinition,
} from '@regle/core';
import { createRule } from '@regle/core';
import { ruleHelpers } from './ruleHelpers';
import { isFilled } from './ruleHelpers';

export function not<
TValue,
Expand Down Expand Up @@ -40,15 +40,15 @@ export function not<

if (_async) {
newValidator = async (value: any, ...params: any[]) => {
if (ruleHelpers.isFilled(value)) {
if (isFilled(value)) {
const result = await validator(value, ...(params as any));
return !result;
}
return true;
};
} else {
newValidator = (value: any, ...params: any[]) => {
if (ruleHelpers.isFilled(value)) {
if (isFilled(value)) {
return !validator(value, ...(params as any));
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MaybeRef } from 'vue';
import { unref } from 'vue';

export function size(value: MaybeRef<string | any[] | Record<string, any> | number>): number {
export function getSize(value: MaybeRef<string | any[] | Record<string, any> | number>): number {
const _value = unref(value);
if (Array.isArray(_value)) return _value.length;
if (typeof _value === 'object') {
Expand Down
Loading

0 comments on commit 4dab715

Please sign in to comment.