mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
send all file
This commit is contained in:
+32
-19
@@ -23,28 +23,41 @@ export const zodDataUrlSchema = z.string().refine(value => zodDataUrlRegex.test(
|
||||
message: 'Invalid data URL',
|
||||
});
|
||||
|
||||
|
||||
//===============================================================================
|
||||
/**
|
||||
* Validates if a string is a valid data URL.
|
||||
* A schema for validating Base64 encoded strings.
|
||||
*
|
||||
* @param url - The string to validate as a data URL.
|
||||
* @returns {void} - Logs the result of the validation.
|
||||
* This schema checks if the string is valid Base64 and ensures that the first 20 characters
|
||||
* do not contain a comma.
|
||||
*
|
||||
* @example
|
||||
* validateDataUrl('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDCAAAAC0BEMAAW9R3AAAAAElFTkSuQmCC');
|
||||
* // Output: Valid data URL: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDCAAAAC0BEMAAW9R3AAAAAElFTkSuQmCC
|
||||
*
|
||||
* validateDataUrl('invalid-url');
|
||||
* // Output: Validation error: [ ... ]
|
||||
* ```typescript
|
||||
* const validString = 'SGVsbG8sIHdvcmxkIQ=='; // Base64 for "Hello, world!"
|
||||
* const invalidString = 'SGVsbG8sIHdvcmxkIQ,,=='; // Invalid due to commas
|
||||
*
|
||||
* console.log(base64Schema.safeParse(validString).success); // true
|
||||
* console.log(base64Schema.safeParse(invalidString).success); // false
|
||||
* ```
|
||||
*/
|
||||
const validateDataUrl = (url: string) => {
|
||||
const result: any = zodDataUrlSchema.safeParse(url);
|
||||
if (result.success) {
|
||||
console.log('Valid data URL:', url);
|
||||
} else {
|
||||
console.error('Validation error:', result.error.errors);
|
||||
}
|
||||
};
|
||||
export const base64Schema = z.string().refine(value => {
|
||||
// Regular expression for Base64 validation
|
||||
const isBase64 = /^[A-Za-z0-9+/=]*$/.test(value) && (value.length % 4 === 0);
|
||||
// Check if the first 20 characters do not contain a comma
|
||||
return isBase64 && !value.substring(0, 20).includes(',');
|
||||
}, {
|
||||
message: 'Invalid Base64 string or comma found in the first 20 characters'
|
||||
});
|
||||
|
||||
// Test the schema
|
||||
validateDataUrl('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDCAAAAC0BEMAAW9R3AAAAAElFTkSuQmCC');
|
||||
validateDataUrl('invalid-url');
|
||||
/**
|
||||
* Validates a given string against the Base64 schema.
|
||||
*
|
||||
* @param input - The string to validate.
|
||||
* @returns `true` if the input is valid according to the schema, `false` otherwise.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* console.log(validateInput('SGVsbG8sIHdvcmxkIQ==')); // true
|
||||
* console.log(validateInput('SGVsbG8sIHdvcmxkIQ,,==')); // false
|
||||
* ```
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { err, ok } from 'neverthrow';
|
||||
import { ZodError, ZodSchema, z } from 'zod';
|
||||
export function zodSafeValidation<T>(schema: ZodSchema, data) {
|
||||
const validation = (schema as ZodSchema<T>).safeParse(data)
|
||||
|
||||
if(validation.success) {
|
||||
return ok(validation.data)
|
||||
} else {
|
||||
return err((validation.error))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user