refactor query + translators

This commit is contained in:
Niels Lyngsø
2024-08-16 14:18:02 +02:00
parent 7f0a0133bb
commit 023cf84dea
15 changed files with 39 additions and 59 deletions

View File

@@ -11,7 +11,7 @@ import { stringOrStringArrayContains } from '@umbraco-cms/backoffice/utils';
import { UmbBlockListEntryContext } from '../../context/block-list-entry.context.js';
import { UMB_BLOCK_LIST, type UmbBlockListLayoutModel } from '../../types.js';
import { UmbObserveValidationStateController } from '@umbraco-cms/backoffice/validation';
import { UmbDataPathBlockElementDataFilter } from '@umbraco-cms/backoffice/block';
import { UmbDataPathBlockElementDataQuery } from '@umbraco-cms/backoffice/block';
/**
* @element umb-block-list-entry
@@ -38,7 +38,7 @@ export class UmbBlockListEntryElement extends UmbLitElement implements UmbProper
new UmbObserveValidationStateController(
this,
`$.contentData[${UmbDataPathBlockElementDataFilter({ udi: value })}]`,
`$.contentData[${UmbDataPathBlockElementDataQuery({ udi: value })}]`,
(hasMessages) => {
this._contentInvalid = hasMessages;
this._blockViewProps.contentInvalid = hasMessages;
@@ -167,7 +167,7 @@ export class UmbBlockListEntryElement extends UmbLitElement implements UmbProper
// Observe settings validation state:
new UmbObserveValidationStateController(
this,
`$.settingsData[${UmbDataPathBlockElementDataFilter(settings)}]`,
`$.settingsData[${UmbDataPathBlockElementDataQuery(settings)}]`,
(hasMessages) => {
this._settingsInvalid = hasMessages;
this._blockViewProps.settingsInvalid = hasMessages;

View File

@@ -1,7 +1,7 @@
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import {
GetPropertyNameFromPath,
UmbDataPathPropertyValueFilter,
UmbDataPathPropertyValueQuery,
UmbValidationPathTranslatorBase,
} from '@umbraco-cms/backoffice/validation';
@@ -23,6 +23,6 @@ export class UmbBlockElementDataValidationPathTranslator extends UmbValidationPa
const specificValue = { alias: key };
// replace the values[ number ] with JSON-Path filter values[@.(...)], continues by the rest of the path:
//return '$.values' + UmbVariantValuesValidationPathTranslator(specificValue) + path.substring(path.indexOf(']'));
return '$.values[' + UmbDataPathPropertyValueFilter(specificValue) + '.value';
return '$.values[' + UmbDataPathPropertyValueQuery(specificValue) + '.value';
}
}

View File

@@ -1,46 +1,23 @@
import { UmbDataPathBlockElementDataFilter } from './data-path-element-data-filter.function.js';
import { UmbDataPathBlockElementDataQuery } from './data-path-element-data-query.function.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbValidationPathTranslatorBase } from '@umbraco-cms/backoffice/validation';
import { UmbAbstractArrayValidationPathTranslator } from '@umbraco-cms/backoffice/validation';
export class UmbBlockElementDataValidationPathTranslator extends UmbValidationPathTranslatorBase {
export class UmbBlockElementDataValidationPathTranslator extends UmbAbstractArrayValidationPathTranslator {
#propertyName: string;
#pathStart: string;
constructor(host: UmbControllerHost, propertyName: 'contentData' | 'settingsData') {
super(host);
super(host, '$.' + propertyName + '[', UmbDataPathBlockElementDataQuery);
this.#propertyName = propertyName;
this.#pathStart = '$.' + propertyName + '[';
}
translate(path: string) {
getDataFromIndex(index: number) {
if (!this._context) return;
if (path.indexOf(this.#pathStart) !== 0) {
// We do not handle this path.
return false;
}
const startLength = this.#pathStart.length;
const pathEnd = path.indexOf(']', startLength);
if (pathEnd === -1) {
// We do not handle this path.
return false;
}
// retrieve the number from the message values index: [NL]
const index = parseInt(path.substring(startLength, pathEnd));
if (isNaN(index)) {
// index is not a number, this means its not a path we want to translate. [NL]
return false;
}
// Get the data from the validation request, the context holds that for us: [NL]
const data = this._context.getTranslationData();
const specificValue = data[this.#propertyName][index];
if (!specificValue || !specificValue.udi) {
const entry = data[this.#propertyName][index];
if (!entry || !entry.udi) {
console.log('block did not have UDI', this.#propertyName, index, data);
return false;
}
// replace the values[ number ] with JSON-Path filter values[@.(...)], continues by the rest of the path:
return this.#pathStart + UmbDataPathBlockElementDataFilter(specificValue) + path.substring(path.indexOf(']'));
return entry;
}
}

View File

@@ -1,13 +1,13 @@
import type { UmbBlockDataType } from '../types.js';
/**
* Validation Data Path filter for Block Element Data.
* Validation Data Path Query generator for Block Element Data.
* write a JSON-Path filter similar to `?(@.udi = 'my-udi://1234')`
* @param udi {string} - The udi of the block Element data.
* @param data {{udi: string}} - A data object with the udi property.
* @returns
*/
export function UmbDataPathBlockElementDataFilter(data: Pick<UmbBlockDataType, 'udi'>): string {
export function UmbDataPathBlockElementDataQuery(data: Pick<UmbBlockDataType, 'udi'>): string {
// write a array of strings for each property, where alias must be present and culture and segment are optional
//const filters: Array<string> = [`@.udi = '${udi}'`];
//return `?(${filters.join(' && ')})`;

View File

@@ -1,2 +1,2 @@
export * from './block-data-validation-path-translator.controller.js';
export * from './data-path-element-data-filter.function.js';
export * from './data-path-element-data-query.function.js';

View File

@@ -7,7 +7,7 @@ import type {
} from '@umbraco-cms/backoffice/content-type';
import { UmbContentTypePropertyStructureHelper } from '@umbraco-cms/backoffice/content-type';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { UmbDataPathPropertyValueFilter } from '@umbraco-cms/backoffice/validation';
import { UmbDataPathPropertyValueQuery } from '@umbraco-cms/backoffice/validation';
import { UMB_PROPERTY_STRUCTURE_WORKSPACE_CONTEXT } from '@umbraco-cms/backoffice/workspace';
import type { UmbVariantId } from '@umbraco-cms/backoffice/variant';
import { UMB_PROPERTY_DATASET_CONTEXT } from '@umbraco-cms/backoffice/property';
@@ -58,7 +58,7 @@ export class UmbContentWorkspaceViewEditPropertiesElement extends UmbLitElement
if (!this.#variantId || !this._propertyStructure) return;
this._dataPaths = this._propertyStructure.map(
(property) =>
`$.values[${UmbDataPathPropertyValueFilter({
`$.values[${UmbDataPathPropertyValueQuery({
alias: property.alias,
culture: property.variesByCulture ? this.#variantId!.culture : null,
segment: property.variesBySegment ? this.#variantId!.segment : null,

View File

@@ -1,5 +1,5 @@
import type { UmbValidator } from '../interfaces/validator.interface.js';
import { UmbDataPathPropertyValueFilter } from '../utils/index.js';
import { UmbDataPathPropertyValueQuery } from '../utils/index.js';
import { UMB_VALIDATION_CONTEXT } from '../context/validation.context-token.js';
import { UMB_VALIDATION_EMPTY_LOCALIZATION_KEY } from '../const.js';
import { UMB_SERVER_MODEL_VALIDATOR_CONTEXT } from './server-model-validator.context-token.js';
@@ -87,7 +87,7 @@ export class UmbServerModelValidatorContext
const uniqueMissingProperties = [...new Set(errorBody.missingProperties)];
uniqueMissingProperties.forEach((alias) => {
this.#data.variants.forEach((variant: any) => {
const path = `$.values[${UmbDataPathPropertyValueFilter({
const path = `$.values[${UmbDataPathPropertyValueQuery({
alias: alias,
culture: variant.culture,
segment: variant.segment,

View File

@@ -1,14 +1,15 @@
import { UmbDataPathVariantFilter } from '../utils/index.js';
import { UmbValidationPathTranslatorBase } from './validation-path-translator-base.controller.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
export abstract class UmbAbstractArrayValidationPathTranslator extends UmbValidationPathTranslatorBase {
#initialPathToMatch: string;
#queryMethod: (data: unknown) => string;
constructor(host: UmbControllerHost, initialPathToMatch: string) {
constructor(host: UmbControllerHost, initialPathToMatch: string, queryMethod: (data: any) => string) {
super(host);
this.#initialPathToMatch = initialPathToMatch;
this.#queryMethod = queryMethod;
}
translate(path: string) {
if (!this._context) return;
@@ -34,7 +35,7 @@ export abstract class UmbAbstractArrayValidationPathTranslator extends UmbValida
if (!data) return false;
// replace the values[ number ] with JSON-Path filter values[@.(...)], continues by the rest of the path:
return this.#initialPathToMatch + UmbDataPathVariantFilter(data as any) + path.substring(path.indexOf(']'));
return this.#initialPathToMatch + this.#queryMethod(data) + path.substring(path.indexOf(']'));
}
abstract getDataFromIndex(index: number): unknown | undefined;

View File

@@ -1,9 +1,10 @@
import { UmbDataPathPropertyValueQuery } from '../utils/data-path-property-value-query.function.js';
import { UmbAbstractArrayValidationPathTranslator } from './abstract-array-path-translator.controller.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
export class UmbVariantValuesValidationPathTranslator extends UmbAbstractArrayValidationPathTranslator {
constructor(host: UmbControllerHost) {
super(host, '$.values[');
super(host, '$.values[', UmbDataPathPropertyValueQuery);
}
getDataFromIndex(index: number) {

View File

@@ -1,9 +1,10 @@
import { UmbDataPathVariantQuery } from '../utils/data-path-variant-query.function.js';
import { UmbAbstractArrayValidationPathTranslator } from './abstract-array-path-translator.controller.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
export class UmbVariantValuesValidationPathTranslator extends UmbAbstractArrayValidationPathTranslator {
export class UmbVariantsValidationPathTranslator extends UmbAbstractArrayValidationPathTranslator {
constructor(host: UmbControllerHost) {
super(host, '$.variants[');
super(host, '$.variants[', UmbDataPathVariantQuery);
}
getDataFromIndex(index: number) {

View File

@@ -2,13 +2,13 @@ import type { UmbPartialSome } from '@umbraco-cms/backoffice/utils';
import type { UmbVariantPropertyValueModel } from '@umbraco-cms/backoffice/variant';
/**
* Validation Data Path filter for Property Value.
* Validation Data Path Query generator for Property Value.
* write a JSON-Path filter similar to `?(@.alias = 'myAlias' && @.culture == 'en-us' && @.segment == 'mySegment')`
* where culture and segment are optional
* @param value
* @returns
*/
export function UmbDataPathPropertyValueFilter(
export function UmbDataPathPropertyValueQuery(
value: UmbPartialSome<Omit<UmbVariantPropertyValueModel, 'value'>, 'culture' | 'segment'>,
): string {
// write a array of strings for each property, where alias must be present and culture and segment are optional

View File

@@ -2,13 +2,13 @@ import type { UmbPartialSome } from '@umbraco-cms/backoffice/utils';
import type { UmbVariantPropertyValueModel } from '@umbraco-cms/backoffice/variant';
/**
* Validation Data Path filter for Variant.
* Validation Data Path query generator for Variant.
* write a JSON-Path filter similar to `?(@.culture == 'en-us' && @.segment == 'mySegment')`
* where segment are optional.
* @param value
* @returns
*/
export function UmbDataPathVariantFilter(
export function UmbDataPathVariantQuery(
value: UmbPartialSome<Pick<UmbVariantPropertyValueModel, 'culture' | 'segment'>, 'segment'>,
): string {
// write a array of strings for each property, where culture must be present and segment is optional

View File

@@ -1,3 +1,3 @@
export * from './data-path-property-value-filter.function.js';
export * from './data-path-variant-filter.function.js';
export * from './data-path-property-value-query.function.js';
export * from './data-path-variant-query.function.js';
export * from './json-path.function.js';

View File

@@ -12,7 +12,7 @@ import { UmbVariantId } from '@umbraco-cms/backoffice/variant';
import { UMB_PROPERTY_DATASET_CONTEXT, isNameablePropertyDatasetContext } from '@umbraco-cms/backoffice/property';
import { UmbLitElement, umbFocus } from '@umbraco-cms/backoffice/lit-element';
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
import { UmbDataPathVariantFilter, umbBindToValidation } from '@umbraco-cms/backoffice/validation';
import { UmbDataPathVariantQuery, umbBindToValidation } from '@umbraco-cms/backoffice/validation';
type UmbDocumentVariantOption = {
culture: string | null;
@@ -221,7 +221,7 @@ export class UmbWorkspaceSplitViewVariantSelectorElement extends UmbLitElement {
.value=${this._name ?? ''}
@input=${this.#handleInput}
required
${umbBindToValidation(this, `$.variants[${UmbDataPathVariantFilter(this._variantId)}].name`, this._name ?? '')}
${umbBindToValidation(this, `$.variants[${UmbDataPathVariantQuery(this._variantId)}].name`, this._name ?? '')}
${umbFocus()}
>
${

View File

@@ -3,7 +3,7 @@ import { html, customElement, state, ifDefined, repeat } from '@umbraco-cms/back
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
import type { PropertyEditorSettingsProperty } from '@umbraco-cms/backoffice/extension-registry';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { UmbDataPathPropertyValueFilter } from '@umbraco-cms/backoffice/validation';
import { UmbDataPathPropertyValueQuery } from '@umbraco-cms/backoffice/validation';
/**
* @element umb-property-editor-config
@@ -46,7 +46,7 @@ export class UmbPropertyEditorConfigElement extends UmbLitElement {
(property) => property.alias,
(property) =>
html`<umb-property
data-path="$.values[${UmbDataPathPropertyValueFilter(property)}].value"
data-path="$.values[${UmbDataPathPropertyValueQuery(property)}].value"
label=${property.label}
description=${ifDefined(property.description)}
alias=${property.alias}