Skip to content

Commit

Permalink
Changed oneOfType to an OR operator
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianrosik committed Mar 13, 2018
1 parent a7828f5 commit f3d47f3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ results.then((errors) => {
| Date | This type check value is instance of Date |
| Array | This type check value is array of any value |
| new Schema | This type check value is instance of Schema and validate value by this schema |
| Schema.oneOfType([type1, type2, ...]) | This type give you posibility check one of types it will return error if value don't match all types |
| Schema.operators.or([type1, type2, ...]) | This type give you posibility check one of types it will return error if value don't match all types |
| Schema.optionalType(type) | This type will pass validation if value is null or undefined when field is not required |
| SchemaType | You can register new schema type that has name, validator, validator when field is required (requiredValidator) and getDefaultValue |
| [OneOfTypesAbove] | This type check value is array of type |
Expand Down Expand Up @@ -207,7 +207,7 @@ const validateIfOfAge = () => ({
If You want create new schema You must put object to constructor with information about object keys names and type of value on key.

```js
import Schema from 'form-schema-validation';
import Schema, { operators } from 'form-schema-validation';

const min = (minLength, message) => ({
validator: (value) => {
Expand All @@ -224,7 +224,7 @@ const schema = new Schema({
validators: [min(2, 'Company name should be longer then 2 chars')]
},
createdAt: {
type: Schema.oneOfTypes([Date, String]),
type: operators.or([Date, String]),
defaultValue: new Date(),
label: 'When start'
},
Expand Down
24 changes: 16 additions & 8 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import validateNumber from './validators/number';
import validateObject from './validators/object';
import validateString from './validators/string';

import OneOfTypes from './operators/OneOfTypes';
import OrOperator from './operators/OrOperator';
import SchemaType from './SchemaType';

const defaultMessages = {
Expand All @@ -44,11 +44,19 @@ const handleTypeValidation = (validatorName, schema, value, key, index) => {
schema.setError(key, schema.messages.validateString(label || key), index);
}
return result;
}
};

export const operators = {
or(types) {
return new OrOperator(types);
},
};

class Schema {
export default class Schema {
static oneOfTypes(types) {
return new OneOfTypes(types);
// eslint-disable-next-line no-console
console.warn('[Deprecated] Pleas use `Schema.operators.or` instead');
return operators.or(types);
}
static optionalType(type, uniqueTypeName = '') {
const fieldType = getFieldType({ type });
Expand Down Expand Up @@ -367,8 +375,8 @@ class Schema {
if (type instanceof Schema) {
return this.validateTypeSchema(value, key, type, index);
}
if (type instanceof OneOfTypes) {
return this.validateOneOfTypes(value, key, type, index);
if (type instanceof OrOperator) {
return this.validateOrOperator(value, key, type, index);
}
throw new Error(`Unrecognized type ${typeName}`);
}
Expand Down Expand Up @@ -417,7 +425,7 @@ class Schema {
return false;
}

validateOneOfTypes(value, key, type, index) {
validateOrOperator(value, key, type, index) {
const schema = new Schema(type.getSchema());
const errors = schema.validate(type.parseValue(value));
const keys = Object.keys(errors);
Expand Down Expand Up @@ -476,4 +484,4 @@ class Schema {
}
}

export default Schema;
Schema.operators = operators;
4 changes: 2 additions & 2 deletions src/operators/OneOfTypes.js → src/operators/OrOperator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class OneOfTypes {
class OrOperator {
constructor(types) {
this.types = types;
}
Expand All @@ -24,4 +24,4 @@ class OneOfTypes {
}
}

export default OneOfTypes;
export default OrOperator;
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import OneOfTypes from './OneOfTypes';
import OrOperator from './OrOperator';

describe('OneOfTypes', () => {
describe('OrOperator', () => {
it('getTypes should return array with 2 types', () => {
const oneOfTypes = new OneOfTypes([String, Number]);
const oneOfTypes = new OrOperator([String, Number]);
expect(oneOfTypes.getTypes().length).toBe(2);
});

it('should return empty array if types undefined', () => {
const oneOfTypes = new OneOfTypes();
const oneOfTypes = new OrOperator();
expect(oneOfTypes.getTypes().length).toBe(0);
});

it('should return schema object', () => {
const oneOfTypes = new OneOfTypes([String, Number]);
const oneOfTypes = new OrOperator([String, Number]);
const schema = oneOfTypes.getSchema();
expect(schema.type0.type).toBe(String);
expect(schema.type1.type).toBe(Number);
});

it('should return parsed value', () => {
const oneOfTypes = new OneOfTypes([String, Number]);
const oneOfTypes = new OrOperator([String, Number]);
const value = 'testValue';
const parsedValue = oneOfTypes.parseValue(value);
expect(parsedValue.type0).toBe(value);
Expand Down

0 comments on commit f3d47f3

Please sign in to comment.