UNPKG

@condenast/flyway-schema-validator

Version:
107 lines (99 loc) 2.38 kB
const joi = require('joi'); const article = require('./models/article'); const nativearticle = require('./models/nativeArticle'); const gallery = require('./models/gallery'); const nativegallery = require('./models/nativeGallery'); const product = require('./models/product'); const recipe = require('./models/recipe'); const hotel = require('./models/hotel'); const business = require('./models/business'); const nativerecipe = require('./models/nativeRecipe'); const { fashionShow, city, contributor, brand, season, fashiongallery, fashionImage, person, runwaymoment, review } = require('./models/fashionShows'); const httpPattern = /^https?:\/\//; const paginatedStub = joi .object() .keys({ id: joi.string().required(), type: joi .string() .valid( 'article', 'fashionShow', 'gallery', 'business', 'hotel', 'nativearticle', 'nativegallery', 'nativerecipe', 'product', 'recipe', 'review' ) .required(), self: joi.string().regex(httpPattern).required() }) .unknown(true); const PaginatedResponseModel = joi .object() .keys({ brand: joi.string().required(), self: joi.string().regex(httpPattern).required(), next: joi.string().regex(httpPattern).optional(), prev: joi.string().regex(httpPattern).optional(), page: joi.number().positive().integer().required(), totalCount: joi.number().integer().min(0).required(), hasMore: joi.boolean().required(), data: joi.array().items(paginatedStub).required() }) .unknown(true); const models = { article, business, contributor, gallery, hotel, nativearticle, nativegallery, nativerecipe, product, recipe, review, brand, city, fashiongallery, fashionShow, person, photo: fashionImage, runwaymoment, season }; const topLevelBase = joi .object({ type: joi .string() .valid(...Object.keys(models)) .required() }) .unknown() .required(); module.exports = async function flywaySchemaValidator(value, paginatedResponse = false) { if (paginatedResponse) { return PaginatedResponseModel.validateAsync(value); } await topLevelBase.validateAsync(value); // value has a valid type const { type } = value; const schema = models[type]; return schema.validateAsync(value); };