Git Product home page Git Product logo

moshi-validation-adapters's Introduction

Moshi Validation Adapters

โ— Not released yet

A collection of simple JsonAdapters for Moshi.

This library acts as an extension to Moshi by providing general purpose, yet useful JsonAdapters that are not present in the main library. Most provided adapters are linked via specialized JsonQualifier annotations that alter serialization/deserialization strategies.

How To Use It

This library is not forcing any of it's own adapters by default. To leverage from any of the provided annotations/adapters add their respective factory to your Moshi.Builder:

val moshi = Moshi.Builder()  
  .add(DecimalMax.ADAPTER_FACTORY)  
  .build()

Overall contract

Every declared annotation/adapter in this library, supports adapter composition. Which means that no JsonAdapter.Factory will short circuit adapter construction on it's own annotation, and instead will delegate to the next adapter returned by Moshi. Meaning that given the following type declaration:

data class Double(  
    @DecimalMax(value = "100", inclusive = true)  
    @DoubleIntValue val x: Int  
)

The resulting JsonAdapter will validate x from the json response and double the value of it.

One important concept to keep in mind, that the order of declared annotations in the example above does not have any influence on the way how the final adapter will be constructed. Instead the order of the JsonAdapter.Factory's added to the Moshi instance is what plays a major role in overall behavior. Meaning that in order for the example above to satisfy the expected result, one must add the factories in the following order:

val moshi = Moshi.Builder()  
  .add(DecimalMax.ADAPTER_FACTORY)  
  .add(DoubleIntValue)  
  .build()

Available annotations

  • @DecimalMax(value=, inclusive=)
    • Checks whether the annotated value is less than the specified maximum, when inclusive=false. Otherwise whether the value is less than or equal to the specified maximum.
    • The parameter value is the string representation of the max value according to the BigDecimal string representation.
    • Supported data types: Int, Byte, Short and Long
    • Example:
       @DecimalMax(value = "100", inclusive = false) val value: Int

  • @DecimalMin(value=, inclusive=) Checks whether the annotated value is larger than the specified minimum, when inclusive=false. Otherwise whether the value is larger than or equal to the specified minimum.
  • The parameter value is the string representation of the min value according to the BigDecimal string representation.
    • Supported data types: Int, Byte, Short and Long
    • Example:
       @DecimalMin(value = "100", inclusive = false) val value: Int

  • @Digits(integer=, fraction=)
    • Checks whether the annotated value is a number having up to integer digits and fraction fractional digits
    • Supported data types: Float and Double.
    • Example:
       @Digits(integer = 1, fraction = 1) val value: Float

  • @NotEmpty
    • Checks that the annotated string is not empty.
    • Supported data types: String.
    • Example:
       @NotEmpty val string: String

  • @NotBlank
    • Checks that the annotated string is not blank.
    • The difference to @NotEmpty is and that trailing white-spaces are ignored.
    • Supported data types: String.
    • Example:
       @NotBlank val string: String

  • @AssertFalse
    • Checks that the annotated element is false.
    • Supported data types: Boolean.
    • Example:
       @AssertFalse val b: Boolean

  • @AssertTrue
    • Checks that the annotated element is true.
    • Supported data types: Boolean.
    • Example:
       @AssertTrue val b: Boolean

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.