mirror of
https://github.com/itdoginfo/podkop.git
synced 2025-12-14 15:36:52 +03:00
feat: migrate validatePath to modular
This commit is contained in:
@@ -2,3 +2,4 @@ export * from './validateIp';
|
||||
export * from './validateDomain';
|
||||
export * from './validateDns';
|
||||
export * from './validateUrl';
|
||||
export * from './validatePath';
|
||||
|
||||
39
fe-app-podkop/src/validators/tests/validatePath.test.js
Normal file
39
fe-app-podkop/src/validators/tests/validatePath.test.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { validatePath } from '../validatePath';
|
||||
|
||||
export const validPaths = [
|
||||
['Single level', '/etc'],
|
||||
['Nested path', '/usr/local/bin'],
|
||||
['With dash', '/var/log/nginx-access'],
|
||||
['With underscore', '/opt/my_app/config'],
|
||||
['With numbers', '/data123/files'],
|
||||
['With dots', '/home/user/.config'],
|
||||
['Deep nested', '/a/b/c/d/e/f/g'],
|
||||
];
|
||||
|
||||
export const invalidPaths = [
|
||||
['Empty string', ''],
|
||||
['Missing starting slash', 'usr/local'],
|
||||
['Only dot', '.'],
|
||||
['Space inside', '/path with space'],
|
||||
['Illegal char', '/path$'],
|
||||
['Backslash not allowed', '\\windows\\path'],
|
||||
['Relative path ./', './relative'],
|
||||
['Relative path ../', '../parent'],
|
||||
];
|
||||
|
||||
describe('validatePath', () => {
|
||||
describe.each(validPaths)('Valid path: %s', (_desc, path) => {
|
||||
it(`returns valid=true for "${path}"`, () => {
|
||||
const res = validatePath(path);
|
||||
expect(res.valid).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe.each(invalidPaths)('Invalid path: %s', (_desc, path) => {
|
||||
it(`returns valid=false for "${path}"`, () => {
|
||||
const res = validatePath(path);
|
||||
expect(res.valid).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
25
fe-app-podkop/src/validators/validatePath.ts
Normal file
25
fe-app-podkop/src/validators/validatePath.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { ValidationResult } from './types';
|
||||
|
||||
export function validatePath(value: string): ValidationResult {
|
||||
if (!value) {
|
||||
return {
|
||||
valid: false,
|
||||
message: 'Path cannot be empty',
|
||||
};
|
||||
}
|
||||
|
||||
const pathRegex = /^\/[a-zA-Z0-9_\-/.]+$/;
|
||||
|
||||
if (pathRegex.test(value)) {
|
||||
return {
|
||||
valid: true,
|
||||
message: 'Valid',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
valid: false,
|
||||
message:
|
||||
'Invalid path format. Path must start with "/" and contain valid characters',
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user