Skip to content

Commit

Permalink
feat: enable array coercion for schemas, use schema while collecting …
Browse files Browse the repository at this point in the history
…cmd data (#96)
  • Loading branch information
ssube committed Jan 2, 2019
1 parent 41d8836 commit d3d52d8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
80 changes: 41 additions & 39 deletions src/controller/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { isNil, isNumber, isString } from 'lodash';
import { isNil, isString } from 'lodash';
import { BaseError } from 'noicejs';

import { NOUN_FRAGMENT } from 'src/controller/CompletionController';
import { Command, CommandVerb } from 'src/entity/Command';
import { InvalidArgumentError } from 'src/error/InvalidArgumentError';
import { Schema } from 'src/schema';
import { mapToDict } from 'src/utils/Map';
import { NOUN_FRAGMENT } from './CompletionController';
import { BaseError } from 'noicejs';
import { isNumber } from 'util';

export function createCompletion(cmd: Command, key: string, msg: string): Command {
if (isNil(cmd.context.parser)) {
Expand Down Expand Up @@ -88,49 +91,48 @@ export function collectOrComplete<TData extends CollectFields>(cmd: Command, fie
}

export function collectValue(value: CollectData, defaultValue: CollectData): CollectData | undefined {
if (Array.isArray(defaultValue)) {
if (Array.isArray(value)) {
return value;
}

if (isNumber(value)) {
return [value.toString(10)];
}
const schema = new Schema({
properties: {
value: buildValueSchema(defaultValue),
},
required: ['value'],
type: 'object',
});

if (isString(value)) {
return [value];
}
const coercedValue = { value };
if (schema.match(coercedValue)) {
return coercedValue.value;
} else {
throw new BaseError('value type error');
}
}

if (isNumber(defaultValue)) {
if (Array.isArray(value)) {
const [head] = value;
return parseInt(head, 10);
}

if (isNumber(value)) {
return value;
}

if (isString(value)) {
return parseInt(value, 10);
}
export function buildValueSchema(defaultValue: CollectData) {
if (Array.isArray(defaultValue)) {
return {
type: 'array',
items: {
default: defaultValue[0],
type: 'string',
},
};
}

if (isString(defaultValue)) {
if (Array.isArray(value)) {
const [head] = value;
return head;
}

if (isNumber(value)) {
return value.toString(10);
}
return {
default: defaultValue,
type: 'string',
};
}

if (isString(value)) {
return value;
}
if (isNumber(defaultValue)) {
return {
default: defaultValue,
type: 'number',
};
}

throw new BaseError('value type error');
return {
type: 'null',
};
}
2 changes: 1 addition & 1 deletion src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Schema {
constructor(schema: object = SCHEMA_GLOBAL) {
this.compiler = new Ajv({
allErrors: true,
coerceTypes: true,
coerceTypes: 'array',
missingRefs: 'fail',
removeAdditional: 'failing',
schemaId: 'auto',
Expand Down

0 comments on commit d3d52d8

Please sign in to comment.