align validation with new fetch client (#19071)

This commit is contained in:
Niels Lyngsø
2025-04-17 09:15:54 +02:00
committed by GitHub
parent 59cad092ae
commit a9043a543e
3 changed files with 27 additions and 36 deletions

View File

@@ -239,8 +239,8 @@ export abstract class UmbContentDetailWorkspaceContextBase<
if (missingThis) {
const context = new UmbValidationController(this);
context.inheritFrom(this.validationContext, '$');
context.autoReport();
context.setVariantId(UmbVariantId.Create(variantOption));
context.autoReport();
this.#variantValidationContexts.push(context);
}
});

View File

@@ -9,18 +9,7 @@ import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import type { UmbDataSourceResponse } from '@umbraco-cms/backoffice/repository';
import type { ClassConstructor } from '@umbraco-cms/backoffice/extension-api';
import { UmbId } from '@umbraco-cms/backoffice/id';
/** This should ideally be generated by the server, but we currently don't generate error-model-types. */
interface ValidateErrorResponseBodyModel {
detail: string;
errors: Record<string, Array<string>>;
// TODO: Remove missingProperties, as we do not use it any longer: [NL]
missingProperties: Array<string>;
operationStatus: string;
status: number;
title: string;
type: string;
}
import type { UmbApiError } from '@umbraco-cms/backoffice/resources';
export class UmbServerModelValidatorContext
extends UmbContextBase<UmbServerModelValidatorContext>
@@ -93,11 +82,11 @@ export class UmbServerModelValidatorContext
let messages: Array<UmbValidationMessage> = [];
// We are missing some typing here, but we will just go wild with 'as any': [NL]
const errorBody = (error as any).body as ValidateErrorResponseBodyModel;
const errorBody = (error as UmbApiError).problemDetails;
// Check if there are validation errors, since the error might be a generic ApiError
if (errorBody?.errors) {
Object.keys(errorBody.errors).forEach((path) => {
const newBodies = errorBody.errors[path];
const newBodies = errorBody.errors![path];
// Correct path to ensure it starts with `$.` (notice it mainly starts with `$.`, but the server sometimes does not include it)
if (path.startsWith('$.')) {
// Everything is good.
@@ -112,26 +101,6 @@ export class UmbServerModelValidatorContext
//this.#context!.messages.addMessages('server', path, errorBody.errors[path]);
});
}
// Check if there are missing properties:
if (errorBody?.missingProperties) {
// TODO: Remove missingProperties, as we do not use it any longer: [NL]
throw new Error('Missing properties are not supported.');
/*
// Retrieve the variants of he send data, as those are the once we will declare as missing properties:
// Temporary fix for missing properties, as we currently get one for each variant, but we do not know which variant it is for: [NL]
const uniqueMissingProperties = [...new Set(errorBody.missingProperties)];
uniqueMissingProperties.forEach((alias) => {
this.#data.variants.forEach((variant: any) => {
const path = `$.values[${UmbDataPathPropertyValueQuery({
alias: alias,
culture: variant.culture,
segment: variant.segment,
})}].value`;
this.#context!.messages.addMessages('server', path, [UMB_VALIDATION_EMPTY_LOCALIZATION_KEY]);
});
});
*/
}
if (messages.length > 0) {
const ctrl = new UmbValidationPathTranslationController(this, {

View File

@@ -48,7 +48,7 @@ describe('UmbValidationController', () => {
expect(ctrl.isValid).to.be.false;
});
it('is invalid when holding relevant variant messages', async () => {
it('is invalid when holding relevant culture and segmented variant', async () => {
ctrl.setVariantId(new UmbVariantId('en-us', 'mySegment'));
ctrl.messages.addMessage(
'server',
@@ -59,6 +59,28 @@ describe('UmbValidationController', () => {
expect(ctrl.isValid).to.be.false;
});
it('is invalid when holding relevant culture variant', async () => {
ctrl.setVariantId(new UmbVariantId('en-us', null));
ctrl.messages.addMessage(
'server',
"$.values[?(@.alias == 'my-property' && @.culture == 'en-us' && @.segment == null)].value",
'test',
);
await ctrl.validate().catch(() => undefined);
expect(ctrl.isValid).to.be.false;
});
it('is invalid when holding relevant segmented variant', async () => {
ctrl.setVariantId(new UmbVariantId(null, 'mySegment'));
ctrl.messages.addMessage(
'server',
"$.values[?(@.alias == 'my-property' && @.culture == null && @.segment == 'mySegment')].value",
'test',
);
await ctrl.validate().catch(() => undefined);
expect(ctrl.isValid).to.be.false;
});
it('is valid when holding irrelevant variant messages', async () => {
ctrl.setVariantId(new UmbVariantId('another-lang', 'mySegment'));
ctrl.messages.addMessage(