Skip to content

Commit

Permalink
fix: fixed edge handling with Native Dates and Files values
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi committed Dec 19, 2024
1 parent 9c89c5a commit c3de719
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"check-password-strength": "2.0.10",
"cross-env": "7.0.3",
"@pinia/testing": "0.1.7",
"date-fns": "4.1.0",
"eslint": "catalog:",
"eslint-config-prettier": "catalog:",
"eslint-plugin-vue": "catalog:",
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/core/useRegle/inferRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ export interface inferRulesFn<TCustomRules extends Partial<AllRulesDeclarations>
>(
state: MaybeRef<TState> | DeepReactiveState<TState> | undefined,
rulesFactory: TRules
): TRules;
<TState extends PrimitiveTypes, TRules extends RegleRuleDecl>(state: MaybeRef<TState>, rulesFactory: TRules): TRules;
): NoInferLegacy<TRules>;
<TState extends PrimitiveTypes, TRules extends RegleRuleDecl>(
state: MaybeRef<TState>,
rulesFactory: TRules
): NoInferLegacy<TRules>;
}

export function createInferRuleHelper<
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/utils/object.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import { effectScope, isRef, unref, type MaybeRef, type Ref } from 'vue';
import type { MaybeGetter } from '../types';

export function isObject(obj: unknown): obj is Record<string, any> {
if (obj instanceof Date || obj instanceof File) {
return false;
}
return typeof obj === 'object' && obj !== null && !Array.isArray(obj);
}

export function isRefObject(obj: Ref<unknown>): obj is Ref<Record<string, any>> {
if (obj.value instanceof Date || obj.value instanceof File) {
return false;
}
return isObject(obj.value);
}

Expand Down
4 changes: 2 additions & 2 deletions playground/vue3/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<template>
<!-- <TestForm /> -->
<Test15 />
<Test14 />
</template>

<script setup lang="ts">
import Test15 from './components/Test15.vue';
import Test14 from './components/Test14.vue';
// const form = ref({ name: [''] });
Expand Down
26 changes: 15 additions & 11 deletions playground/vue3/src/components/Test14.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,38 @@
</div>
<div>
<input
v-model="form.name"
:class="{ valid: r$.$fields.name.$valid, error: r$.$fields.name.$error }"
:placeholder="`Type your name${r$.$fields.name.$rules.required.$active ? '*' : ''}`"
type="date"
v-model="form.foo"
:class="{ valid: r$.$fields.foo.$valid, error: r$.$fields.foo.$error }"
:placeholder="`Type your foo${r$.$fields.foo.$rules.required.$active ? '*' : ''}`"
/>
<button type="button" @click="r$.$resetAll">Reset</button>
</div>
<ul v-if="r$.$errors.name.length">
<li v-for="error of r$.$errors.name" :key="error">
<ul v-if="r$.$errors.foo.length">
<li v-for="error of r$.$errors.foo" :key="error">
{{ error }}
</li>
</ul>
</div>
<JSONViewer :data="r$.$fields.name"></JSONViewer>
<JSONViewer :data="r$"></JSONViewer>
</template>

<script setup lang="ts">
import { useRegle } from '@regle/core';
import { inferRules, useRegle } from '@regle/core';
import { applyIf, minLength, required } from '@regle/rules';
import { ref } from 'vue';
import JSONViewer from './JSONViewer.vue';
const form = ref({ name: '' });
type Form = {
foo: Date;
};
const form = ref<Form>({ foo: new Date() });
const condition = ref(false);
const { r$ } = useRegle(form, {
name: {
required: applyIf(condition, required),
minLength: applyIf(condition, minLength(3)),
foo: {
required,
},
});
</script>
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions tests/unit/useRegle/nested/nested.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { useRegle } from '@regle/core';
import { required } from '@regle/rules';
import { dateAfter, dateBefore, required } from '@regle/rules';
import { ref } from 'vue';
import { createRegleComponent } from '../../../utils/test.utils';
import { shouldBeInvalidField, shouldBeValidField } from '../../../utils/validations.utils';
import { shouldBeErrorField, shouldBeInvalidField, shouldBeValidField } from '../../../utils/validations.utils';
import { addDays } from 'date-fns';

describe('nested validations', () => {
function nestedCollectionRules() {
const form = ref({
level0: { level1: { name: '' } },
testDate: null as Date | null,
});

return useRegle(form, {
level0: {
level1: { name: { required } },
},
testDate: { required, dateAfter: dateAfter(addDays(new Date(), 1)) },
});
}

Expand All @@ -23,19 +26,23 @@ describe('nested validations', () => {
shouldBeInvalidField(vm.r$.$fields.level0);
shouldBeInvalidField(vm.r$.$fields.level0.$fields.level1);
shouldBeInvalidField(vm.r$.$fields.level0.$fields.level1.$fields.name);
shouldBeInvalidField(vm.r$.$fields.testDate);

vm.r$.$value = {
level0: {
level1: {
name: 'foobar',
},
},
testDate: new Date(),
};

await vm.$nextTick();

shouldBeValidField(vm.r$.$fields.level0);
shouldBeValidField(vm.r$.$fields.level0.$fields.level1);
shouldBeValidField(vm.r$.$fields.level0.$fields.level1.$fields.name);
shouldBeErrorField(vm.r$.$fields.testDate);

vm.r$.$resetAll();

Expand All @@ -44,18 +51,21 @@ describe('nested validations', () => {
shouldBeInvalidField(vm.r$.$fields.level0);
shouldBeInvalidField(vm.r$.$fields.level0.$fields.level1);
shouldBeInvalidField(vm.r$.$fields.level0.$fields.level1.$fields.name);
shouldBeInvalidField(vm.r$.$fields.testDate);

vm.r$.$value = {
level0: {
level1: {
name: 'foobar',
},
},
testDate: addDays(new Date(), 3),
};
await vm.$nextTick();

shouldBeValidField(vm.r$.$fields.level0);
shouldBeValidField(vm.r$.$fields.level0.$fields.level1);
shouldBeValidField(vm.r$.$fields.level0.$fields.level1.$fields.name);
shouldBeValidField(vm.r$.$fields.testDate);
});
});

0 comments on commit c3de719

Please sign in to comment.