项目作者: sarat1669

项目描述 :
Extensible type checking framework for arbitrary objects
高级语言: Java
项目地址: git://github.com/sarat1669/prop-types.git
创建时间: 2019-04-27T14:09:31Z
项目社区:https://github.com/sarat1669/prop-types

开源协议:Apache License 2.0

下载


prop-types Maven Central

Extensible type checking framework for arbitrary objects

Usage

To use prop-types, add the following dependency:

  1. <dependency>
  2. <groupId>com.factor18.oss</groupId>
  3. <artifactId>prop-types</artifactId>
  4. <version>VERSION</version>
  5. </dependency>

Quick Start

PropTypes is a powerful tool for validating arbitrary objects.

PropTypes exports a range of validators that can be used to make sure the data you receive is valid.

Here are some examples which showcase the possibilities using a simple integer validator

  1. PropType schema = PInteger.builder().defaultValue(10).required(true).build();
  2. // 123 will be assigned to 'a'
  3. Integer a = (Integer) schema.parse(123);
  4. // 10 will be assigned to 'b'
  5. Integer b = (Integer) schema.parse(null);
  6. // will throw InvalidPropTypeException
  7. Integer c = (Integer) schema.parse("invalid");
  1. PropType schemaWithoutRequired = PInteger.builder().defaultValue(10).required(false).build();
  2. // 123 will be assigned to 'd'
  3. Integer d = (Integer) schemaWithoutRequired.parse(123);
  4. // null will be assigned to 'e'
  5. Integer e = (Integer) schemaWithoutRequired.parse(null);
  6. // will throw InvalidPropTypeException
  7. Integer f = (Integer) schemaWithoutRequired.parse("invalid");
  1. PropType schemaWithoutDefaultValue = PInteger.builder().required(true).build();
  2. // 123 will be assigned to 'g'
  3. Integer g = (Integer) schemaWithoutDefaultValue.parse(123);
  4. // will throw InvalidPropTypeException
  5. Integer h = (Integer) schemaWithoutDefaultValue.parse(null);
  6. // will throw InvalidPropTypeException
  7. Integer i = (Integer) schemaWithoutDefaultValue.parse("invalid");

Default value is only used when required is true. So schemaWithoutRequired is the same as schemaWithoutDefaultValueAndRequired. This behaviour can be overriden by creating custom prop-types, which we will look at further in this document.

  1. PropType schemaWithoutDefaultValueAndRequired = PInteger.builder().required(false).build();
  2. // 123 will be assigned to 'j'
  3. Integer j = (Integer) schemaWithoutDefaultValueAndRequired.parse(123);
  4. // null will be assigned to 'k'
  5. Integer k = (Integer) schemaWithoutDefaultValueAndRequired.parse(null);
  6. // will throw InvalidPropTypeException
  7. Integer l = (Integer) schemaWithoutDefaultValueAndRequired.parse("invalid");

PInteger, PFloat, PBoolean, PString, PObject have the same properties.

required -> to validate if the given value is required or not
defaultValue -> if required and the value is null, it will be replaced with this value

PArray, PShape are special. They allow us to compose complex structures.

PArray

As the name suggests, it is used to define arrays.

PArray has two additional properties items, additionalItems

Lets say you want to validate a List which has this list [ 1, true, "sample", 2.0 ].
The items property comes in handy. It allows you to define the order of prop-types to be used to validate the List.

  1. PropType schema = PArray.builder().items(Lists.newArrayList(
  2. PInteger.builder().required(true).build(),
  3. PBoolean.builder().required(true).build(),
  4. PString.builder().required(true).build(),
  5. PFloat.builder().required(true).build()
  6. )).required(true).build();
  7. List<Object> a = schema.parse(Lists.newArrayList(1, true, "as", 2.0));

There might be a use case where you aren’t aware the number items of the list. additionalItems will help you express such lists.

  1. PropType schema = PArray.builder().additionalItems(
  2. PInteger.builder().required(true).build()
  3. ).required(true).build();
  4. List<Integer> a = (List<Integer>) schema.parse(Lists.newArrayList(1, 2, 3, 4));

additionalItems can be clubbed with items to express lists like [1, true, "as", 2.0, 2, 3, 4, 5, 6, ... ]

  1. PropType schema = PArray.builder().items(Lists.newArrayList(
  2. PInteger.builder().required(true).build(),
  3. PBoolean.builder().required(true).build(),
  4. PString.builder().required(true).build(),
  5. PFloat.builder().required(true).build()
  6. )).additionalItems(
  7. PInteger.builder().required(true).build()
  8. ).required(true).build();
  9. List<Object> a = (List<Object>) schema.parse(Lists.newArrayList(1, true, "as", 2.0, 1, 2, 3, 4, 5, 6));

PShape

It allows you to define the structure of a Map

PShape has a special property schema, which is a map of String:PropType. It is used to validate Maps like

  1. {
  2. "integer": 10,
  3. "float": 2.0,
  4. "boolean": true,
  5. "string": "sample",
  6. "object": "any random object",
  7. "array": [ 1, 2, 3, 4, 5 ]
  8. }

The below mentioned schema validates the above mentioned data

  1. Map<String, PropType> shape = Maps.newHashMap();
  2. shape.put("integer",
  3. PInteger.builder().required(true).build()
  4. );
  5. shape.put("float",
  6. PFloat.builder().required(true).build()
  7. );
  8. shape.put("boolean",
  9. PBoolean.builder().required(true).build()
  10. );
  11. shape.put("string",
  12. PString.builder().required(true).build()
  13. );
  14. shape.put("object",
  15. PObject.builder().required(true).build()
  16. );
  17. shape.put("array",
  18. PArray.builder().additionalItems(
  19. PInteger.builder().required(true).build()
  20. ).required(true).build()
  21. );
  22. PropType schema = PShape.builder().schema(shape).required(true).build();
  23. Map<String, Object> data = Maps.newHashMap();
  24. data.put("integer", 10);
  25. data.put("float", 2.0);
  26. data.put("boolean", true);
  27. data.put("string", "sample");
  28. data.put("object", "any random object");
  29. data.put("array", Lists.newArrayList(1, 2, 3, 4, 5));
  30. Map<String, Object> a = (Map<String, Object>) schema.parse(data);

Custom PropType

You can create your custom prop-types by implementing the PropType interface

  1. public interface PropType {
  2. String getType();
  3. Object parse(Object value) throws InvalidPropTypeException;
  4. Boolean isRequired();
  5. Object getDefaultValue();
  6. boolean isValid(Object value);
  7. }

License

prop-types is released under the Apache 2 License. Check LICENSE file for more information.