mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
* Fixing the regex that is used to validate the Space id * Adding basic jest tests for the space schema's ID
This commit is contained in:
parent
a6327d64fb
commit
6c562ff1ed
2 changed files with 65 additions and 1 deletions
64
x-pack/plugins/spaces/server/lib/space_schema.test.ts
Normal file
64
x-pack/plugins/spaces/server/lib/space_schema.test.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { spaceSchema } from './space_schema';
|
||||
|
||||
const defaultProperties = {
|
||||
id: 'foo',
|
||||
name: 'foo',
|
||||
};
|
||||
|
||||
describe('#id', () => {
|
||||
test('is optional', () => {
|
||||
const result = spaceSchema.validate({
|
||||
...defaultProperties,
|
||||
id: undefined,
|
||||
});
|
||||
expect(result.error).toBeNull();
|
||||
});
|
||||
|
||||
test('allows lowercase a-z, 0-9, "_" and "-"', () => {
|
||||
const result = spaceSchema.validate({
|
||||
...defaultProperties,
|
||||
id: 'abcdefghijklmnopqrstuvwxyz0123456789_-',
|
||||
});
|
||||
expect(result.error).toBeNull();
|
||||
});
|
||||
|
||||
test(`doesn't allow uppercase`, () => {
|
||||
const result = spaceSchema.validate({
|
||||
...defaultProperties,
|
||||
id: 'Foo',
|
||||
});
|
||||
expect(result.error).toMatchInlineSnapshot(
|
||||
`[ValidationError: child "id" fails because ["id" with value "Foo" fails to match the lower case, a-z, 0-9, "_", and "-" are allowed pattern]]`
|
||||
);
|
||||
});
|
||||
|
||||
test(`doesn't allow an empty string`, () => {
|
||||
const result = spaceSchema.validate({
|
||||
...defaultProperties,
|
||||
id: '',
|
||||
});
|
||||
expect(result.error).toMatchInlineSnapshot(
|
||||
`[ValidationError: child "id" fails because ["id" is not allowed to be empty]]`
|
||||
);
|
||||
});
|
||||
|
||||
['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+', ',', '.', '/', '?'].forEach(
|
||||
invalidCharacter => {
|
||||
test(`doesn't allow ${invalidCharacter}`, () => {
|
||||
const result = spaceSchema.validate({
|
||||
...defaultProperties,
|
||||
id: `foo-${invalidCharacter}`,
|
||||
});
|
||||
expect(result.error).toMatchObject({
|
||||
message: `child "id" fails because ["id" with value "foo-${invalidCharacter}" fails to match the lower case, a-z, 0-9, "_", and "-" are allowed pattern]`,
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
|
@ -8,7 +8,7 @@ import Joi from 'joi';
|
|||
import { MAX_SPACE_INITIALS } from '../../common/constants';
|
||||
|
||||
export const spaceSchema = Joi.object({
|
||||
id: Joi.string().regex(/[a-z0-9_\-]*/, `lower case, a-z, 0-9, "_", and "-" are allowed`),
|
||||
id: Joi.string().regex(/^[a-z0-9_\-]+$/, `lower case, a-z, 0-9, "_", and "-" are allowed`),
|
||||
name: Joi.string().required(),
|
||||
description: Joi.string().allow(''),
|
||||
initials: Joi.string().max(MAX_SPACE_INITIALS),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue