3.0.5 • Published 3 years ago

@keplr/typed-ajv v3.0.5

Weekly downloads
1,548
License
MIT
Repository
github
Last release
3 years ago

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

TypeDescription
AnyAnything
Boolean
ConstConstant of any type
IntegerAn integer type as number
Number
String
UnknownAnything typed as unknown
NullOnly null

Supported compound types

TypeDescription
AnyOfAny type within a selection of definitions
ArrayAn array of a type
EnumA string with enumerated values
ObjectAn object with typed properties
MergeObjectsMerge two object definitions into one object containing all the properties

Helpers

TypeDescriptionExample
RequiredChanges the type of the schema to be requiredCS.Required(CS.String(false)) === CS.String(true)
OptionalChanges the type of the schema to be optionalCS.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.

3.0.5

3 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0-rc.2

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.15.2

5 years ago

1.15.0

5 years ago

1.15.1

5 years ago

1.14.0

5 years ago

1.13.1

5 years ago

1.13.0

5 years ago

1.12.3

5 years ago

1.12.1

5 years ago

1.12.0

6 years ago

1.11.2

6 years ago

1.11.1

6 years ago

1.10.1

6 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago