@strapi/utils
Advanced tools
Comparing version 4.0.0-beta.10 to 4.0.0-beta.11
@@ -11,3 +11,4 @@ 'use strict'; | ||
const templateConfiguration = require('./template-configuration'); | ||
const { yup, formatYupErrors } = require('./validators'); | ||
const { yup, handleYupError, validateYupSchema, validateYupSchemaSync } = require('./validators'); | ||
const errors = require('./errors'); | ||
const { | ||
@@ -39,3 +40,3 @@ nameToSlug, | ||
yup, | ||
formatYupErrors, | ||
handleYupError, | ||
policy, | ||
@@ -70,2 +71,5 @@ templateConfiguration, | ||
pagination, | ||
errors, | ||
validateYupSchema, | ||
validateYupSchemaSync, | ||
}; |
'use strict'; | ||
const { merge, pipe, omit, isNil } = require('lodash/fp'); | ||
const { PaginationError } = require('./errors'); | ||
@@ -55,3 +56,3 @@ const STRAPI_DEFAULTS = { | ||
if (usePagePagination && useOffsetPagination) { | ||
throw new Error('Cannot use both page & offset pagination in the same query'); | ||
throw new PaginationError('Cannot use both page & offset pagination in the same query'); | ||
} | ||
@@ -58,0 +59,0 @@ |
@@ -13,3 +13,3 @@ 'use strict'; | ||
if (!body.data) { | ||
throw strapi.errors.badRequest( | ||
return ctx.badRequest( | ||
`When using multipart/form-data you need to provide your data in a JSON 'data' field.` | ||
@@ -23,3 +23,3 @@ ); | ||
} catch (error) { | ||
throw strapi.errors.badRequest(`Invalid 'data' field. 'data' should be a valid JSON.`); | ||
return ctx.badRequest(`Invalid 'data' field. 'data' should be a valid JSON.`); | ||
} | ||
@@ -31,3 +31,3 @@ | ||
if (fullPath.length <= 1 || fullPath[0] !== 'files') { | ||
throw strapi.errors.badRequest( | ||
return ctx.badRequest( | ||
`When using multipart/form-data you need to provide your files by prefixing them with the 'files'. | ||
@@ -34,0 +34,0 @@ For example, when a media file is named "avatar", make sure the form key name is "files.avatar"` |
@@ -5,3 +5,6 @@ 'use strict'; | ||
const _ = require('lodash'); | ||
const { defaults } = require('lodash/fp'); | ||
const utils = require('./string-formatting'); | ||
const { YupValidationError } = require('./errors'); | ||
const printValue = require('./print-value'); | ||
@@ -60,25 +63,51 @@ const MixedSchemaType = yup.mixed; | ||
/** | ||
* Returns a formatted error for http responses | ||
* @param {Object} validationError - a Yup ValidationError | ||
*/ | ||
const formatYupErrors = validationError => { | ||
if (!validationError.inner) { | ||
throw new Error('invalid.input'); | ||
const handleYupError = (error, errorMessage) => { | ||
throw new YupValidationError(error, errorMessage); | ||
}; | ||
const defaultValidationParam = { strict: true, abortEarly: false }; | ||
const validateYupSchema = (schema, options = {}) => async (body, errorMessage) => { | ||
try { | ||
const optionsWithDefaults = defaults(defaultValidationParam, options); | ||
return await schema.validate(body, optionsWithDefaults); | ||
} catch (e) { | ||
handleYupError(e, errorMessage); | ||
} | ||
}; | ||
if (validationError.inner.length === 0) { | ||
if (validationError.path === undefined) return validationError.errors; | ||
return { [validationError.path]: validationError.errors }; | ||
const validateYupSchemaSync = (schema, options = {}) => (body, errorMessage) => { | ||
try { | ||
const optionsWithDefaults = defaults(defaultValidationParam, options); | ||
return schema.validateSync(body, optionsWithDefaults); | ||
} catch (e) { | ||
handleYupError(e, errorMessage); | ||
} | ||
return validationError.inner.reduce((acc, err) => { | ||
acc[err.path] = err.errors; | ||
return acc; | ||
}, {}); | ||
}; | ||
// Temporary fix of this issue : https://github.com/jquense/yup/issues/616 | ||
yup.setLocale({ | ||
mixed: { | ||
notType({ path, type, value, originalValue }) { | ||
let isCast = originalValue != null && originalValue !== value; | ||
let msg = | ||
`${path} must be a \`${type}\` type, ` + | ||
`but the final value was: \`${printValue(value, true)}\`` + | ||
(isCast ? ` (cast from the value \`${printValue(originalValue, true)}\`).` : '.'); | ||
/* Remove comment that is not supposed to be seen by the enduser | ||
if (value === null) { | ||
msg += `\n If "null" is intended as an empty value be sure to mark the schema as \`.nullable()\``; | ||
} | ||
*/ | ||
return msg; | ||
}, | ||
}, | ||
}); | ||
module.exports = { | ||
yup, | ||
formatYupErrors, | ||
handleYupError, | ||
validateYupSchema, | ||
validateYupSchemaSync, | ||
}; |
{ | ||
"name": "@strapi/utils", | ||
"version": "4.0.0-beta.10", | ||
"version": "4.0.0-beta.11", | ||
"description": "Shared utilities for the Strapi packages", | ||
@@ -17,2 +17,3 @@ "homepage": "https://strapi.io", | ||
"date-fns": "2.24.0", | ||
"http-errors": "1.8.0", | ||
"lodash": "4.17.21", | ||
@@ -45,3 +46,3 @@ "yup": "0.32.9" | ||
"license": "SEE LICENSE IN LICENSE", | ||
"gitHead": "6d23a0e2da28a45c69589516ed205f2bf3e6486f" | ||
"gitHead": "5b04fa152dc597fc3aa80afb25796217b60403f3" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60802
35
1821
5
2
+ Addedhttp-errors@1.8.0
+ Addeddepd@1.1.2(transitive)
+ Addedhttp-errors@1.8.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedstatuses@1.5.0(transitive)
+ Addedtoidentifier@1.0.0(transitive)