Skip to content

Commit

Permalink
feat: moved allowedResource errors out of field namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
BasKiers committed Jun 21, 2023
1 parent c09c7ec commit 19b2be8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
10 changes: 5 additions & 5 deletions docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ author
])
```

## field.allowedResources.DUPLICATE_SOURCE
## allowedResources.DUPLICATE_SOURCE

When trying to set the same space multiple times in one `allowedResources` property.

Expand All @@ -527,7 +527,7 @@ author
])
```

## field.allowedResources.INVALID_RESOURCE
## allowedResources.INVALID_RESOURCE

When trying to set an allowed resource that is not an object.

Expand All @@ -541,7 +541,7 @@ author
.allowedResources(['crn:contentful:::content:spaces/books'])
```

## field.allowedResources.INVALID_RESOURCE_PROPERTY
## allowedResources.INVALID_RESOURCE_PROPERTY

When trying to set an allowed resource where a property of an allowed resource is missing, unexpected, or has the wrong format.

Expand All @@ -560,7 +560,7 @@ author
])
```

## field.allowedResources.TOO_FEW_ITEMS
## allowedResources.TOO_FEW_ITEMS

When trying to set an empty list of allowed resources.

Expand All @@ -570,7 +570,7 @@ When trying to set an empty list of allowed resources.
author.createField('works').name('Works').type('ResourceLink').allowedResources([])
```

## field.allowedResources.TOO_MANY_ITEMS
## allowedResources.TOO_MANY_ITEMS

When trying to set more than 3 allowed resources on a field.

Expand Down
34 changes: 17 additions & 17 deletions src/lib/offline-api/validator/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,6 @@ const errors = {
return `"${propName}" validation expected to be "${expectedType}", but got "${actualType}"`
}
},
allowedResources: {
DUPLICATE_SOURCE: (fieldId, source, prop) => {
return `The property "${prop}" on the field "${fieldId}" contains duplicate source "${source}".`
},
INVALID_RESOURCE: (fieldId, index, actualType) => {
return `Allowed resource at index ${index} on the field "${fieldId}" expected to be "object", but got "${actualType}".`
},
INVALID_RESOURCE_PROPERTY: (fieldId, index, error) => {
return `Allowed resource at index ${index} on the field "${fieldId}" has an invalid property: ${error}.`
},
TOO_FEW_ITEMS: (fieldId, prop) => {
return `The property "${prop}" on the field "${fieldId}" must not be empty.`
},
TOO_MANY_ITEMS: (fieldId, prop) => {
return `The property "${prop}" on the field "${fieldId}" must have at most ${MAX_ALLOWED_RESOURCES} items.`
}
},
defaultValue: {
TYPE_MISMATCH: (fieldId, valueType, locale, fieldType) => {
return `Cannot set default value of type "${valueType}" for locale "${locale}" on field "${fieldId}". The default value must match the field type "${fieldType}".`
Expand Down Expand Up @@ -141,6 +124,23 @@ const errors = {
REQUIRED_PROPERTY: (path) => {
return `The property "${path}" is required on a tag.`
}
},
allowedResources: {
DUPLICATE_SOURCE: (fieldId, source, prop) => {
return `The property "${prop}" on the field "${fieldId}" contains duplicate source "${source}".`
},
INVALID_RESOURCE: (fieldId, index, actualType) => {
return `Allowed resource at index ${index} on the field "${fieldId}" expected to be "object", but got "${actualType}".`
},
INVALID_RESOURCE_PROPERTY: (fieldId, index, error) => {
return `Allowed resource at index ${index} on the field "${fieldId}" has an invalid property: ${error}.`
},
TOO_FEW_ITEMS: (fieldId, prop) => {
return `The property "${prop}" on the field "${fieldId}" must not be empty.`
},
TOO_MANY_ITEMS: (fieldId, prop) => {
return `The property "${prop}" on the field "${fieldId}" must have at most ${MAX_ALLOWED_RESOURCES} items.`
}
}
}

Expand Down
22 changes: 9 additions & 13 deletions src/lib/offline-api/validator/schema/schema-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ const validateFields = function (
const error = details.message.replace(/".+?"/, `"${context.key}"`)
return {
type: 'InvalidPayload',
message: errorMessages.field.allowedResources.INVALID_RESOURCE_PROPERTY(
message: errorMessages.allowedResources.INVALID_RESOURCE_PROPERTY(
field.id,
path[2],
error
Expand All @@ -319,7 +319,7 @@ const validateFields = function (

return {
type: 'InvalidPayload',
message: errorMessages.field.allowedResources.INVALID_RESOURCE(
message: errorMessages.allowedResources.INVALID_RESOURCE(
field.id,
context.key,
actualType
Expand All @@ -330,21 +330,21 @@ const validateFields = function (
if (type === 'array.min') {
return {
type: 'InvalidPayload',
message: errorMessages.field.allowedResources.TOO_FEW_ITEMS(field.id, 'allowedResources')
message: errorMessages.allowedResources.TOO_FEW_ITEMS(field.id, 'allowedResources')
}
}

if (type === 'array.max') {
return {
type: 'InvalidPayload',
message: errorMessages.field.allowedResources.TOO_MANY_ITEMS(field.id, 'allowedResources')
message: errorMessages.allowedResources.TOO_MANY_ITEMS(field.id, 'allowedResources')
}
}

if (type === 'array.unique') {
return {
type: 'InvalidPayload',
message: errorMessages.field.allowedResources.DUPLICATE_SOURCE(
message: errorMessages.allowedResources.DUPLICATE_SOURCE(
field.id,
context.value.source,
'allowedResources'
Expand All @@ -355,21 +355,21 @@ const validateFields = function (
if (type === 'array.min') {
return {
type: 'InvalidPayload',
message: errorMessages.field.allowedResources.TOO_FEW_ITEMS(field.id, prop)
message: errorMessages.allowedResources.TOO_FEW_ITEMS(field.id, prop)
}
}

if (type === 'array.max') {
return {
type: 'InvalidPayload',
message: errorMessages.field.allowedResources.TOO_MANY_ITEMS(field.id, prop)
message: errorMessages.allowedResources.TOO_MANY_ITEMS(field.id, prop)
}
}

if (type === 'array.unique') {
return {
type: 'InvalidPayload',
message: errorMessages.field.allowedResources.DUPLICATE_SOURCE(
message: errorMessages.allowedResources.DUPLICATE_SOURCE(
field.id,
context.value.source,
prop
Expand All @@ -383,11 +383,7 @@ const validateFields = function (

return {
type: 'InvalidPayload',
message: errorMessages.field.allowedResources.INVALID_RESOURCE_PROPERTY(
field.id,
path[6],
error
)
message: errorMessages.allowedResources.INVALID_RESOURCE_PROPERTY(field.id, path[6], error)
}
}

Expand Down

0 comments on commit 19b2be8

Please sign in to comment.