reject bad validations + server validation impl

This commit is contained in:
Niels Lyngsø
2024-04-09 23:04:45 +02:00
parent f3ac2e0971
commit 22bd7fa195
8 changed files with 41 additions and 34 deletions

View File

@@ -1,4 +1,4 @@
import type { UmbServerModelValidationContext } from './server-model-validation.context.js';
import type { UmbServerModelValidationContext } from './index.js';
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
export const UMB_SERVER_MODEL_VALIDATION_CONTEXT = new UmbContextToken<UmbServerModelValidationContext>(

View File

@@ -4,7 +4,6 @@ import { UMB_VALIDATION_CONTEXT } from './validation.context-token.js';
import { UMB_SERVER_MODEL_VALIDATION_CONTEXT } from './server-model-validation.context-token.js';
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
export class UmbServerModelValidationContext
extends UmbContextBase<UmbServerModelValidationContext>
@@ -12,7 +11,6 @@ export class UmbServerModelValidationContext
{
#validatePromise?: Promise<boolean>;
#validatePromiseResolve?: (valid: boolean) => void;
#validatePromiseReject?: () => void;
#context?: typeof UMB_VALIDATION_CONTEXT.TYPE;
#isValid = true;
@@ -35,24 +33,25 @@ export class UmbServerModelValidationContext
});
}
async askServerForValidation(requestPromise: Promise<{ data: string | undefined }>): Promise<void> {
async askServerForValidation(requestPromise: Promise<{ data: string | undefined; error: any }>): Promise<void> {
this.#context?.messages.removeMessagesByType('server');
this.#validatePromiseReject?.();
this.#validatePromise = new Promise<boolean>((resolve, reject) => {
this.#validatePromiseResolve = resolve;
this.#validatePromiseReject = reject;
});
this.#serverFeedback = {};
this.#isValid = false;
//this.#validatePromiseReject?.();
this.#validatePromise = new Promise<boolean>((resolve) => {
this.#validatePromiseResolve = resolve;
});
// Ask the server for validation...
const { data } = await tryExecuteAndNotify(this, requestPromise);
const { data, error } = await requestPromise;
console.log('VALIDATE — Got server response:');
console.log(data);
console.log(data, error);
this.#validatePromiseResolve?.(true);
this.#isValid = false;
this.#validatePromiseResolve?.(false);
this.#validatePromiseResolve = undefined;
this.#validatePromiseReject = undefined;
//this.#validatePromise = undefined;
}
addTranslator(translator: UmbValidationMessageTranslator): void {
@@ -71,10 +70,11 @@ export class UmbServerModelValidationContext
get isValid(): boolean {
return this.#isValid;
}
validate(): Promise<boolean> {
// TODO: Return to this decision once we have a bit more implementation to perspectives against: [NL]
// If we dont have a validatePromise, we valid cause then no one has called askServerForValidation(). [NL] (I might change my mind about this one, to then say we are invalid unless we have been validated by the server... )
return this.#validatePromise ?? Promise.resolve(true);
async validate(): Promise<void> {
if (this.#validatePromise) {
await this.#validatePromise;
}
return this.#isValid ? Promise.resolve() : Promise.reject();
}
reset(): void {}

View File

@@ -63,23 +63,27 @@ export class UmbValidationContext extends UmbContextBase<UmbValidationContext> i
*
* @returns succeed {Promise<boolean>} - Returns a promise that resolves to true if the validator succeeded, this depends on the validators and wether forceSucceed is set.
*/
async validate(): Promise<boolean> {
async validate(): Promise<void> {
// TODO: clear server messages here?, well maybe only if we know we will get new server messages? Do the server messages hook into the system like another validator?
this.#validationMode = true;
const results = await Promise.all(this.#validators.map((v) => v.validate()));
const resultsStatus = await Promise.all(this.#validators.map((v) => v.validate())).then(
() => Promise.resolve(true),
() => Promise.reject(false),
);
// If we have any messages then we are not valid, otherwise lets check the validation results: [NL]
// This enables us to keep client validations though UI is not present anymore — because the client validations got defined as messages. [NL]
const isValid = this.messages.getHasAnyMessages() ? false : results.every((r) => r);
const isValid = this.messages.getHasAnyMessages() ? false : resultsStatus;
this.#isValid = isValid;
// Focus first invalid element:
if (!isValid) {
if (isValid === false) {
// Focus first invalid element:
this.focusFirstInvalidElement();
return Promise.reject();
}
//return this.#preventFail ? true : isValid;
return isValid;
return Promise.resolve();
}
focusFirstInvalidElement(): void {

View File

@@ -86,9 +86,9 @@ export class UmbBindValidationMessageToFormControl extends UmbControllerBase {
this.#control.checkValidity();
}
validate(): Promise<boolean> {
validate(): Promise<void> {
//this.#isValid = this.#control.checkValidity();
return Promise.resolve(this.#isValid);
return this.#isValid ? Promise.resolve() : Promise.reject();
}
/**

View File

@@ -56,9 +56,9 @@ export class UmbFormControlValidator extends UmbControllerBase implements UmbVal
#setInvalid = this.#setIsValid.bind(this, false);
#setValid = this.#setIsValid.bind(this, true);
validate(): Promise<boolean> {
validate(): Promise<void> {
this.#isValid = this.#control.checkValidity();
return Promise.resolve(this.#isValid);
return this.#isValid ? Promise.resolve() : Promise.reject();
}
/**

View File

@@ -7,7 +7,7 @@ export interface UmbValidator extends EventTarget {
/**
* Validate the form, will return a promise that resolves to true if what the Validator represents is valid.
*/
validate(): Promise<boolean>;
validate(): Promise<void>;
/**
* Reset the validator to its initial state.

View File

@@ -5,7 +5,7 @@ import {
type UpdateDocumentRequestModel,
} from '@umbraco-cms/backoffice/external/backend-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
import { tryExecute } from '@umbraco-cms/backoffice/resources';
/**
* A server data source for Document Validation
@@ -44,8 +44,9 @@ export class UmbDocumentValidationServerDataSource {
variants: model.variants,
};
const { data, error } = await tryExecuteAndNotify(
this.#host,
// Maybe use: tryExecuteAndNotify
const { data, error } = await tryExecute(
//this.#host,
DocumentResource.postDocumentValidate({
requestBody,
}),
@@ -73,8 +74,9 @@ export class UmbDocumentValidationServerDataSource {
variants: model.variants,
};
const { data, error } = await tryExecuteAndNotify(
this.#host,
// Maybe use: tryExecuteAndNotify
const { data, error } = await tryExecute(
//this.#host,
DocumentResource.putDocumentByIdValidate({
id: model.unique,
requestBody,

View File

@@ -581,6 +581,7 @@ export class UmbDocumentWorkspaceContext
const saveData = this.#buildSaveData(variantIds);
// Create the validation repository if it does not exist. (we first create this here when we need it) [NL]
this.#validationRepository ??= new UmbDocumentValidationRepository(this);
if (this.getIsNew()) {