Skip to content

10.1.0

Choose a tag to compare

@peterver peterver released this 09 Mar 15:45
· 54 commits to main since this release

Added

  • feat: Added support for using schemas in conditional blocks, eg:
const v = Validator.create({
    user: ['?', {
        first_name: 'string_ne',
        last_name: 'string_ne',
        email: 'email',
        details: ['?', {
            isActive: 'boolean',
            isEnabled: 'boolean',
        }],
    }],
    createdAt: 'date',
    updatedAt: '?date',
});

/* Getting the output type */
type vType = typeof v.schema;

/* This type will be equivalent to this */
type vType = {
    user: {
        first_name: string;
        last_name: string;
        email: Email;
        details: {
            isActive: boolean;
            isEnabled: boolean;
        } | undefined;
    } | undefined;
    createdAt: Date;
    updatedAt: Date | undefined;
};

This also works with multiple schemas:

const v = Validator.create({
    obj: [
        '?',
        {
            first_name: 'string_ne',
            last_name: 'string_ne',
            email: 'email',
            details: ['?', {
                isActive: 'boolean',
                isEnabled: 'boolean',
            }],
        },
        {
            type: 'string_ne',
            phone: 'phone',
        }
    ],
    createdAt: 'date',
    updatedAt: '?date',
});

/* Getting the output type */
type vType = typeof v.schema;

/* This type will be equivalent to this */
type vType = {
    user: {
        first_name: string;
        last_name: string;
        email: Email;
        details: {
            isActive: boolean;
            isEnabled: boolean;
        } | undefined;
    } | {
        type: string;
        phone: Phone;
    } undefined;
    createdAt: Date;
    updatedAt: Date | undefined;
};

And other primitive rules:

const v = Validator.create({
    obj: ['?', {
        first_name: 'string_ne',
        last_name: 'string_ne',
    }, 'string_ne'],
    createdAt: 'date',
    updatedAt: '?date',
});

/* Getting the output type */
type vType = typeof v.schema;

/* This type will be equivalent to this */
type vType = {
    obj: string | {
        first_name: string;
        last_name: string;
    } | undefined;
    createdAt: Date;
    updatedAt: Date | undefined;
}

You can even go pretty far with this (sidenote, please don't do stuff like this in production-grade application code):

const CustomValidator = Validator.extend({
    security_type: ['sso', 'credentials', 'otp'],
});

const v = CustomValidator.create({
    user: ['?', {
        details: ['?', {
            security: ['?', {types: '?[unique|min:1|max:10]security_type'}],
        }],
    }],
});

/* Getting the output type */
type vType = typeof v.schema;

/* This type will be equivalent to this */
type vType = {
    user: {
        details: {
            security: {
                types: ('sso' | 'credentials' | 'otp')[] | undefined;
            } | undefined;
        } | undefined;
    } | undefined;
}

Improved

  • deps: Upgrade @types/node to 22.13.0
  • deps: Upgrade @valkyriestudios/utils to 12.34.0
  • deps: Upgrade eslint to 9.22.0
  • deps: Upgrade typescript to 5.8.2
  • deps: Upgrade typescript-eslint to 8.26.0

Fixed

  • dx: Fixed an issue where optional array rules (eg: ?[unique|min:1]string_ne) would not be correctly inferred