Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion for a new feature - Allow date in format ISO 8601 #65

Open
padinko opened this issue Jul 17, 2024 · 1 comment
Open

Discussion for a new feature - Allow date in format ISO 8601 #65

padinko opened this issue Jul 17, 2024 · 1 comment

Comments

@padinko
Copy link

padinko commented Jul 17, 2024

Package version

2.1.0

Describe the bug

It is currently impossible to validate dates in ISO8601 format.

Day.js requires ISO8601 format when using the dayjs(value) function. documentation

but this plugin force to use dayjs(value, formats, true) and there is no format for ISO8601 string in available formats

ISO8601 format is most common format for dates, can we use it in vine date validator please?

Reproduction repo

No response

@padinko
Copy link
Author

padinko commented Jul 17, 2024

I created an adonisjs provider to handle ISO8601 dates if anyone needs it:

import vine, { symbols, Vine, VineDate } from '@vinejs/vine';
import { messages } from '@vinejs/vine/defaults';
import dayjs from 'dayjs';

const isIsoDate = vine.createRule((value, _, field) => {
    if (typeof value !== 'string') {
        field.report(messages.date, 'date', field);
        return;
    }

    const dateTime = dayjs(value);
    if (!dateTime.isValid()) {
        field.report(messages.date, 'date', field);
        return;
    }

    field.meta.$value = dateTime;
    field.mutate(dateTime.toDate(), field);
});

class VineDateIso extends VineDate {
    constructor() {
        super(undefined, [isIsoDate()]);
    }

    [symbols.UNIQUE_NAME] = 'vine.dateIso';

    [symbols.IS_OF_TYPE] = (value: any) => {
        if (typeof value !== 'string') {
            return false;
        }

        return dayjs(value).isValid();
    };
}

declare module '@vinejs/vine' {
    interface Vine {
        dateIso(): VineDateIso;
    }
}

export default class VineDateIsoProvider {
    /**
     * The container bindings have booted
     */
    async boot() {
        Vine.macro('dateIso', () => new VineDateIso());
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant