routie dev init since i didn't adhere to any proper guidance up until now
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
export * from './rules.js';
|
||||
export * from './plugin.js';
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export * from "./rules.js";
|
||||
export * from "./plugin.js";
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","names":[],"sources":["../../../src/labs/rules/index.ts"],"sourcesContent":["export * from './rules'\nexport * from './plugin'\n"],"mappings":"","ignoreList":[]}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import type { App } from 'vue';
|
||||
import type { RulesOptions } from './rules.js';
|
||||
import type { LocaleInstance } from '../../composables/locale.js';
|
||||
export interface RulesPlugin {
|
||||
install: (app: App) => void;
|
||||
}
|
||||
export declare function createRulesPlugin(rules: RulesOptions, locale: LocaleInstance): RulesPlugin;
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// Composables
|
||||
import { createRules, RulesSymbol } from "./rules.js"; // Types
|
||||
export function createRulesPlugin(rules, locale) {
|
||||
return {
|
||||
install(app) {
|
||||
app.provide(RulesSymbol, createRules(rules, locale));
|
||||
}
|
||||
};
|
||||
}
|
||||
//# sourceMappingURL=plugin.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"plugin.js","names":["createRules","RulesSymbol","createRulesPlugin","rules","locale","install","app","provide"],"sources":["../../../src/labs/rules/plugin.ts"],"sourcesContent":["// Composables\nimport { createRules, RulesSymbol } from './rules'\n\n// Types\nimport type { App } from 'vue'\nimport type { RulesOptions } from './rules'\nimport type { LocaleInstance } from '@/composables/locale'\n\nexport interface RulesPlugin {\n install: (app: App) => void\n}\n\nexport function createRulesPlugin (rules: RulesOptions, locale: LocaleInstance): RulesPlugin {\n return {\n install (app: App) {\n app.provide(RulesSymbol, createRules(rules, locale))\n },\n }\n}\n"],"mappings":"AAAA;AAAA,SACSA,WAAW,EAAEC,WAAW,sBAEjC;AASA,OAAO,SAASC,iBAAiBA,CAAEC,KAAmB,EAAEC,MAAsB,EAAe;EAC3F,OAAO;IACLC,OAAOA,CAAEC,GAAQ,EAAE;MACjBA,GAAG,CAACC,OAAO,CAACN,WAAW,EAAED,WAAW,CAACG,KAAK,EAAEC,MAAM,CAAC,CAAC;IACtD;EACF,CAAC;AACH","ignoreList":[]}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
import type { InjectionKey, Ref } from 'vue';
|
||||
import type { LocaleInstance } from '../../composables/locale.js';
|
||||
import type { ValidationProps, ValidationRule } from '../../composables/validation.js';
|
||||
export type ValidationRuleBuilderWithoutOptions = (err?: string) => ValidationRule;
|
||||
export type ValidationRuleBuilderWithOptions<T> = (options: T, err?: string) => ValidationRule;
|
||||
export type CustomValidationRuleBuilder = (...args: any[]) => ValidationRule;
|
||||
export interface RuleAliases {
|
||||
[name: string]: CustomValidationRuleBuilder;
|
||||
required: ValidationRuleBuilderWithoutOptions;
|
||||
email: ValidationRuleBuilderWithoutOptions;
|
||||
number: ValidationRuleBuilderWithoutOptions;
|
||||
integer: ValidationRuleBuilderWithoutOptions;
|
||||
capital: ValidationRuleBuilderWithoutOptions;
|
||||
maxLength: ValidationRuleBuilderWithOptions<number>;
|
||||
minLength: ValidationRuleBuilderWithOptions<number>;
|
||||
strictLength: ValidationRuleBuilderWithOptions<number>;
|
||||
exclude: ValidationRuleBuilderWithOptions<string[]>;
|
||||
notEmpty: ValidationRuleBuilderWithoutOptions;
|
||||
pattern: ValidationRuleBuilderWithOptions<RegExp>;
|
||||
}
|
||||
export type RulesOptions = {
|
||||
aliases?: Partial<RuleAliases>;
|
||||
};
|
||||
type ValidationRuleParams = [any, string?];
|
||||
export type ValidationAlias = string | [string, ...ValidationRuleParams];
|
||||
export type RulesInstance = {
|
||||
resolve: (fn: () => ValidationProps['rules']) => Readonly<Ref<any[]>>;
|
||||
aliases: RuleAliases;
|
||||
};
|
||||
export declare function createRules(options: RulesOptions | undefined, locale: LocaleInstance): {
|
||||
resolve: (fn: () => ValidationProps['rules']) => import("vue").ComputedRef<([string, any, (string | undefined)?] | ValidationRule)[]>;
|
||||
aliases: RuleAliases;
|
||||
};
|
||||
export declare const RulesSymbol: InjectionKey<RulesInstance>;
|
||||
export declare function useRules(): RuleAliases;
|
||||
export declare function useRules(fn: () => ValidationProps['rules']): Readonly<Ref<ValidationProps['rules']>> | Readonly<Ref<ValidationRule[]>>;
|
||||
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
// Utilities
|
||||
import { computed, inject, toRef } from 'vue';
|
||||
|
||||
// Types
|
||||
|
||||
export function createRules(options, locale) {
|
||||
const {
|
||||
t
|
||||
} = locale;
|
||||
const aliases = {
|
||||
required: err => {
|
||||
return v => {
|
||||
// If the modifier .number is used, the 0 will be a number and it's a falsy value so we need to check for it
|
||||
return v === 0 || !!v || t(err || '$vuetify.rules.required');
|
||||
};
|
||||
},
|
||||
email: err => {
|
||||
return v => !v || typeof v === 'string' && /^.+@\S+\.\S+$/.test(v) || t(err || '$vuetify.rules.email');
|
||||
},
|
||||
number: err => {
|
||||
return v => !v || !isNaN(Number(v)) || t(err || '$vuetify.rules.number');
|
||||
},
|
||||
integer: err => {
|
||||
return v => /^[\d]*$/.test(v) || t(err || '$vuetify.rules.integer');
|
||||
},
|
||||
capital: err => {
|
||||
return v => /^[A-Z]*$/.test(v) || t(err || '$vuetify.rules.capital');
|
||||
},
|
||||
maxLength: (len, err) => {
|
||||
return v => !v || v.length <= len || t(err || '$vuetify.rules.maxLength', len);
|
||||
},
|
||||
minLength: (len, err) => {
|
||||
return v => !v || v.length >= len || t(err || '$vuetify.rules.minLength', len);
|
||||
},
|
||||
strictLength: (len, err) => {
|
||||
return v => !v || v.length === len || t(err || '$vuetify.rules.strictLength', len);
|
||||
},
|
||||
exclude: (forbiddenCharacters, err) => {
|
||||
return v => {
|
||||
let error = true;
|
||||
for (const character of forbiddenCharacters) {
|
||||
if (v.includes(character)) error = err || t('$vuetify.rules.exclude', character);
|
||||
}
|
||||
return error;
|
||||
};
|
||||
},
|
||||
notEmpty: err => {
|
||||
return v => v && v.length > 0 || t(err || '$vuetify.rules.notEmpty');
|
||||
},
|
||||
pattern: (pattern, err) => {
|
||||
return v => !v || pattern.test(v) || t(err || '$vuetify.rules.pattern');
|
||||
},
|
||||
...options?.aliases
|
||||
};
|
||||
function resolve(fn) {
|
||||
return computed(() => fn().map(rule => {
|
||||
let ruleName = null;
|
||||
let ruleParams = [undefined];
|
||||
if (Array.isArray(rule)) {
|
||||
ruleName = rule[0];
|
||||
ruleParams = rule.slice(1);
|
||||
} else if (typeof rule === 'string') {
|
||||
ruleName = rule;
|
||||
}
|
||||
if (ruleName !== null) {
|
||||
if (ruleName.startsWith('$')) {
|
||||
ruleName = ruleName.slice(1);
|
||||
}
|
||||
return aliases[ruleName]?.(...ruleParams);
|
||||
} else {
|
||||
return rule;
|
||||
}
|
||||
}));
|
||||
}
|
||||
return {
|
||||
resolve,
|
||||
aliases
|
||||
};
|
||||
}
|
||||
export const RulesSymbol = Symbol.for('vuetify:rules');
|
||||
export function useRules(fn) {
|
||||
const rules = inject(RulesSymbol, null);
|
||||
if (!fn) {
|
||||
if (!rules) {
|
||||
throw new Error('Could not find Vuetify rules injection');
|
||||
}
|
||||
return rules.aliases;
|
||||
}
|
||||
return rules?.resolve(fn) ?? toRef(fn);
|
||||
}
|
||||
//# sourceMappingURL=rules.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user