@stackbit/sdk
Advanced tools
Comparing version 0.2.13 to 0.2.14
@@ -78,3 +78,3 @@ "use strict"; | ||
backgroundSize: ['auto', 'cover', 'contain'], | ||
borderRadius: ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'full'], | ||
borderRadius: ['none', 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'full'], | ||
borderStyle: ['solid', 'dashed', 'dotted', 'double', 'none'], | ||
@@ -81,0 +81,0 @@ boxShadow: ['none', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'inner'] |
@@ -10,10 +10,17 @@ "use strict"; | ||
const config_consts_1 = require("../config-consts"); | ||
const sizePattern = /^[xylrtb](?:\d+(?::\d+(?::\d+)?)?)?|\d+(?::\d+(?::\d+)?)?|tw[xylrtb]?(?:\d+|\d\.5|px)?$/; | ||
const styleSizeSchema = stylePropWithAll(joi_1.default.array().items(joi_1.default.string().pattern(sizePattern)).single()).prefs({ | ||
const sizePattern = /^[xylrtb](?:\d+(?::\d+(?::\d+)?)?)?|\d+(?::\d+(?::\d+)?)?$/; | ||
const sizeTailwindPattern = /^tw[xylrtb]?(?:\d+|\d\.5|px)?$/; | ||
const styleSizeSchema = stylePropWithAll(joi_1.default.array().items(joi_1.default.string().pattern(sizePattern).meta({ errorDesc: 'size pattern' })).single(), joi_1.default.array().items(joi_1.default.string().pattern(sizeTailwindPattern).meta({ errorDesc: 'tailwind size pattern' })).single()).prefs({ | ||
messages: { | ||
'string.pattern.base': 'Invalid field name "{{#value}}" at "{{#label}}". A field name must contain only alphanumeric characters, ' + | ||
'hyphens and underscores, must start and end with an alphanumeric character.' | ||
'string.pattern.base': 'Illegal definition "{{#value}}" of style field "{{#label}}". This field must match "padding" or "margin" style pattern' | ||
}, | ||
errors: { wrap: { label: false } } | ||
}); | ||
const borderWidthPattern = /^\d+(?::\d+(?::\d+)?)?$/; | ||
const borderWidthSchema = stylePropWithAll(joi_1.default.array().items(joi_1.default.string().pattern(borderWidthPattern)).single()).prefs({ | ||
messages: { | ||
'string.pattern.base': 'Illegal definition "{{#value}}" of style field "{{#label}}". This field must match "borderWidth" style pattern' | ||
}, | ||
errors: { wrap: { label: false } } | ||
}); | ||
const fontWeightPattern = /^[1-8]00:[2-9]00$/; | ||
@@ -54,3 +61,3 @@ const opacityPattern = /^[1-9]?[05]:(?:5|[1-9][05]|100)$/; | ||
borderRadius: arrayOfStringsWithAll(...config_consts_1.STYLE_PROPS_VALUES.borderRadius), | ||
borderWidth: styleSizeSchema, | ||
borderWidth: borderWidthSchema, | ||
borderColor: styleColorSchema, | ||
@@ -123,3 +130,4 @@ borderStyle: arrayOfStringsWithAll(...config_consts_1.STYLE_PROPS_VALUES.borderStyle), | ||
else if (lodash_1.default.has(schemaItem, 'rules') && lodash_1.default.some(schemaItem.rules, { name: 'pattern' })) { | ||
localTypes.singleItems.push(`array of ${stylePropName} pattern`); | ||
const errorDesc = lodash_1.default.get(schemaItem, ['metas', 0, 'errorDesc'], 'pattern'); | ||
localTypes.singleItems.push(`array of ${stylePropName} ${errorDesc}`); | ||
} | ||
@@ -126,0 +134,0 @@ } |
@@ -251,3 +251,3 @@ "use strict"; | ||
borderRadius: stylePropSchemaWithValidValues(config_consts_1.STYLE_PROPS_VALUES.borderRadius), | ||
borderWidth: stylePropSizeSchema, | ||
borderWidth: stylePropBorderWidthSchema, | ||
borderColor: stylePropObjectValueSchema, | ||
@@ -278,4 +278,6 @@ borderStyle: stylePropSchemaWithValidValues(config_consts_1.STYLE_PROPS_VALUES.borderStyle), | ||
} | ||
// TODO: validate Tailwind paddings | ||
styleConfig = lodash_1.default.castArray(styleConfig); | ||
if (lodash_1.default.some(styleConfig, (style) => lodash_1.default.startsWith(style, 'tw'))) { | ||
return joi_1.default.array().items(joi_1.default.string()); | ||
} | ||
const dirSchemas = lodash_1.default.reduce(styleConfig, (dirSchemas, pattern) => { | ||
@@ -315,3 +317,3 @@ if (typeof pattern !== 'string') { | ||
if (parts.length === 3) { | ||
valueSchema = valueSchema.multiple(parts[3]); | ||
valueSchema = valueSchema.multiple(parts[2]); | ||
} | ||
@@ -328,2 +330,35 @@ } | ||
} | ||
function stylePropBorderWidthSchema(styleConfig) { | ||
if (styleConfig === '*') { | ||
return joi_1.default.number(); | ||
} | ||
styleConfig = lodash_1.default.castArray(styleConfig); | ||
const alternativeSchemas = lodash_1.default.reduce(styleConfig, (alternativeSchemas, value) => { | ||
let valueSchema = joi_1.default.number(); | ||
if (lodash_1.default.isNumber(value)) { | ||
return alternativeSchemas.concat(valueSchema.valid(value)); | ||
} | ||
if (!lodash_1.default.isString(value)) { | ||
return alternativeSchemas; | ||
} | ||
if (lodash_1.default.isEmpty(value)) { | ||
return alternativeSchemas; | ||
} | ||
const parts = value.split(':').map((value) => Number(value)); | ||
if (lodash_1.default.some(parts, lodash_1.default.isNaN)) { | ||
return alternativeSchemas; | ||
} | ||
if (parts.length === 1) { | ||
return alternativeSchemas.concat(valueSchema.valid(value)); | ||
} | ||
const start = parts[0]; | ||
const end = parts[1]; | ||
valueSchema = valueSchema.min(start).max(end); | ||
if (parts.length === 3) { | ||
valueSchema = valueSchema.multiple(parts[2]); | ||
} | ||
return alternativeSchemas.concat(valueSchema); | ||
}, []); | ||
return joi_1.default.alternatives(...alternativeSchemas); | ||
} | ||
function stylePropObjectValueSchema(styleConfig) { | ||
@@ -330,0 +365,0 @@ return joi_1.default.valid(...lodash_1.default.map(styleConfig, (object) => lodash_1.default.get(object, 'value'))); |
{ | ||
"name": "@stackbit/sdk", | ||
"version": "0.2.13", | ||
"version": "0.2.14", | ||
"description": "Stackbit SDK", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -79,5 +79,5 @@ // SSGs Stackbit Studio supports | ||
backgroundSize: ['auto', 'cover', 'contain'], | ||
borderRadius: ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'full'], | ||
borderRadius: ['none', 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'full'], | ||
borderStyle: ['solid', 'dashed', 'dotted', 'double', 'none'], | ||
boxShadow: ['none', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'inner'] | ||
}; |
@@ -6,8 +6,10 @@ import Joi, { CustomHelpers, ErrorReport } from 'joi'; | ||
const sizePattern = /^[xylrtb](?:\d+(?::\d+(?::\d+)?)?)?|\d+(?::\d+(?::\d+)?)?|tw[xylrtb]?(?:\d+|\d\.5|px)?$/; | ||
const styleSizeSchema = stylePropWithAll(Joi.array().items(Joi.string().pattern(sizePattern)).single()).prefs({ | ||
const sizePattern = /^[xylrtb](?:\d+(?::\d+(?::\d+)?)?)?|\d+(?::\d+(?::\d+)?)?$/; | ||
const sizeTailwindPattern = /^tw[xylrtb]?(?:\d+|\d\.5|px)?$/; | ||
const styleSizeSchema = stylePropWithAll( | ||
Joi.array().items(Joi.string().pattern(sizePattern).meta({ errorDesc: 'size pattern' })).single(), | ||
Joi.array().items(Joi.string().pattern(sizeTailwindPattern).meta({ errorDesc: 'tailwind size pattern' })).single() | ||
).prefs({ | ||
messages: { | ||
'string.pattern.base': | ||
'Invalid field name "{{#value}}" at "{{#label}}". A field name must contain only alphanumeric characters, ' + | ||
'hyphens and underscores, must start and end with an alphanumeric character.' | ||
'string.pattern.base': 'Illegal definition "{{#value}}" of style field "{{#label}}". This field must match "padding" or "margin" style pattern' | ||
}, | ||
@@ -17,2 +19,10 @@ errors: { wrap: { label: false } } | ||
const borderWidthPattern = /^\d+(?::\d+(?::\d+)?)?$/; | ||
const borderWidthSchema = stylePropWithAll(Joi.array().items(Joi.string().pattern(borderWidthPattern)).single()).prefs({ | ||
messages: { | ||
'string.pattern.base': 'Illegal definition "{{#value}}" of style field "{{#label}}". This field must match "borderWidth" style pattern' | ||
}, | ||
errors: { wrap: { label: false } } | ||
}); | ||
const fontWeightPattern = /^[1-8]00:[2-9]00$/; | ||
@@ -62,3 +72,3 @@ const opacityPattern = /^[1-9]?[05]:(?:5|[1-9][05]|100)$/; | ||
borderRadius: arrayOfStringsWithAll(...STYLE_PROPS_VALUES.borderRadius), | ||
borderWidth: styleSizeSchema, | ||
borderWidth: borderWidthSchema, | ||
borderColor: styleColorSchema, | ||
@@ -136,3 +146,4 @@ borderStyle: arrayOfStringsWithAll(...STYLE_PROPS_VALUES.borderStyle), | ||
} else if (_.has(schemaItem, 'rules') && _.some(schemaItem.rules, { name: 'pattern' })) { | ||
localTypes.singleItems.push(`array of ${stylePropName} pattern`); | ||
const errorDesc = _.get(schemaItem, ['metas', 0, 'errorDesc'], 'pattern'); | ||
localTypes.singleItems.push(`array of ${stylePropName} ${errorDesc}`); | ||
} | ||
@@ -139,0 +150,0 @@ } else if (schemaItem.type === 'number') { |
@@ -301,3 +301,3 @@ import Joi from 'joi'; | ||
borderRadius: stylePropSchemaWithValidValues(STYLE_PROPS_VALUES.borderRadius), | ||
borderWidth: stylePropSizeSchema, | ||
borderWidth: stylePropBorderWidthSchema, | ||
borderColor: stylePropObjectValueSchema, | ||
@@ -330,4 +330,6 @@ borderStyle: stylePropSchemaWithValidValues(STYLE_PROPS_VALUES.borderStyle), | ||
} | ||
// TODO: validate Tailwind paddings | ||
styleConfig = _.castArray(styleConfig); | ||
if (_.some(styleConfig, (style) => _.startsWith(style, 'tw'))) { | ||
return Joi.array().items(Joi.string()); | ||
} | ||
const dirSchemas = _.reduce( | ||
@@ -367,3 +369,3 @@ styleConfig, | ||
if (parts.length === 3) { | ||
valueSchema = valueSchema.multiple(parts[3]); | ||
valueSchema = valueSchema.multiple(parts[2]); | ||
} | ||
@@ -383,2 +385,41 @@ } | ||
function stylePropBorderWidthSchema(styleConfig: any) { | ||
if (styleConfig === '*') { | ||
return Joi.number(); | ||
} | ||
styleConfig = _.castArray(styleConfig); | ||
const alternativeSchemas = _.reduce( | ||
styleConfig, | ||
(alternativeSchemas: Joi.Schema[], value) => { | ||
let valueSchema = Joi.number(); | ||
if (_.isNumber(value)) { | ||
return alternativeSchemas.concat(valueSchema.valid(value)); | ||
} | ||
if (!_.isString(value)) { | ||
return alternativeSchemas; | ||
} | ||
if (_.isEmpty(value)) { | ||
return alternativeSchemas; | ||
} | ||
const parts = value.split(':').map((value) => Number(value)); | ||
if (_.some(parts, _.isNaN)) { | ||
return alternativeSchemas; | ||
} | ||
if (parts.length === 1) { | ||
return alternativeSchemas.concat(valueSchema.valid(value)); | ||
} | ||
const start = parts[0]!; | ||
const end = parts[1]!; | ||
valueSchema = valueSchema.min(start).max(end); | ||
if (parts.length === 3) { | ||
valueSchema = valueSchema.multiple(parts[2]!); | ||
} | ||
return alternativeSchemas.concat(valueSchema); | ||
}, | ||
[] | ||
); | ||
return Joi.alternatives(...alternativeSchemas); | ||
} | ||
function stylePropObjectValueSchema(styleConfig: any) { | ||
@@ -385,0 +426,0 @@ return Joi.valid(..._.map(styleConfig, (object) => _.get(object, 'value'))); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
726783
12820