Skip to content

Commit

Permalink
fix(): remove console error
Browse files Browse the repository at this point in the history
  • Loading branch information
horprogs committed Dec 30, 2021
1 parent 35ade7f commit b0fd2ea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
16 changes: 6 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import isBefore from 'date-fns/isAfter';
import isValid from 'date-fns/isValid';
import parse from 'date-fns/parse';

import type {
PluginValueType,
PluginContextType,
} from 'just-validate/dist/modules/interfaces';
type PluginValueType = string | boolean;
type PluginFieldsType = {
[key: string]: any;
};

interface ConfigInterface {
format?: string;
Expand Down Expand Up @@ -46,10 +46,6 @@ const getComparedDate = (
}

if (!isValid(sourceDate)) {
console.error(
'It is impossible convert value to the valid Date. Dates should be in the same format as defined in format field. The result will be always invalid'
);

return null;
}

Expand Down Expand Up @@ -83,8 +79,8 @@ const checkIsAfter = (
};

const pluginDate =
(func: (fields: PluginContextType) => ConfigInterface) =>
(value: PluginValueType, fields: PluginContextType) => {
(func: (fields: PluginFieldsType) => ConfigInterface) =>
(value: PluginValueType, fields: PluginFieldsType) => {
const config = func(fields);
const valid: KeysEnum<ConfigInterface> = {
format: true,
Expand Down
10 changes: 9 additions & 1 deletion src/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('plugin date', () => {
}))('2021-10-05', {})
).toBeFalsy();

expect(console.error).toHaveBeenCalled();
expect(console.error).not.toHaveBeenCalled();
// @ts-ignore
console.error.mockReset();

Expand Down Expand Up @@ -85,6 +85,7 @@ describe('plugin date', () => {
format: 'dd/MM/yyyy',
}))('10/12/2021', {})
).toBeTruthy();

expect(
pluginDate(() => ({
format: 'dd/MM/yyyy',
Expand All @@ -108,6 +109,13 @@ describe('plugin date', () => {
format: 'd MMM yy',
}))('01/12/2021', {})
).toBeFalsy();

// https://github.com/date-fns/date-fns/issues/2087
// expect(
// pluginDate(() => ({
// format: 'dd/MM/yyyy',
// }))('01/12/21', {})
// ).toBeFalsy();
});

test('should be able to validate isBefore', async () => {
Expand Down
4 changes: 2 additions & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default defineConfig(({ command, mode }) => {
build: {
lib: {
entry: path.resolve(__dirname, 'src/main.ts'),
name: 'JustValidate',
name: 'JustValidatePluginDate',
fileName: (format) =>
`just-validate.${format === 'umd' ? 'production.min' : format}.js`,
`just-validate-plugin-date.${format === 'umd' ? 'production.min' : format}.js`,
},
rollupOptions: {},
},
Expand Down

0 comments on commit b0fd2ea

Please sign in to comment.