Fixing the regex that is used to validate the Space ID (#33716)

* 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:
Brandon Kobel 2019-03-25 11:03:25 -07:00 committed by GitHub
parent dfeb12f502
commit 3f9c36bb25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 1 deletions

View 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]`,
});
});
}
);
});

View file

@ -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),