Skip to content

Commit

Permalink
Improve schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
cowuake committed Oct 11, 2024
1 parent dd3031e commit 22b93f1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 58 deletions.
38 changes: 23 additions & 15 deletions src/schemas/movies/data.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
import { MovieSchema } from './data';
import { MovieSchema, type MovieSchemaType } from './data';
import { TypeCompiler } from '@sinclair/typebox/compiler';

describe('MovieSchema', () => {
const validate = TypeCompiler.Compile(MovieSchema);

const movieTitle = 'Yet Another Movie';
const movieType = 'movie';
// const movieCast = ['This Guy', 'That Guy', 'The Other Guy', 'Guybrush Threepwood'];
// const movieGenres = ['Action', 'Adventure', 'Comedy'];
const movieYear = 2022;

const movieWithMandatoryFields: MovieSchemaType = {
title: movieTitle,
type: movieType,
year: movieYear
};

it('should validate a movie with at least the mandatory fields', () => {
const validMovie = movieWithMandatoryFields;
expect(validate.Check(validMovie)).toBe(true);
});

it('should validate a valid movie', () => {
const validUser = {
title: movieTitle,
type: movieType
};
expect(validate.Check(validUser)).toBe(true);
it('should not validate a movie without an empty cast', () => {
const invalidMovie = { ...movieWithMandatoryFields, cast: [] };
expect(validate.Check(invalidMovie)).toBe(false);
});

it('should not validate a movie without a title', () => {
const invalidUser = {
type: movieType
};
expect(validate.Check(invalidUser)).toBe(false);
const invalidMovie = { ...movieWithMandatoryFields, title: '' };
expect(validate.Check(invalidMovie)).toBe(false);
});

it('should not validate a movie without a type', () => {
const invalidUser = {
title: movieTitle
};
expect(validate.Check(invalidUser)).toBe(false);
const invalidMovie = { ...movieWithMandatoryFields, type: '' };
expect(validate.Check(invalidMovie)).toBe(false);
});
});
89 changes: 46 additions & 43 deletions src/schemas/movies/data.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
import { type Static, Type } from '@sinclair/typebox';

const StringSchema = Type.String({ minLength: 1 });
const StringArraySchema = Type.Array(StringSchema, { minItems: 1 });
const FloatSchema = Type.Number({ format: 'float' });
const NaturalSchema = Type.Integer({ minimum: 0, default: 0 });
const UriSchema = Type.String({ format: 'uri' });

const AwardsSchema = Type.Object({
wins: NaturalSchema,
nominations: NaturalSchema,
text: StringSchema
});

const ImdbSchema = Type.Object({
id: StringSchema,
rating: FloatSchema,
votes: NaturalSchema
});

const TomatoesSchema = Type.Object({
viewer: Type.Object({
meter: NaturalSchema,
numReviews: NaturalSchema,
rating: FloatSchema
}),
lastUpdated: StringSchema
});

const MovieMandatoryFieldsSchema = Type.Object({
title: Type.String(),
type: Type.String()
title: StringSchema,
type: StringSchema,
year: NaturalSchema
});

const MovieOptionalFieldsSchema = Type.Partial(
Type.Object({
awards: Type.Object({
wins: Type.Number(),
nominations: Type.Number(),
text: Type.String()
}),
cast: Type.Array(Type.String()),
countries: Type.Array(Type.String()),
directors: Type.Array(Type.String()),
fullplot: Type.String(),
genres: Type.Array(Type.String()),
imdb: Type.Partial(
Type.Object({
id: Type.String(),
rating: Type.Number(),
votes: Type.Number()
})
),
languages: Type.Array(Type.String()),
lastupdated: Type.String(),
awards: AwardsSchema,
cast: StringArraySchema,
countries: StringArraySchema,
directors: StringArraySchema,
fullplot: StringSchema,
imdb: Type.Partial(ImdbSchema),
languages: StringArraySchema,
lastupdated: StringSchema,
metacritic: Type.Number(),
num_mflix_comments: Type.Number(),
plot: Type.String(),
poster: Type.String(),
rated: Type.String(),
released: Type.String(),
runtime: Type.Number(),
tomatoes: Type.Partial(
Type.Object({
viewer: Type.Optional(
Type.Partial(
Object({
meter: Type.Number(),
numReviews: Type.Number(),
rating: Type.Number()
})
)
),
lastUpdated: Type.String()
})
),
writers: Type.Array(Type.String()),
year: Type.Any()
num_mflix_comments: NaturalSchema,
plot: StringSchema,
poster: UriSchema,
rated: StringSchema,
released: StringSchema,
runtime: NaturalSchema,
tomatoes: Type.Partial(TomatoesSchema),
writers: StringArraySchema
})
);

Expand Down

0 comments on commit 22b93f1

Please sign in to comment.