Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions oas_docs/output/kibana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34062,9 +34062,9 @@ components:
- assistant
type: string
Security_AI_Assistant_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_AI_Assistant_API_NormalizedAnonymizationFieldError:
type: object
Expand Down Expand Up @@ -37008,9 +37008,9 @@ components:
- severity
- $ref: '#/components/schemas/Security_Detections_API_NewTermsRuleCreateFields'
Security_Detections_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Detections_API_NormalizedRuleAction:
additionalProperties: false
Expand Down Expand Up @@ -40219,9 +40219,9 @@ components:
- text
type: string
Security_Endpoint_Exceptions_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Endpoint_Exceptions_API_PlatformErrorResponse:
type: object
Expand Down Expand Up @@ -40533,9 +40533,9 @@ components:
required:
- hostStatuses
Security_Endpoint_Management_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Endpoint_Management_API_NoParametersRequestSchema:
type: object
Expand Down Expand Up @@ -41723,9 +41723,9 @@ components:
- text
type: string
Security_Exceptions_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Exceptions_API_PlatformErrorResponse:
type: object
Expand Down Expand Up @@ -41969,9 +41969,9 @@ components:
- text
type: string
Security_Lists_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Lists_API_PlatformErrorResponse:
type: object
Expand Down
14 changes: 14 additions & 0 deletions packages/kbn-openapi-common/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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".
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-openapi-common'],
};
8 changes: 3 additions & 5 deletions packages/kbn-openapi-common/schemas/primitives.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
*/

import { z } from '@kbn/zod';
import { isNonEmptyString } from '@kbn/zod-helpers';

/**
* A string that is not empty and does not contain only whitespace
* A string that does not contain only whitespace characters
*/
export type NonEmptyString = z.infer<typeof NonEmptyString>;
export const NonEmptyString = z
.string()
.min(1)
.regex(/^(?! *$).+$/);
export const NonEmptyString = z.string().min(1).superRefine(isNonEmptyString);

/**
* A universally unique identifier
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-openapi-common/schemas/primitives.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ components:
schemas:
NonEmptyString:
type: string
pattern: ^(?! *$).+$
minLength: 1
description: A string that is not empty and does not contain only whitespace
format: nonempty
description: A string that does not contain only whitespace characters

UUID:
type: string
Expand Down
44 changes: 44 additions & 0 deletions packages/kbn-openapi-common/schemas/primitives.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 { NonEmptyString } from './primitives.gen';

describe('NonEmptyString', () => {
describe('accepts ', () => {
// \t\r\n\f
test('accepts newline chars', () => {
expect(() => NonEmptyString.parse('hello \nworld')).not.toThrow();
});
test('accepts tab chars', () => {
expect(() => NonEmptyString.parse('hello \tworld')).not.toThrow();
});
test('accepts carriage return chars', () => {
expect(() => NonEmptyString.parse('hello \rworld')).not.toThrow();
});
test('accepts form feed return chars', () => {
expect(() => NonEmptyString.parse('hello \fworld')).not.toThrow();
});
});
describe('rejects', () => {
test('rejects only tab chars chars', () => {
expect(() => NonEmptyString.parse('\t\t\t\t')).toThrow();
});
test('rejects only newline chars chars', () => {
expect(() => NonEmptyString.parse('\n\n\n\n\n')).toThrow();
});
test('rejects only carriage return chars chars', () => {
expect(() => NonEmptyString.parse('\r\r\r\r')).toThrow();
});
test('rejects only form feed chars chars', () => {
expect(() => NonEmptyString.parse('\f\f\f\f\f')).toThrow();
});
test('rejects comment with just spaces', () => {
expect(() => NonEmptyString.parse(' ')).toThrow();
});
});
});
1 change: 1 addition & 0 deletions packages/kbn-openapi-common/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"include": ["**/*.ts"],
"kbn_references": [
"@kbn/zod",
"@kbn/zod-helpers",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import type { ZodTypeDef } from '@kbn/zod';
import { z } from '@kbn/zod';
import { requiredOptional, isValidDateMath, ArrayFromString, BooleanFromString } from '@kbn/zod-helpers';
import { requiredOptional, isValidDateMath, isNonEmptyString, ArrayFromString, BooleanFromString } from '@kbn/zod-helpers';

{{#each imports}}
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,8 @@ z.unknown()
{{~#if (eq format 'date-math')}}.superRefine(isValidDateMath){{/if~}}
{{~#if (eq format 'uuid')}}.uuid(){{/if~}}
{{~#if pattern}}.regex(/{{pattern}}/){{/if~}}
{{~#if (eq format 'trim')}}.trim(){{/if~}}
{{~#if (eq format 'nonempty')}}.superRefine(isNonEmptyString){{/if~}}

{{~/if~}}
{{~/inline~}}
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1800,9 +1800,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1800,9 +1800,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1487,9 +1487,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1487,9 +1487,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-zod-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
19 changes: 19 additions & 0 deletions packages/kbn-zod-helpers/src/non_empty_string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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): void {
if (input.trim() === '') {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'No empty strings allowed',
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -978,9 +978,9 @@ components:
- assistant
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
NormalizedAnonymizationFieldError:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,9 +978,9 @@ components:
- assistant
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
NormalizedAnonymizationFieldError:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
*/

import { z } from '@kbn/zod';
import { isNonEmptyString } from '@kbn/zod-helpers';

/**
* A string that is not empty and does not contain only whitespace
* A string that does not contain only whitespace characters
*/
export type NonEmptyString = z.infer<typeof NonEmptyString>;
export const NonEmptyString = z
.string()
.min(1)
.regex(/^(?! *$).+$/);
export const NonEmptyString = z.string().min(1).superRefine(isNonEmptyString);

/**
* A universally unique identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ components:
schemas:
NonEmptyString:
type: string
pattern: ^(?! *$).+$
format: nonempty
minLength: 1
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters

UUID:
type: string
Expand All @@ -33,4 +33,3 @@ components:
enum:
- 'asc'
- 'desc'

Loading