Skip to content

Commit

Permalink
some validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lgestc committed Nov 13, 2024
1 parent a3375b8 commit f30cb5e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { ValidationFunc } from '@kbn/es-ui-shared-plugin/static/forms/hook_form_lib/types';
import { emailValidator, genericValidator } from './fields_config';

describe('emailValidator', () => {
it('should return an error if the value is not a string', () => {
const result = emailValidator({
value: undefined,
path: 'email',
} as Parameters<ValidationFunc>[0]);

expect(result).toEqual({
code: 'ERR_NOT_STRING',
message: 'Value should be a string',
path: 'email',
});
});

it('should return an error if the value is not a valid email', () => {
const result = emailValidator({
value: 'invalid-email',
path: 'email',
} as Parameters<ValidationFunc>[0]);
expect(result).toEqual({
code: 'ERR_NOT_EMAIL',
message: 'Value should be an email',
path: 'email',
});
});

it('should return undefined if the value is a valid email', () => {
const result = emailValidator({
value: '[email protected]',
path: 'email',
} as Parameters<ValidationFunc>[0]);
expect(result).toBeUndefined();
});
});

describe('genericValidator', () => {
it('should return an error if the value is not a string', () => {
const result = genericValidator({
value: 123,
path: 'generic',
} as Parameters<ValidationFunc>[0]);
expect(result).toEqual({
code: 'ERR_NOT_STRING',
message: 'Value should be a string',
path: 'generic',
});
});

it('should return an error if the value is not valid', () => {
const result = genericValidator({
value: 'invalid value!',
path: 'generic',
} as Parameters<ValidationFunc>[0]);
expect(result).toEqual({
code: 'ERR_NOT_VALID',
message: 'Value is invalid',
path: 'generic',
});
});

it('should return undefined if the value is valid', () => {
const result = genericValidator({
value: 'valid_value',
path: 'generic',
} as Parameters<ValidationFunc>[0]);
expect(result).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ export const normalizeValueType = (value: string): keyof typeof fieldsConfig.val
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const GENERIC_REGEX = /^[a-zA-Z0-9._:/\\]+$/;

const notStringError = (path: string) => ({
code: 'ERR_NOT_STRING',
message: 'Value should be a string',
path,
});

const { emptyField } = fieldValidators;

const emailValidator: ValidationFunc = (...args: Parameters<ValidationFunc>) => {
export const emailValidator: ValidationFunc = (...args: Parameters<ValidationFunc>) => {
const [{ value, path }] = args;

if (typeof value !== 'string') {
return emptyField('Value is required')(...args);
return notStringError(path);
}

if (!EMAIL_REGEX.test(value)) {
Expand All @@ -38,11 +44,11 @@ const emailValidator: ValidationFunc = (...args: Parameters<ValidationFunc>) =>
}
};

const genericValidator: ValidationFunc = (...args: Parameters<ValidationFunc>) => {
export const genericValidator: ValidationFunc = (...args: Parameters<ValidationFunc>) => {
const [{ value, path }] = args;

if (typeof value !== 'string') {
return emptyField('Value is required')(...args);
return notStringError(path);
}

if (!GENERIC_REGEX.test(value)) {
Expand Down

0 comments on commit f30cb5e

Please sign in to comment.