Skip to content

Useful Utils โ€‹

esw-utils is the shared utility package every Host and Remote already depends on. It collects the small, fiddly helpers you'd otherwise rewrite on every screen โ€” form validation, notifications, list add/update/remove, OData filters, JSON parsing โ€” into one place, tested and consistent.

Reach for these before writing custom code (Rule 13 in Template Rules). If a helper here covers what you need, use it; don't reimplement it.

How to use โ€‹

The helpers are exposed through the project's helper composable at src/composables/use-helper.ts (a Host keeps its shared one under src/composables/shared/use-helper.ts). It's auto-imported โ€” destructure what you need, no import line required:

ts
// the composable exported from src/composables/use-helper.ts
const { validateAllSchema, notif, replacerData } = useHelper()

That composable simply spreads in esw-utils's useUtils(). To pull from the source directly instead:

ts
import { useUtils } from 'esw-utils'

const { validateSchema, filterOrIn } = useUtils()

Notifications โ€‹

HelperSignatureDescription
notifnotif(options): Promise<void>One unified notification for success / info / warning / error / promise. Optional routing + redirect; non-error toasts auto-dismiss (~3s).

Schema validation (Yup) โ€‹

HelperSignatureDescription
validateAllSchemavalidateAllSchema({ schema, input, error }, cb?): Promise<boolean>Validate a whole form object against a Yup schema (nested objects supported). Fills the error object with per-field messages + validity.
validateSchemavalidateSchema({ schema, field, input, error }, cb?): Promise<boolean>Validate a single field. Updates that field's message + valid flag in the error object.
validateAllSchemaCheckvalidateAllSchemaCheck(error): booleanQuick check โ€” true if any field in the error object is currently invalid.
clearSchemaValidationclearSchemaValidation({ error })Recursively reset all errors โ€” valid = true, message = ''.

Data manipulation & filtering โ€‹

HelperSignatureDescription
replacerDatareplacerData({ fn, type, item, key, items })Add / update / remove a row in an array or a DevExtreme DataSource. type picks the target โ€” data (array) or datasource; fn is the op โ€” push (prepend), replace (upsert), or remove. Strips @odata.context / @odata.url metadata on the datasource path.
filterOrInfilterOrIn(field, values, combine?)Build an OData filter from a list of values โ€” compact [field, 'in', [...]], or a chained (field = a) or (field = b) form. Returns null when there are no values.
dxFilterToStringdxFilterToString({ filter, encode? })Convert a DevExtreme filter object to an OData v4 $filter string. Handles and/or, comparisons (eq/ne/gtโ€ฆ), and contains/startswith/endswith. Optional URL encoding.

DevExtreme UI helpers โ€‹

HelperSignatureDescription
preventMinuspreventMinus()Block the minus sign on numeric fields โ€” ASCII - and Unicode โˆ’, via keydown and paste. Returns { onKeyDown, onPaste }.
numColTemplatenumColTemplate()Auto-numbering column for DataGrids โ€” row numbers that respect pagination, with sort/filter/edit/export disabled.

JSON & misc utilities โ€‹

HelperSignatureDescription
isDateisDate(value): value is DateType guard โ€” true for a valid Date or a parseable date string/number.
isJSONStringisJSONString({ input, strict?, root? })Check whether a string is valid JSON. Optional strict/lenient mode and a root filter (array / object / primitive).
safeJSONParsesafeJSONParse(str): anyParse JSON with fallbacks for double-wrapped strings and error arrays like [System.Exception: {...}]. Returns null on failure.

Fetching lives elsewhere

esw-utils also has fetching / datasource-building helpers, but in these template apps data access goes through mono-utils/fetching โ€” see Data Fetching and DataSource. This page only covers the value-add helpers above; see Template Rules (Rule 13) for the when-to-use-them rule.