Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /usr/local/lib/node_modules/strapi/lib/services/__tests__/ drwxr-xr-x | |
| Viewing file: Select action/file-type: 'use strict';
const createEntityValidator = require('../entity-validator');
describe('Entity validator', () => {
describe('General Errors', () => {
it('Throws a badRequest error on invalid input', async () => {
const errors = {
badRequest: jest.fn(),
};
const entityValidator = createEntityValidator({
strapi: {
errors,
},
});
const model = {
attributes: {
title: {
type: 'string',
},
},
};
const input = { title: 1234 };
expect.hasAssertions();
await entityValidator.validateEntity(model, input).catch(() => {
expect(errors.badRequest).toHaveBeenCalledWith('ValidationError', expect.any(Object));
});
});
it('Returns data on valid input', async () => {
const errors = {
badRequest: jest.fn(),
};
const entityValidator = createEntityValidator({
strapi: {
errors,
},
});
const model = {
attributes: {
title: {
type: 'string',
},
},
};
const input = { title: 'test Title' };
expect.hasAssertions();
const data = await entityValidator.validateEntity(model, input);
expect(data).toEqual(input);
});
it('Returns casted data when possible', async () => {
const errors = {
badRequest: jest.fn(),
};
const entityValidator = createEntityValidator({
strapi: {
errors,
},
});
const model = {
attributes: {
title: {
type: 'string',
},
number: {
type: 'integer',
},
},
};
const input = { title: 'Test', number: '123' };
expect.hasAssertions();
const data = await entityValidator.validateEntity(model, input);
expect(data).toEqual({
title: 'Test',
number: 123,
});
});
test('Throws on required not respected', async () => {
const errors = {
badRequest: jest.fn(),
};
const entityValidator = createEntityValidator({
strapi: {
errors,
},
});
const model = {
attributes: {
title: {
type: 'string',
required: true,
},
},
};
expect.hasAssertions();
await entityValidator.validateEntity(model, {}).catch(() => {
expect(errors.badRequest).toHaveBeenCalledWith('ValidationError', {
errors: { title: [expect.stringMatching('must be defined')] },
});
});
await entityValidator.validateEntity(model, { title: null }).catch(() => {
expect(errors.badRequest).toHaveBeenCalledWith('ValidationError', {
errors: { title: [expect.stringMatching('must be defined')] },
});
});
});
});
describe('String validator', () => {
test('Throws on min length not respected', async () => {
const errors = {
badRequest: jest.fn(),
};
const entityValidator = createEntityValidator({
strapi: {
errors,
},
});
const model = {
attributes: {
title: {
type: 'string',
minLength: 10,
},
},
};
const input = { title: 'tooSmall' };
expect.hasAssertions();
await entityValidator.validateEntity(model, input).catch(() => {
expect(errors.badRequest).toHaveBeenCalledWith('ValidationError', {
errors: { title: [expect.stringMatching('at least 10 characters')] },
});
});
});
test('Throws on max length not respected', async () => {
const errors = {
badRequest: jest.fn(),
};
const entityValidator = createEntityValidator({
strapi: {
errors,
},
});
const model = {
attributes: {
title: {
type: 'string',
maxLength: 2,
},
},
};
const input = { title: 'tooSmall' };
expect.hasAssertions();
await entityValidator.validateEntity(model, input).catch(() => {
expect(errors.badRequest).toHaveBeenCalledWith('ValidationError', {
errors: { title: [expect.stringMatching('at most 2 characters')] },
});
});
});
test('Allows empty strings even when required', async () => {
const errors = {
badRequest: jest.fn(),
};
const entityValidator = createEntityValidator({
strapi: {
errors,
},
});
const model = {
attributes: {
title: {
type: 'string',
},
},
};
const input = { title: '' };
expect.hasAssertions();
const data = await entityValidator.validateEntity(model, input);
expect(data).toEqual(input);
});
test('Assign default values', async () => {
const errors = {
badRequest: jest.fn(),
};
const entityValidator = createEntityValidator({
strapi: {
errors,
},
});
const model = {
attributes: {
title: {
type: 'string',
required: true,
default: 'New',
},
type: {
type: 'string',
default: 'test',
},
testDate: {
type: 'date',
required: true,
default: '2020-04-01T04:00:00.000Z',
},
testJSON: {
type: 'date',
required: true,
default: {
foo: 1,
bar: 2,
},
},
},
};
await expect(entityValidator.validateEntity(model, {})).resolves.toMatchObject({
title: 'New',
type: 'test',
testDate: '2020-04-01T04:00:00.000Z',
testJSON: {
foo: 1,
bar: 2,
},
});
});
});
});
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0054 ]-- |