@rudi23/yup-to-openapi
v2.1.3
Published
Configurable, input validated routing for koa using yup
Downloads
2,205
Maintainers
Readme
yup-to-openapi
Simple tool to convert yup object schema to OpenApi 3 object schema.
Installation
Install using npm
:
npm install @rudi23/yup-to-openapi
NodeJS >= 12.0.0.
is required.
Example
import yupToOpenAPI from '@rudi23/yup-to-openapi';
const yupSchema = yup.object({
firstName: yup.string().required(),
lastName: yup.number().required(),
age: yup.number().required().min(0).max(100),
job: yup.string(),
});
const openApiSchema = yupToOpenAPI(yupSchema);
yup methods implemented
API
mixed.label(label: string)
const input = yup.string().label('label');
yupToOpenAPI(input); // => { type: 'string', title: 'label' }
mixed.meta({ title: string })
const input = yup.string().meta({ title: 'title' });
yupToOpenAPI(input); // => { type: 'string', title: 'title' };
mixed.meta({ description: string })
const input = yup.string().meta({ description: 'description' });
yupToOpenAPI(input); // => { type: 'string', description: 'description' };
mixed.nullable()
const input = yup.string().nullable();
yupToOpenAPI(input); // => { type: 'string', nullable: true }
mixed.default(value: any)
const input = yup.string().default('default value');
yupToOpenAPI(input); // => { type: 'string', default: 'default value' }
const input = yup.number().default(1.23);
yupToOpenAPI(input); // => { type: 'string', format: 'float', default: 1.23 }
mixed.oneOf(arrayOfValues: Array<any>)
const input = yup.string().oneOf(['a', 'b', 'c']);
yupToOpenAPI(input); // => { type: 'string', enum: ['a', 'b', 'c'] }
const input = yup.number().oneOf([1, 2, 3]);
yupToOpenAPI(input); // => { type: 'number', format: 'float', enum: [1, 2, 3] }
mixed.required()
required()
works only for object's props
const input = yup.object({
foo: yup.string().required(),
bar: yup.string(),
baz: yup.number().required(),
});
yupToOpenAPI(input); // =>
// {
// type: 'object',
// properties: {
// foo: { type: 'string' },
// bar: { type: 'string' },
// baz: { type: 'number', format: 'float' },
// },
// required: ['foo', 'baz'],
// }
string
const input = yup.string();
yupToOpenAPI(input); // => { type: 'string' }
string.min(limit: number)
const input = yup.string().min(1);
yupToOpenAPI(input); // => { type: 'string', minLength: 1 }
string.max(max: number)
const input = yup.string().max(10);
yupToOpenAPI(input); // => { type: 'string', maxLength: 10 }
string.matches(regex: Regex)
const input = yup.string().matches(/(hi|bye)/);
yupToOpenAPI(input); // => { type: 'string', pattern: /(hi|bye)/ }
string.email()
const input = yup.string().email();
yupToOpenAPI(input); // => { type: 'string', format: 'email' }
string.url()
const input = yup.string().url();
yupToOpenAPI(input); // => { type: 'string', format: 'url' }
string.uuid()
const input = yup.string().uuid();
yupToOpenAPI(input); // => { type: 'string', format: 'uuid' }
number
const input = yup.number();
yupToOpenAPI(input); // => { type: 'integer', format: 'float' };
number.integer()
const input = yup.number().integer();
yupToOpenAPI(input); // => { type: 'integer', format: 'int32' };
number.min(limit: number)
const input = yup.number().min(1);
yupToOpenAPI(input); // => { type: 'number', format: 'float', minimum: 1 };
number.max(limit: number)
const input = yup.number().max(10);
yupToOpenAPI(input); // => { type: 'number', format: 'float', maximum: 10 };
number.positive()
const input = yup.number().positive();
yupToOpenAPI(input); // => { type: 'number', format: 'float', minimum: 0, exclusiveMinimum: true };
number.negative()
const input = yup.number().negative();
yupToOpenAPI(input); // => { type: 'number', format: 'float', maximum: 0, exclusiveMaximum: true };
number.lessThan(max: number)
const input = yup.number().lessThan(100);
yupToOpenAPI(input); // => { type: 'number', format: 'float', maximum: 100, exclusiveMaximum: true };
number.moreThan(min: number)
const input = yup.number().moreThan(10);
yupToOpenAPI(input); // => { type: 'number', format: 'float', minimum: 10, exclusiveMinimum: true };
boolean
const input = yup.boolean();
yupToOpenAPI(input); // => { type: 'boolean' };
date
const input = yup.date();
yupToOpenAPI(input); // => { type: 'string', format: 'date' };
date.default(value: function | Date)
const input = yup.date().default(new Date('2021-01-01T00:00:00.000Z'));
yupToOpenAPI(input); // => { type: 'string', format: 'date', default: new Date('2021-01-01T00:00:00.000Z') };
const input = yup.date().default(function now() {
return new Date('2021-01-01T00:00:00.000Z');
});
yupToOpenAPI(input); // => { type: 'string', format: 'date', default: new Date('2021-01-01T00:00:00.000Z') };
array
const input = yup.array().of(yup.string());
yupToOpenAPI(input); // =>
// {
// type: 'array',
// items: {
// type: 'string',
// },
// }
array.min(limit: number)
const input = yup.array().min(1);
yupToOpenAPI(input); // =>
// {
// type: 'array',
// minItems: 1,
// }
array.max(limit: number)
const input = yup.array().max(10);
yupToOpenAPI(input); // =>
// {
// type: 'array',
// maxItems: 10,
// }
object
const input = yup.object({
foo: yup.string(),
bar: yup.string(),
});
yupToOpenAPI(input); // =>
// {
// type: 'object',
// properties: {
// foo: { type: 'string' },
// bar: { type: 'string' },
// },
// }