All Projects → xieyuheng → ty

xieyuheng / ty

Licence: GPL-3.0 License
Here is a schema checker which can return well typed results. Tell your friends!

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to ty

another-json-schema
Another JSON Schema validator, simple & flexible & intuitive.
Stars: ✭ 48 (+128.57%)
Mutual labels:  schema, json-schema, validator
magnet
A JSON/BSON schema generator
Stars: ✭ 16 (-23.81%)
Mutual labels:  schema, json-schema
Maat
Validation and transformation library powered by deductive ascending parser. Made to be extended for any kind of project.
Stars: ✭ 27 (+28.57%)
Mutual labels:  json-schema, validator
joyce
Joyce is a highly scalable event-driven Cloud Native Data Hub.
Stars: ✭ 37 (+76.19%)
Mutual labels:  schema, json-schema
Joiful
TypeScript Declarative Validation for Joi
Stars: ✭ 177 (+742.86%)
Mutual labels:  schema, validator
Vue Form Json Schema
Create forms using JSON schema. Bring your components!
Stars: ✭ 253 (+1104.76%)
Mutual labels:  schema, json-schema
garn-validator
Create validations with ease
Stars: ✭ 42 (+100%)
Mutual labels:  schema, validator
Awesome Python Models
A curated list of awesome Python libraries, which implement models, schemas, serializers/deserializers, ODM's/ORM's, Active Records or similar patterns.
Stars: ✭ 124 (+490.48%)
Mutual labels:  schema, validator
typescript-to-json-schema
Generate JSON schema from your Typescript sources
Stars: ✭ 54 (+157.14%)
Mutual labels:  schema, json-schema
volder
volder is powerful Object schema validation lets you describe your data using a simple and readable schema and transform a value to match the requirements
Stars: ✭ 106 (+404.76%)
Mutual labels:  schema, validator
openui5-validator
A library to validate OpenUI5 fields
Stars: ✭ 17 (-19.05%)
Mutual labels:  json-schema, validator
Newtonsoft.json.schema
Json.NET Schema is a powerful, complete and easy to use JSON Schema framework for .NET
Stars: ✭ 167 (+695.24%)
Mutual labels:  schema, json-schema
Schema Utils
Options Validation
Stars: ✭ 162 (+671.43%)
Mutual labels:  schema, validator
sf-java-ui
Json Schema Form java based library allow developers to define schema and form using field annotations
Stars: ✭ 23 (+9.52%)
Mutual labels:  schema, json-schema
Schema Typed
Schema for data modeling & validation
Stars: ✭ 150 (+614.29%)
Mutual labels:  schema, validator
finspec-spec
Multi-protocol, machine-readable specifications for financial services
Stars: ✭ 18 (-14.29%)
Mutual labels:  json-schema, validator
Schema Registry
Confluent Schema Registry for Kafka
Stars: ✭ 1,647 (+7742.86%)
Mutual labels:  schema, json-schema
Typedload
Python library to load dynamically typed data into statically typed data structures
Stars: ✭ 120 (+471.43%)
Mutual labels:  schema, json-schema
openapi4j
OpenAPI 3 parser, JSON schema and request validator.
Stars: ✭ 92 (+338.1%)
Mutual labels:  schema, json-schema
element-schema-form
A schema-based element-ui form component for Vue2.x.
Stars: ✭ 31 (+47.62%)
Mutual labels:  schema, json-schema

Ty

Write schema to bring TypeScript's types to runtime.

Which can be used to:

  • Validate untyped data and return well typed result.
  • Generate random data of a given schema, to do property-based testing.
    • We also provide a library of logic theories, to be used as target of models.

Install

npm i @xieyuheng/ty

Examples

Validation untyped data

import ty, { Obtain } from "@xieyuheng/ty"

const userSchema = ty.object({
  id: ty.int({ min: 0 }),
  first_name: ty.string(),
  last_name: ty.string(),
})


type User = Obtain<typeof userSchema>

// NOTE We can extract a `User` type from the type of `userSchema`,
//   which will be the same as the following type definition:

// type User = {
//   id: number
//   first_name: string
//   last_name: string
// }

{
  const data: any = {
    id: 1,
    first_name: "Yuheng",
    last_name: "Xie",
  }

  const user: User = userSchema.validate(data)
  const userOmitId: Omit<User, "id"> = ty.omit(userSchema, "id").validate(data)
}

Generate random data of a given schema

{
  const user: User = userSchema.generate()

  userSchema.validate(user)

  console.log(user)
  // Will print something like:
  //   { id: 0, first_name: 'ada4a39ab0', last_name: '73be' }
}

Recursive and generic schema

type List<T> = null | { head: T; tail: List<T> }

function cons<T>(head: T, tail: List<T>): List<T> {
  return { head, tail }
}

function listSchema<T>(itemSchema: Schema<T>): Schema<List<T>> {
  const nullSchema = ty.null()
  const consSchema = ty.object({
    head: itemSchema,
    tail: ty.lazy(() => listSchema(itemSchema)),
  })
  return ty.union(nullSchema, consSchema)
}

{
  const schema = listSchema(ty.string())
  const data0: List<string> = schema.validate(null)
  const data1: List<string> = schema.validate(cons("a", null))
  const data2: List<string> = schema.validate(cons("a", cons("b", null)))
  const data3: List<string> = schema.validate(
    cons("a", cons("b", cons("c", null)))
  )
  schema.assertInvalid(cons(1, null))
  schema.assertInvalid(cons(1, cons(2, null)))
  schema.assertInvalid(cons(1, cons(2, cons(3, null))))
}

{
  const schema = listSchema(ty.number())
  const data0: List<number> = schema.validate(null)
  const data1: List<number> = schema.validate(cons(1, null))
  const data2: List<number> = schema.validate(cons(1, cons(2, null)))
  const data3: List<number> = schema.validate(cons(1, cons(2, cons(3, null))))
  schema.assertInvalid(cons("a", null))
  schema.assertInvalid(cons("a", cons("b", null)))
  schema.assertInvalid(cons("a", cons("b", cons("c", null))))
}

Property-based testing

TODO

Logic theories (interface and typeclass of laws)

To be used as target of models, for property-based testing.

TODO

API Docs

Primitive:

Collection:

Set-Theoretic:

Structural:

Recursion:

Similar projects

Runtime typing:

Property-based testing and data generation:

Contributions

Be polite, do not bring negative emotion to others.

License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].