Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for simple object pattern in schema #356

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
74 changes: 74 additions & 0 deletions src/__tests__/patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Joi from 'joi';

import { convertSchema } from '../index';

describe('test `Joi.object().pattern()`', () => {
test('`pattern(Joi.string(), Joi.AnySchema())`', () => {
const schema = Joi.object()
.pattern(Joi.string(), Joi.array().items(Joi.object({
id: Joi.string().required(),
propertyName1: Joi.boolean().required()
})))
.description('a test pattern schema definition');

const result = convertSchema({}, schema, 'TestSchema');
expect(result).not.toBeUndefined;
expect(result?.content).toBe(`/**
* a test pattern schema definition
*/
export interface TestSchema {
[pattern: string]: {
id: string;
propertyName1: boolean;
}[];
}`);
});

test('`pattern(Joi.string(), Joi.number())`', () => {
const schema = Joi.object()
.description('a test deep pattern schema definition')
.pattern(Joi.string(), Joi.number().description('Number Property'));

const result = convertSchema({}, schema, 'TestSchema');
expect(result).not.toBeUndefined;
expect(result?.content).toBe(`/**
* a test deep pattern schema definition
*/
export interface TestSchema {
/**
* Number Property
*/
[pattern: string]: number;
}`);
});

test('`pattern(/^test$/, Joi.AnySchema())`', () => {
const schema = Joi.object({
name: Joi.string(),
})
.description('a test regex pattern schema definition')
.pattern(Joi.string(), {
name: Joi.string().optional(),
propertyName1: Joi.object().pattern(/^test$/, Joi.object({
propertyName2: Joi.boolean()
})).required()
});

const result = convertSchema({ sortPropertiesByName: false }, schema, 'TestSchema');
expect(result).not.toBeUndefined;
expect(result?.content).toBe(`/**
* a test regex pattern schema definition
*/
export interface TestSchema {
name?: string;
[pattern: string]: {
name?: string;
propertyName1: {
[pattern: string]: {
propertyName2?: boolean;
};
};
};
}`);
});
});
42 changes: 0 additions & 42 deletions src/__tests__/unknown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,6 @@ export interface TestSchema {
}`);
});

test('`pattern(Joi.string(), Joi.number())`', () => {
mrjono1 marked this conversation as resolved.
Show resolved Hide resolved
const schema = Joi.object({
name: Joi.string()
})
.meta({ className: 'TestSchema', unknownType: 'number' })
.description('a test schema definition')
.pattern(Joi.string(), Joi.number());

const result = convertSchema({ sortPropertiesByName: false }, schema);
expect(result).not.toBeUndefined;
expect(result?.content).toBe(`/**
* a test schema definition
*/
export interface TestSchema {
name?: string;
/**
* Number Property
*/
[x: string]: number;
}`);
});

test('`unknown(true).meta({ unknownType: Joi.AnySchema() })`', () => {
const schema = Joi.object({})
.unknown(true)
Expand All @@ -109,26 +87,6 @@ export interface TestSchema {
[x: string]: {
id: string;
}[];
}`);
});

test('`pattern(Joi.string(), Joi.AnySchema())`', () => {
const unknownTypeSchema = Joi.array().items(Joi.object({ id: Joi.string().required() }));

const schema = Joi.object({})
.pattern(Joi.string(), unknownTypeSchema)
.meta({ className: 'TestSchema', unknownType: unknownTypeSchema })
.description('a test schema definition');

const result = convertSchema({}, schema);
expect(result).not.toBeUndefined;
expect(result?.content).toBe(`/**
* a test schema definition
*/
export interface TestSchema {
[x: string]: {
id: string;
}[];
}`);
});
});
3 changes: 2 additions & 1 deletion src/joiDescribeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export interface BaseDescribe extends Joi.Description {
* https://joi.dev/api/#objectpatternpattern-schema-options
*/
patterns?: {
schema: Describe;
schema?: Describe;
regex?: string;
rule: Describe;
}[];
metas?: Meta[];
Expand Down
21 changes: 19 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,19 @@ function parseUnknown(details: ObjectDescribe, settings: Settings): TypeContent
return buildUnknownTypeContent();
}

function parsePatterns(details: ObjectDescribe, settings: Settings): TypeContent {
const parsedSchema = details.patterns ? parseSchema(details.patterns[0].rule, settings) : null;

if (!parsedSchema) {
return parseUnknown(details, settings);
}

parsedSchema.required = true;
parsedSchema.interfaceOrTypeName = '[pattern: string]';

return parsedSchema;
}

function parseObjects(details: ObjectDescribe, settings: Settings): TypeContent | undefined {
let children = filterMap(Object.entries(details.keys || {}), ([key, value]) => {
const parsedSchema = parseSchema(value, settings);
Expand All @@ -478,8 +491,12 @@ function parseObjects(details: ObjectDescribe, settings: Settings): TypeContent
parsedSchema.interfaceOrTypeName = /^[$A-Z_][0-9A-Z_$]*$/i.test(key || '') ? key : `'${key}'`;
return parsedSchema;
});
const isMap = details.patterns?.length === 1 && details.patterns[0].schema.type === 'string';
if (details?.flags?.unknown === true || isMap) {

if (details.patterns?.length === 1 && (details.patterns[0].schema?.type === 'string' || details.patterns[0].regex)) {
children.push(parsePatterns(details, settings));
}

if (details?.flags?.unknown === true) {
children.push(parseUnknown(details, settings));
}

Expand Down