All Projects → keplr-team → typed-ajv

keplr-team / typed-ajv

Licence: MIT license
Define TypeScript types and JSON Schema schemas from the same declarations

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to typed-ajv

nextjs-boilerplate
Jam3 NextJS Generator for SPA, SSG, SSR and JAMStack applications
Stars: ✭ 95 (+427.78%)
Mutual labels:  backend
kotlin-no-backend
Lista de empresas que utilizam Kotlin no Brasil.
Stars: ✭ 46 (+155.56%)
Mutual labels:  backend
foss-events
A simple website with a collection of open-source events happening across the globe. This is a beginner-friendly repository that helps you learn git and contribute to web projects. Happy Hacktober!
Stars: ✭ 123 (+583.33%)
Mutual labels:  backend
front-end
定位为非前端开发同学,科普前端
Stars: ✭ 33 (+83.33%)
Mutual labels:  backend
ITL
Sample Web API implementation with .NET Core and DDD using Clean Architecture.
Stars: ✭ 29 (+61.11%)
Mutual labels:  backend
typeless
running wild with shapeless
Stars: ✭ 16 (-11.11%)
Mutual labels:  backend
I18next Xhr Backend
[deprecated] can be replaced with i18next-http-backend
Stars: ✭ 252 (+1300%)
Mutual labels:  backend
Dragon
基于 .Net Core 的后端基础框架
Stars: ✭ 17 (-5.56%)
Mutual labels:  backend
solr
Apache Solr open-source search software
Stars: ✭ 651 (+3516.67%)
Mutual labels:  backend
trampolim
A modern Python build backend
Stars: ✭ 39 (+116.67%)
Mutual labels:  backend
exocore
A distributed private application framework
Stars: ✭ 50 (+177.78%)
Mutual labels:  backend
MultiplatformPlayground
Kotlin Multiplatform project in Jetpack Compose & SwiftUI with shared ViewModel layer and File upload
Stars: ✭ 72 (+300%)
Mutual labels:  backend
awesome-coder-resources
编程路上加油站!------【持续更新中...欢迎star,欢迎常回来看看......】【内容:编程/学习/阅读资源,开源项目,面试题,网站,书,博客,教程等等】
Stars: ✭ 54 (+200%)
Mutual labels:  backend
oh-my-backend
Что нужно знать бэкенд-разработчику web-приложений. Backend Roadmap (from Junior to Senior).
Stars: ✭ 657 (+3550%)
Mutual labels:  backend
Pharmacy-Mangment-System
👨‍💻 🏥 MEAN stack Pharmacy Management system.
Stars: ✭ 229 (+1172.22%)
Mutual labels:  backend
Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (+1305.56%)
Mutual labels:  backend
todo
An example todo application with Go!
Stars: ✭ 61 (+238.89%)
Mutual labels:  backend
be-the-hero-omnistack-11
Semana OmniStack 11 - Be The Hero [possui alterações pessoais]
Stars: ✭ 14 (-22.22%)
Mutual labels:  backend
backend-server
📠 The backend of the Fairfield Programming Association website.
Stars: ✭ 26 (+44.44%)
Mutual labels:  backend
gae-vue-webapp2-starter
A simple GAE Vue Webapp2 starter project.
Stars: ✭ 17 (-5.56%)
Mutual labels:  backend

typed-ajv

Define TypeScript types and JSON Schema schemas from the same declaration.

Getting started

Define your schema:

const cs = CS.Object({
  a: CS.String(true),
  b: CS.Number(false),
});

Get the type:

type Type = typeof cs.type; // { a: string, b?: number }

Get the json schema:

const jsonSchema = cs.getJsonSchema(); // { type: 'object', properties: [...] }

Validate your input data and type it :

if (ajv.validate(jsonSchema, inputData)) {
  const data: Type = inputData;
}

Supported primitive types

Type Description
Any Anything
Boolean
Const Constant of any type
Integer An integer type as number
Number
String
Unknown Anything typed as unknown
Null Only null

Supported compound types

Type Description
AnyOf Any type within a selection of definitions
Array An array of a type
Enum A string with enumerated values
Object An object with typed properties
MergeObjects Merge two object definitions into one object containing all the properties

Helpers

Type Description Example
Required Changes the type of the schema to be required CS.Required(CS.String(false)) === CS.String(true)
Optional Changes the type of the schema to be optional CS.Optional(CS.String(true)) === CS.String(false)

Using AnyOf with objects and removeAdditional: true

AnyOf, when ajv is configured with removeAdditional: true, doesn't behave the expected way. For example :

CS.AnyOf(
  [
    CS.Object(
      { type: CS.Enum(['car'] as const, true), wheels: CS.Number(true) },
      true,
    ),
    CS.Object(
      { type: CS.Enum(['horse'] as const, true), legs: CS.Number(true) },
      true,
    ),
  ],
  true,
);

The above schema will unexpectedly fail to validate {type: 'horse', legs: 4} because, when evaluating the 'car' option, ajv will remove all the properties except type and wheels. Thus the 'horse' option will be evaluated with the legs property removed and fail to validate.

For the above schema to work as expected, use the discriminator option:

CS.AnyOf(
  [
    CS.Object(
      { type: CS.Enum(['car'] as const, true), wheels: CS.Number(true) },
      true,
    ),
    CS.Object(
      { type: CS.Enum(['horse'] as const, true), legs: CS.Number(true) },
      true,
    ),
  ],
  true,
  { discriminator: 'type' },
);

Ajv will also need to be initialized with the discriminator: true option.

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].