improve: validation logic.

This commit is contained in:
Darshan committed 2025-02-27 19:55:54 +05:30
1 parent 86f140c1bf
commit 73253518b9
7 files changed
+22 -69

No files matched your search

+8
View File
@@ -11,6 +11,8 @@
export let id: string;
export let name: string = id;
export let value = '';
export let pattern: string = null;
export let patternError: string = '';
export let placeholder = '';
export let required = false;
export let hideRequired = false;
@@ -45,6 +47,11 @@
return;
}
if (patternError && element.validity.patternMismatch) {
error = patternError;
return;
}
error = element.validationMessage;
};
@@ -118,6 +125,7 @@
{disabled}
{readonly}
{required}
{pattern}
{maxlength}
autocomplete={autocomplete ? 'on' : 'off'}
type="text"
+2 -15
View File
@@ -50,19 +50,6 @@ export function formatNum(number: number): string {
}
/**
* Validates a hostname, optionally allowing wildcards.
*
* @param {string} hostname - The hostname to validate.
* @param {boolean} [wildcard=true] - Whether to allow wildcard hostnames (e.g., `*.example.com`).
* Returns a regex to check hostname validity. Supports wildcards too!
*/
export function isHostnameValid(hostname: string, wildcard: boolean = true): boolean {
if (!hostname) return false;
if (!wildcard && hostname.startsWith('*.')) return false;
const hostnameRegex = wildcard
? /^(\*\.)?([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}$/
: /^([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}$/;
return hostnameRegex.test(hostname);
}
export const hostnameRegex = String.raw`(\*)|(\*\.)?(?!-)[A-Za-z0-9\-]+([\-\.]{1}[a-z0-9]+)*\.[A-Za-z]{2,18}`;
+1 -7
View File
@@ -68,12 +68,10 @@
}
async function submit() {
let interceptorResponse = null;
if ($wizard.interceptor) {
$wizard.nextDisabled = true;
try {
interceptorResponse = await $wizard.interceptor();
await $wizard.interceptor();
} catch (error) {
if (!$wizard.interceptorNotificationEnabled) return;
addNotification({
@@ -86,10 +84,6 @@
}
}
// maintains backward compatibility.
// explicit boolean check as the return type can be undefined/void too!
if (typeof interceptorResponse === 'boolean' && interceptorResponse === false) return;
wizard.setInterceptor(null);
if (isLastStep) {
if ($wizard.finalAction) {
+2 -7
View File
@@ -4,14 +4,9 @@
import { onDestroy } from 'svelte';
/**
* Pass callback that is run before the Form submit,
* can be canceled by throwing an `Exception` or returning `false`.
*
* Example -
* 1. validate > throw Error > notification is shown
* 2. validate > return False > show your UI as no notification is shown.
* Pass callback that is run before the Form submit, can be canceled with throwing an exception.
*/
export let beforeSubmit: () => Promise<void> | Promise<boolean> = null;
export let beforeSubmit: () => Promise<void> = null;
export let nextDisabled = false;
$: wizard.setNextDisabled(nextDisabled);
+1 -1
View File
@@ -8,7 +8,7 @@ export type WizardStore = {
media?: string;
component?: typeof SvelteComponent<unknown>;
cover?: typeof SvelteComponent<unknown>;
interceptor?: () => Promise<void> | Promise<boolean>;
interceptor?: () => Promise<void>;
finalAction?: () => Promise<void>;
nextDisabled: boolean;
step: number;
@@ -1,14 +1,14 @@
<script lang="ts">
import { page } from '$app/stores';
import { Pill } from '$lib/elements';
import { FormList, Helper, InputText } from '$lib/elements/forms';
import { FormList, InputText } from '$lib/elements/forms';
import { WizardStep } from '$lib/layout';
import { sdk } from '$lib/stores/sdk';
import { createPlatform } from '../store';
import { Submit, trackEvent } from '$lib/actions/analytics';
import { PlatformType } from '@appwrite.io/console';
import { isValueOfStringEnum } from '$lib/helpers/types';
import { isHostnameValid } from '$lib/helpers/string';
import { hostnameRegex } from '$lib/helpers/string';
// enum Platform {
// Android = 'flutter-android',
@@ -27,8 +27,6 @@
? $createPlatform.type
: PlatformType.Flutterandroid;
let error = null;
const projectId = $page.params.project;
const suggestions = ['*.vercel.app', '*.netlify.app', '*.gitpod.io'];
const placeholder: Partial<
@@ -87,14 +85,6 @@
};
async function beforeSubmit() {
error = null;
// double-check the hostname value.
if (platform === PlatformType.Flutterweb && !isHostnameValid($createPlatform.hostname)) {
error = 'Please enter a valid hostname';
return false;
}
if ($createPlatform.$id) {
await sdk.forConsole.projects.deletePlatform(projectId, $createPlatform.$id);
}
@@ -124,10 +114,6 @@
[PlatformType.Flutterwindows]: 'Package name',
[PlatformType.Flutterweb]: 'Hostname'
}[platform];
$: if (!$createPlatform.hostname && error) {
error = null;
}
</script>
<WizardStep {beforeSubmit}>
@@ -188,12 +174,10 @@
placeholder={placeholder[platform].hostname}
tooltip={placeholder[platform].tooltip}
required
pattern={hostnameRegex}
patternError="Please enter a valid hostname"
bind:value={$createPlatform.hostname} />
{#if error}
<Helper type="warning">{error}</Helper>
{/if}
<div class="u-flex u-gap-16 u-margin-block-start-8">
{#each suggestions as suggestion}
<Pill
@@ -2,27 +2,18 @@
import { page } from '$app/stores';
import { Alert } from '$lib/components';
import { Pill } from '$lib/elements';
import { FormList, Helper, InputText } from '$lib/elements/forms';
import { FormList, InputText } from '$lib/elements/forms';
import { WizardStep } from '$lib/layout';
import { sdk } from '$lib/stores/sdk';
import { createPlatform } from '../store';
import { Submit, trackEvent } from '$lib/actions/analytics';
import { PlatformType } from '@appwrite.io/console';
import { isHostnameValid } from '$lib/helpers/string';
import { hostnameRegex } from '$lib/helpers/string';
let error = null;
const projectId = $page.params.project;
const suggestions = ['*.vercel.app', '*.netlify.app', '*.gitpod.io'];
async function beforeSubmit() {
error = null;
// double-check the hostname value.
if (!isHostnameValid($createPlatform.hostname)) {
error = 'Please enter a valid hostname';
return false;
}
if ($createPlatform.$id) {
await sdk.forConsole.projects.updatePlatform(
projectId,
@@ -51,10 +42,6 @@
$createPlatform.$id = platform.$id;
}
$: if (!$createPlatform.hostname && error) {
error = null;
}
</script>
<WizardStep {beforeSubmit}>
@@ -73,12 +60,10 @@
placeholder="localhost"
tooltip="The hostname that your website will use to interact with the Appwrite APIs in production or development environments. No protocol or port number required."
required
pattern={hostnameRegex}
patternError="Please enter a valid hostname"
bind:value={$createPlatform.hostname} />
{#if error}
<Helper type="warning">{error}</Helper>
{/if}
<div class="u-flex u-gap-16 u-margin-block-start-8">
{#each suggestions as suggestion}
<Pill