0.0.6 • Published 1 year ago

@coderich/quin v0.0.6

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

Quin

Data Validation & Transformation

Quin is a GraphQL plugin for data validation and transformation. It provides a custom @quin directive for declarative schema design plus a JavaScript API for runtime validation.

Features include:

  • Validation & Transformation Framework
  • JavaScript Schema API
  • Fully Extensible

Basic Example:

Decorate a GraphQL schema with @quin:

Type User {
  id: ID!
  name: String! @quin(transform: [trim, toTitleCase])
  emailAddress: String! @quin(transform: toLowerCase, enforce: email)
}

Validate schema at runtime:

const schema = new Quin(gql);
const userModel = schema.getModel('User');

// Validate will first run any transformers
userModel.validate({ name: ' QUIN ' }); // Returns { name: 'Quin' }
userModel.validate({ emailAddress: 'foobar' }); // Throws Error

Quin Directive

By default the @quin directive provides two key-value pairs for immediate use:

keyvalueexample
transformList of Transformer enums to apply@quin(transform: [trim, toTitleCase])
enforceList of Rule enums to enforce@quin(enforce: [email, immutable])

Note: email and immutable are examples of custom Rules

Default Transformers

Below is a list of default Transformers that come with @quin:

enumdescription
trimRemove whitespace from both ends of a string
trimEndRemove whitespace from the end of a string
trimStartRemove whitespace from the start of a string
toLowerCaseConvert string to lower case
toUpperCaseConvert string to upper case
toTitleCaseConvert string to title case
toSentenceCaseConvert string to sentence case
dedupeRemove duplicates from array
timestampReturns Date.now()

Default Rules

There are no default Rules other than the required rule. The required Rule is automatically run when validation takes place on a required ! field.

Quin API

Schema

const schema = new Quin(gql); // Call BEFORE makeExecutableSchema(). Modifies gql.
APIdescriptionReturns
schema.getModels()Retrieve map of all modelsMap <model>
schema.getRules()Retrieve map of all rulesMap <rule>
schema.getTransformers()Retrieve map of all transformersMap <transformer>
schema.getExecutableSchema()Get underlying executable schemaGQLSchema

Model

const model = schema.getModel('User');
APIdescriptionReturns
model.getFields()Retrieve map of all modelsMap <model>
model.transform(data, mapper)Retrieve map of all rulesMap <rule>
model.validate(data, mapper)Retrieve map of all transformersMap <transformer>

Field

const field = model.getField('emailAddress');
APIdescriptionReturns
field.transform(data, mapper)Apply all field transformers to dataTransformed data
field.validate(data, mapper)First performs a transform(...) then enforces all rulesTransformed data or Throws Error

Extending Quin

0.0.3

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.6

1 year ago

0.0.2

4 years ago

0.0.1

4 years ago