From a8f342823aac4f2074d6663ccda04aab0d376f89 Mon Sep 17 00:00:00 2001 From: Devin Hurley Date: Wed, 4 Dec 2024 07:45:48 -0500 Subject: [PATCH] cleanup --- .../schemas/primitives.schema.yaml | 2 -- packages/kbn-zod-helpers/index.ts | 1 + .../kbn-zod-helpers/src/is_valid_date_math.ts | 9 --------- .../kbn-zod-helpers/src/non_empty_string.ts | 18 ++++++++++++++++++ 4 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 packages/kbn-zod-helpers/src/non_empty_string.ts diff --git a/packages/kbn-openapi-common/schemas/primitives.schema.yaml b/packages/kbn-openapi-common/schemas/primitives.schema.yaml index 1244b259fd92b..b6794d01f15b9 100644 --- a/packages/kbn-openapi-common/schemas/primitives.schema.yaml +++ b/packages/kbn-openapi-common/schemas/primitives.schema.yaml @@ -8,9 +8,7 @@ components: schemas: NonEmptyString: type: string - # pattern: ^(?! *$).+$ minLength: 1 - # format: trim format: nonempty description: A string that is not empty and does not contain only whitespace diff --git a/packages/kbn-zod-helpers/index.ts b/packages/kbn-zod-helpers/index.ts index cbd864e327a20..65624b7ec6c80 100644 --- a/packages/kbn-zod-helpers/index.ts +++ b/packages/kbn-zod-helpers/index.ts @@ -16,3 +16,4 @@ export * from './src/required_optional'; export * from './src/safe_parse_result'; export * from './src/stringify_zod_error'; export * from './src/build_route_validation_with_zod'; +export * from './src/non_empty_string'; diff --git a/packages/kbn-zod-helpers/src/is_valid_date_math.ts b/packages/kbn-zod-helpers/src/is_valid_date_math.ts index 04b89eb055ac6..dcb015f921763 100644 --- a/packages/kbn-zod-helpers/src/is_valid_date_math.ts +++ b/packages/kbn-zod-helpers/src/is_valid_date_math.ts @@ -30,12 +30,3 @@ export function isValidDateMath(input: string, ctx: z.RefinementCtx) { }); } } - -export function isNonEmptyString(input: string, ctx: z.RefinementCtx) { - if (typeof input === 'string' && input.trim() === '') { - ctx.addIssue({ - code: z.ZodIssueCode.custom, - message: 'No empty strings allowed', - }); - } -} diff --git a/packages/kbn-zod-helpers/src/non_empty_string.ts b/packages/kbn-zod-helpers/src/non_empty_string.ts new file mode 100644 index 0000000000000..0a02219f3e77b --- /dev/null +++ b/packages/kbn-zod-helpers/src/non_empty_string.ts @@ -0,0 +1,18 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import * as z from '@kbn/zod'; +export function isNonEmptyString(input: string, ctx: z.RefinementCtx) { + if (typeof input === 'string' && input.trim() === '') { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: 'No empty strings allowed', + }); + } +}