import { Plugin, Record, RecordId, Shape, ts } from '@servicenow/sdk-build-core' export const ViewPlugin = Plugin.create({ name: 'ViewPlugin', records: { sys_ui_view: { coalesce: ['name'], }, }, shapes: [ { shape: Record, inspect(record, { diagnostics, logger }) { if (record.getTable() !== 'sys_ui_view') { return } if (record.get('title').equals('Default view')) { // The default view's name will be empty, so skip the validation return } const viewName = record.get('name') if (!viewName.isString() || !/^[a-zA-Z0-9_,]+$/.test(viewName.getValue())) { if (ts.Node.isNode(viewName.getOriginalSource())) { diagnostics.error(viewName, `View name can only contain alphanumeric characters`) } else { logger.warn( `[ViewPlugin] View name '${viewName.isString() ? viewName.getValue() : ''}' in ${record.getOriginalFilePath()} can only contain alphanumeric characters` ) } } }, }, { shape: RecordId, async commit(id, target, { commit }) { if (id.getTable() !== 'sys_ui_view') { return { success: false } } const viewName = id.getKey('name') if (!viewName) { return { success: false } } await commit(Shape.from(id, viewName).asString(), target) return { success: true } }, }, ], })