Skip to content

Commit

Permalink
feat: Coerce primitive config values to arrays, when array is expected
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Oct 1, 2020
1 parent ddd8f88 commit a6ff964
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lib/classes/ConfigSchemaHandler/index.js
Expand Up @@ -91,7 +91,7 @@ class ConfigSchemaHandler {
this.relaxProviderSchema();
}

const ajv = new Ajv({ allErrors: true, coerceTypes: true, verbose: true });
const ajv = new Ajv({ allErrors: true, coerceTypes: 'array', verbose: true });
require('ajv-keywords')(ajv, 'regexp');
// Workaround https://github.com/ajv-validator/ajv/issues/1255
normalizeSchemaObject(this.schema, this.schema);
Expand Down
1 change: 0 additions & 1 deletion lib/configSchema.js
Expand Up @@ -17,7 +17,6 @@ const schema = {
disabledDeprecations: {
anyOf: [
{ const: '*' },
{ $ref: '#/definitions/errorCode' },
{
type: 'array',
items: { $ref: '#/definitions/errorCode' },
Expand Down
16 changes: 7 additions & 9 deletions lib/plugins/aws/package/compile/events/alb/index.js
Expand Up @@ -7,10 +7,8 @@ const compileTargetGroups = require('./lib/targetGroups');
const compileListenerRules = require('./lib/listenerRules');
const compilePermissions = require('./lib/permissions');

function arrayOrSingleSchema(schema) {
return {
oneOf: [schema, { type: 'array', items: schema }],
};
function defineArray(schema) {
return { type: 'array', items: schema };
}

class AwsCompileAlbEvents {
Expand All @@ -37,7 +35,7 @@ class AwsCompileAlbEvents {
this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'alb', {
type: 'object',
properties: {
authorizer: arrayOrSingleSchema({ type: 'string' }),
authorizer: defineArray({ type: 'string' }),
conditions: {
type: 'object',
properties: {
Expand All @@ -50,19 +48,19 @@ class AwsCompileAlbEvents {
additionalProperties: false,
required: ['name', 'values'],
},
host: arrayOrSingleSchema({
host: defineArray({
type: 'string',
pattern: '^[A-Za-z0-9*?.-]+$',
maxLength: 128,
}),
ip: arrayOrSingleSchema({
ip: defineArray({
oneOf: [
{ type: 'string', format: 'ipv4' },
{ type: 'string', format: 'ipv6' },
],
}),
method: arrayOrSingleSchema({ type: 'string', pattern: '^[A-Z_-]+$', maxLength: 40 }),
path: arrayOrSingleSchema({
method: defineArray({ type: 'string', pattern: '^[A-Z_-]+$', maxLength: 40 }),
path: defineArray({
type: 'string',
pattern: '^([A-Za-z0-9*?_.$/~"\'@:+-]|&)+$',
maxLength: 128,
Expand Down
4 changes: 1 addition & 3 deletions lib/plugins/aws/package/compile/events/httpApi/index.js
Expand Up @@ -68,9 +68,7 @@ class HttpApiEvents {
anyOf: [{ type: 'string' }, { $ref: '#/definitions/awsCfFunction' }],
},
name: { type: 'string' },
scopes: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
scopes: { type: 'array', items: { type: 'string' } },
},
oneOf: [{ required: ['id'] }, { required: ['name'] }],
additionalProperties: false,
Expand Down
52 changes: 12 additions & 40 deletions lib/plugins/aws/provider/awsProvider.js
Expand Up @@ -282,9 +282,7 @@ class AwsProvider {
required: ['Fn::Sub'],
additionalProperties: false,
},
awsIamPolicyAction: {
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
awsIamPolicyAction: { type: 'array', items: { type: 'string' } },
awsIamPolicyPrincipal: {
anyOf: [
{ const: '*' },
Expand All @@ -294,30 +292,19 @@ class AwsProvider {
AWS: {
anyOf: [
{ const: '*' },
{ $ref: '#/definitions/awsArn' },
{ type: 'array', items: { $ref: '#/definitions/awsArn' } },
],
},
Federated: {
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
Service: {
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
CanonicalUser: {
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
Federated: { type: 'array', items: { type: 'string' } },
Service: { type: 'array', items: { type: 'string' } },
CanonicalUser: { type: 'array', items: { type: 'string' } },
},
additionalProperties: false,
},
],
},
awsIamPolicyResource: {
anyOf: [
{ const: '*' },
{ $ref: '#/definitions/awsArn' },
{ type: 'array', items: { $ref: '#/definitions/awsArn' } },
],
anyOf: [{ const: '*' }, { type: 'array', items: { $ref: '#/definitions/awsArn' } }],
},
// Definition of Statement taken from https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_grammar.html#policies-grammar-bnf
awsIamPolicyStatements: {
Expand Down Expand Up @@ -405,9 +392,7 @@ class AwsProvider {
pattern: '^[/#A-Za-z0-9-_.]+$',
},
awsResourceCondition: { type: 'string' },
awsResourceDependsOn: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
awsResourceDependsOn: { type: 'array', items: { type: 'string' } },
awsResourceProperties: {
Properties: { type: 'object' },
CreationPolicy: { type: 'object' },
Expand Down Expand Up @@ -489,13 +474,8 @@ class AwsProvider {
identitySource: { $ref: '#/definitions/awsCfInstruction' },
issuerUrl: { $ref: '#/definitions/awsCfInstruction' },
audience: {
oneOf: [
{ $ref: '#/definitions/awsCfInstruction' },
{
type: 'array',
items: { $ref: '#/definitions/awsCfInstruction' },
},
],
type: 'array',
items: { $ref: '#/definitions/awsCfInstruction' },
},
},
required: ['identitySource', 'issuerUrl', 'audience'],
Expand All @@ -509,18 +489,10 @@ class AwsProvider {
type: 'object',
properties: {
allowCredentials: { type: 'boolean' },
allowedHeaders: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
allowedMethods: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
allowedOrigins: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
exposedResponseHeaders: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
allowedHeaders: { type: 'array', items: { type: 'string' } },
allowedMethods: { type: 'array', items: { type: 'string' } },
allowedOrigins: { type: 'array', items: { type: 'string' } },
exposedResponseHeaders: { type: 'array', items: { type: 'string' } },
maxAge: { type: 'integer', minimum: 0 },
},
additionalProperties: false,
Expand Down

0 comments on commit a6ff964

Please sign in to comment.