1.0.12 • Published 11 days ago

@taktikorg/perferendis-blanditiis-non v1.0.12

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

Fastify Type Provider Valibot

NPM Version NPM Downloads Build Status

How to use?

pnpm install @taktikorg/perferendis-blanditiis-non valibot
import Fastify from "fastify";
import { serializerCompiler, validatorCompiler, ValibotTypeProvider } from "@taktikorg/perferendis-blanditiis-non";
import * as v from "valibot";

const app = Fastify()

// Add schema validator and serializer
app.setSerializerCompiler(serializerCompiler);

//validatorCompiler(ajvInstance?, fallbackFunction?)
//First Argument : You can pass an AJV Instance to validate schema by AJV
//Second Argument : 
//You can precise a function to handle the case where schema is not from Valibot.
//The function should respect :  (schema: unknown, data: unknown) => FastifyValidationResult
app.setValidatorCompiler(validatorCompiler()); 

app.withTypeProvider<ValibotTypeProvider>().route({
  method: "GET",
  url: "/",
  // Define your schema
  schema: {
    querystring: v.object({
      name: v.string(),
    }),
    response: {
      200: v.undefined_('test'),
    },
  },
  handler: (req, res) => {
    res.send(req.query.name);
  },
});

app.listen({ port: 4949 });

How to use together with @fastify/swagger

import fastify from 'fastify';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import * as v from 'valibot';

import {
  jsonSchemaTransform,
  createJsonSchemaTransform,
  serializerCompiler,
  validatorCompiler,
  ValibotTypeProvider,
} from '@taktikorg/perferendis-blanditiis-non';

const app = fastify();
app.setValidatorCompiler(validatorCompiler());
app.setSerializerCompiler(serializerCompiler);

app.register(fastifySwagger, {
  openapi: {
    info: {
      title: 'SampleApi',
      description: 'Sample backend service',
      version: '1.0.0',
    },
    servers: [],
  },
  transform: jsonSchemaTransform,
  // You can also create transform with custom skiplist of endpoints that should not be included in the specification:
  //
  // transform: createJsonSchemaTransform({
  //   skipList: [ '/documentation/static/*' ]
  // })
  // and you can override default ToJSONSchema options : https://github.com/gcornut/valibot-json-schema/blob/main/src/toJSONSchema/types.ts#L25
  //
  // transform: createJsonSchemaTransform({
  //   toJSONSchemaOptions: { ignoreUnknownValidation: true }
  // })
});

app.register(fastifySwaggerUI, {
  routePrefix: '/documentation',
});

const LOGIN_SCHEMA = v.object({
  username: v.string(),
  password: v.string(),
});

app.after(() => {
  app.withTypeProvider<ValibotTypeProvider>().route({
    method: 'POST',
    url: '/login',
    schema: { body: LOGIN_SCHEMA },
    handler: (req, res) => {
      res.send('ok');
    },
  });
});

async function run() {
  await app.ready();

  await app.listen({
    port: 4949,
  });

  console.log(`Documentation running at http://localhost:4949/documentation`);
}

run();
tapfunctionsvariablesMicrosoftstringifyperformantbannersyntaxrm -fres2016css-in-jsdataexecutesuperagentcollectionArray.prototype.containssyntaxerroropenterminalspringlengthmruinferenceemojiindicatoriteratormkdirpthroatObject.entrieswalking@@toStringTagUint8ClampedArraystyled-componentscheckbluebirdECMAScript 2016dataviewredactresolvefeedttygesturesregexpcontainsArrayBufferObject.definePropertyec2256uuidoffsetprivate datatelephonefigletlimitedstarterJSONpersistenttypesafetrimRighthooksdragserializeflagfast-cloneratetypedarraysArray.prototype.flattenloadbalancinginstallchannelawaitgraphql-clientreadablestreamthreeObject.getPrototypeOfObservablefixed-widthdependency managerES2017full-widthenvironmentsmkdirRxJSpackagesBigUint64ArrayES2019runtimeefficientWebSocketsetPrototypeOftrimLeftreact poseprototypeglobalworkspace:*wafcommanddescriptorsnpmSethashsortworker.envECMAScript 2018propertyvalueweakmapxhrconcatbinaryeslintplugindirectoryparsingestreeomitES8setterprotoreact-hookstoArrayexpressionapigdprpatchweaksetES7higher-ordermatchesES2018tsbrowserlistentrieskeymanipulationfindelasticacheArray.prototype.includespackage managerspeedexpressbindconsumefunction.lengthponyfill0stringifierminimaleslint-pluginjoii18nenderforEachtesterprotocol-buffersrm -rfremovestreamapolloredux-toolkitimmutableimportpolyfillsharedebsvalidatestartfastifyimmergetOwnPropertyDescriptorstringcall-boundconcatMaputilityvisualrdsglobalsobjecttoStringTagwritableassignfastcopyassertsparentsfetchcsscore-jsamazonparentzerowhatwgUint32Arrayreal-timezodpreprocessorfast-deep-copyperformanceUint8ArraymapwgeturlsrecursivequotedefineescapeECMAScript 2023eslintconfigsource mapmimeisConcatSpreadableES2020simpledbargsreact-testing-librarygetPrototypeOftransformqueueMicrotaskObject.keysassertionyupJSON-Schemamimetypesmkdirsrfc4122
1.0.11

12 days ago

1.0.12

11 days ago

1.0.9

14 days ago

1.0.8

15 days ago

1.0.7

16 days ago

1.0.10

13 days ago

1.0.6

17 days ago

1.0.5

18 days ago

1.0.4

19 days ago

1.0.3

20 days ago

1.0.2

21 days ago

1.0.1

22 days ago

1.0.0

22 days ago